Advertisement
Comatose-1990

NETServ v1.0

Nov 3rd, 2013
3,129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
DCPU-16 11.36 KB | None | 0 0
  1. REM ================================
  2. REM * NETLIB v1.1
  3. REM The base library for NET code
  4. REM ================================
  5. NETLIB:
  6.  
  7. REM # Init
  8. SET A, 0
  9. SET B, [@ScreenStart]
  10. HWI 0
  11. JSR ClearScreen
  12. IAS HardwareIterrupt
  13.  
  14. SET PC, [@CodeStart]
  15.  
  16. REM ##############
  17. REM NETLIB Vars
  18. REM ##############
  19. @CodeStart:      DAT NETServ
  20.  
  21. @ScreenStart:    DAT 0x8000
  22. @NextPrint:      DAT 0x7FE0
  23. @ScreenEnd:      DAT 0x817F
  24. @PacketHandler:  DAT 0x0000
  25. @KeyHandler:     DAT 0x0000
  26. @TimeoutAddress: DAT 0x0000
  27. @TimeoutTicks:   DAT 0x0000
  28. @HandlerReg:     DAT 0x0000
  29. @IP:             DAT 0x0000
  30. @ClearPC:        DAT 0x0000
  31.  
  32. @Input:
  33. DAT 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  34.  
  35. $InvalidInput:
  36. DAT "Invalid input", 0x0
  37.  
  38. REM ###############################
  39. REM Wait until you get an IP
  40. REM ###############################
  41. WaitForIP:
  42. SET A, 9
  43. SET B, 0xffff
  44. WaitForIP_hang:
  45. HWI 4
  46. IFE B, 0xffff
  47.   MOV WaitForIP_hang
  48.  
  49. REM # Store IP
  50. SET [@IP], B
  51.  
  52. SET PC, POP
  53.  
  54. REM ###############################
  55. REM Print null-str at [A] on a
  56. REM new line
  57. REM ###############################
  58. PrintLn:
  59. SET PUSH, B
  60.  
  61. REM # Load line to B
  62. JSR LoadNextLine
  63. JSR PrintStr
  64.  
  65. SET B, POP
  66. SET PC, POP
  67.  
  68. REM #################################
  69. REM Loads the address of the next
  70. REM line into B
  71. REM #################################
  72. LoadNextLine:
  73. ADD [@NextPrint], 0x20
  74. IFG [@NextPrint], [@ScreenEnd]
  75.   JSR ScrollUp
  76. SET B, [@NextPrint]
  77.  
  78. SET PC, POP
  79.  
  80. REM ###############################
  81. REM Print null-str at [A]
  82. REM ###############################
  83. PrintStr:
  84. SET PUSH, C
  85.  
  86. PrintStr_loop:
  87. IFE [A], 0
  88.   MOV PrintStr_exit
  89.  
  90. SET C, [A]
  91. BOR C, 0x1900
  92. SET [B], C
  93.  
  94. ADD A, 1
  95. ADD B, 1
  96. MOV PrintStr_loop
  97.  
  98. PrintStr_exit:
  99. SET C, POP
  100. SET PC, POP
  101.  
  102. REM ###############################
  103. REM Print a hex number A to [B]
  104. REM ###############################
  105. PrintHex:
  106. PUSHA
  107.  
  108. REM # "0x"
  109. SET [B], 0x1930
  110. ADD B, 1
  111. SET [B], 0x1978
  112. ADD B, 1
  113.  
  114. SET I, 12
  115. PrintHex_loop:
  116. SET C, A
  117. SHR C, I
  118. AND C, 0xf
  119. IFG C, 0x9
  120.   ADD C, 0x37
  121. IFL C, 0xa
  122.   ADD C, 0x30
  123.  
  124. BOR C, 0x1900
  125. SET [B], C
  126. ADD B, 1
  127. SUB I, 4
  128. IFA I, 0xffff
  129.   SET PC, PrintHex_loop
  130.  
  131. POPA
  132. SET PC, POP
  133.  
  134. REM #################################
  135. REM Print a character C to [B++]
  136. REM #################################
  137. PrintChar:
  138. SET PUSH, A
  139.  
  140. REM # Check for end of line
  141. SET A, [@NextPrint]
  142. ADD A, 0x1E
  143. IFG B, A
  144.   MOV PrintChar_skip
  145.  
  146. SET [B], C
  147. BOR [B], 0x1900
  148. ADD B, 1
  149.  
  150. PrintChar_skip:
  151. SET [B], 0x199D
  152.  
  153. SET A, POP
  154. SET PC, POP
  155.  
  156. REM ##################################
  157. REM Clears screen at [--B]
  158. REM ##################################
  159. PrintBackspace:
  160. SET PUSH, A
  161.  
  162. SET [B], 0x1920
  163.  
  164. REM # Check for start of line
  165. SET A, [@NextPrint]
  166. ADD A, 3
  167. IFL B, A
  168.   MOV PrintBackspace_skip
  169.  
  170. SUB B, 1
  171. SET [B], 0x1920
  172.  
  173. PrintBackspace_skip:
  174. SET [B], 0x199D
  175.  
  176. SET A, POP
  177. SET PC, POP
  178.  
  179. REM #################################
  180. REM Clears the screen to 0x1920
  181. REM #################################
  182. ClearScreen:
  183. SET PUSH, A
  184. SET PUSH, B
  185. SET A, [@ScreenStart]
  186. SET B, [@ScreenEnd]
  187. ADD B, 1
  188.  
  189. ClearScreen_loop:
  190. SET [A], 0x1920
  191. ADD A, 1
  192. IFL A, B
  193.   MOV ClearScreen_loop
  194.  
  195. SET B, POP
  196. SET A, POP
  197. SET PC, POP
  198.  
  199. REM #################################
  200. REM Scroll the screen up a line
  201. REM #################################
  202. ScrollUp:
  203. SET PUSH, C
  204. SET PUSH, I
  205. SET I, [@ScreenStart]
  206.  
  207. SET C, [@ScreenEnd]
  208. SUB C, 0x1F
  209. SET [@NextPrint], C
  210.  
  211. ScrollUp_loop:
  212. SET [I], [I + 0x20]
  213. SET [I + 0x20], 0x1920
  214. ADD I, 1
  215. IFL I, C
  216.   SET PC, ScrollUp_loop
  217.  
  218. SET I, POP
  219. SET C, POP
  220. SET PC, POP
  221.  
  222. REM #################################
  223. REM If string [A] == string [B] then
  224. REM   JSR to C and then
  225. REM   return to I if I != 0
  226. REM else do nothing
  227. REM #################################
  228. StrEquals:
  229. PUSHA
  230.  
  231. StrEquals_loop:
  232. IFN [A], [B]
  233.   MOV StrEquals_nomatch
  234.  
  235. IFE [A], 0
  236.   IFE [B], 0
  237.     MOV StrEquals_match
  238.  
  239. ADD A, 1
  240. ADD B, 1
  241. MOV StrEquals_loop
  242.  
  243. StrEquals_match:
  244. JSR C
  245.  
  246. IFE I, 0
  247.   MOV StrEquals_nomatch
  248.  
  249. POPA
  250. REM # Clear PC from stack
  251. SET [@ClearPC], POP
  252.  
  253. SET PC, I
  254.    
  255. REM # No match found
  256. StrEquals_nomatch:
  257. POPA
  258. SET PC, POP
  259.  
  260.  
  261. REM ##################################
  262. REM Wait for a hex number with then
  263. REM   format #### to be entered
  264. REM ##################################
  265. WaitForNumber:
  266. SET PUSH, B
  267. SET PUSH, C
  268.  
  269. REM # Load line to B
  270. JSR LoadNextLine
  271.  
  272. REM # >
  273. SET [B], 0x193E
  274. ADD B, 2
  275. SET [B], 0x199D
  276.  
  277. SET C, &InputNumber
  278. JSR SetKeyCallback
  279.  
  280. SET C, POP
  281. SET B, POP
  282. SET PC, POP
  283.  
  284. REM # Input line Callback
  285. &InputNumber:
  286. SET PUSH, A
  287. SET PUSH, C
  288.  
  289. SET A, 1
  290. HWI 1
  291. IFE C, 0x10
  292.   JSR PrintBackspace
  293. IFE C, 0x11
  294.   MOV &InputNumber_done
  295.  
  296. REM # Shorter char range check
  297. SET A, [@NextPrint]
  298. ADD A, 0x5
  299. IFG B, A
  300.   MOV &InputNumber_exit
  301.  
  302. REM # 0-9
  303. IFG C, 0x2F
  304.   IFL C, 0x3A
  305.     JSR PrintChar
  306.  
  307. REM # a-f
  308. IFG C, 0x60
  309.   IFL C, 0x67
  310.     JSR PrintChar
  311.  
  312. REM # Not done yet
  313. MOV &InputNumber_exit
  314.  
  315. &InputNumber_done:
  316. SET [B], 0x1920
  317. JSR ClearKeyCallback
  318.  
  319. &InputNumber_exit:
  320. SET C, POP
  321. SET A, POP
  322. SET PC, POP
  323.  
  324. REM #################################
  325. REM Parses a number stored in @Input
  326. REM and stores the result in C
  327. REM If error I == 0xffff
  328. REM #################################
  329. ParseNumber:
  330. SET PUSH, A
  331. SET PUSH, B
  332.  
  333. SET C, 0
  334. SET I, 12
  335.  
  336. SET A, @Input
  337. ParseNumber_loop:
  338. SET B, [A]
  339.  
  340. REM # 0-9
  341. IFL B, 0x3A
  342.   SUB B, 0x30
  343.  
  344. REM # a-f
  345. IFG B, 0x60
  346.   IFL B, 0x67
  347.    SUB B, 0x57
  348.    
  349. IFG B, 0xF
  350.   MOV ParseNumber_bad
  351.  
  352. REM # Add to number
  353. SHL B, I
  354. ADD C, B
  355.  
  356. ADD A, 1
  357. SUB I, 4
  358.  
  359. REM # IF > -1
  360. IFA I, 0xffff
  361.   MOV ParseNumber_loop
  362.  
  363. REM # Is a 4 char number
  364. IFE [A], 0x20
  365.   MOV ParseNumber_done
  366. IFE [A], 0x00
  367.   MOV ParseNumber_done
  368.  
  369. REM # Bad input
  370. ParseNumber_bad:
  371. SET I, 0xffff
  372. SET A, $InvalidInput
  373. JSR PrintLn
  374.  
  375. ParseNumber_done:
  376. SET B, POP
  377. SET A, POP
  378. SET PC, POP
  379.  
  380. REM ##################################
  381. REM Wait for a command to be entered
  382. REM ##################################
  383. WaitForString:
  384. SET PUSH, B
  385. SET PUSH, C
  386.  
  387. REM # Load line to B
  388. JSR LoadNextLine
  389.  
  390. REM # >
  391. SET [B], 0x193E
  392. ADD B, 2
  393. SET [B], 0x199D
  394.  
  395. SET C, &InputLine
  396. JSR SetKeyCallback
  397.  
  398. SET C, POP
  399. SET B, POP
  400. SET PC, POP
  401.  
  402. REM # Input line Callback
  403. &InputLine:
  404. SET PUSH, A
  405. SET PUSH, C
  406.  
  407. SET A, 1
  408. HWI 1
  409. IFE C, 0x10
  410.   JSR PrintBackspace
  411. IFE C, 0x11
  412.   MOV &InputLine_done
  413. IFG C, 0x1F
  414.   IFL C, 0x80
  415.     JSR PrintChar
  416.  
  417. REM # Not done
  418. MOV &InputLine_exit
  419.  
  420. &InputLine_done:
  421. SET [B], 0x1920
  422. JSR ClearKeyCallback
  423. JSR ParseLineInput
  424.  
  425. &InputLine_exit:
  426. SET C, POP
  427. SET A, POP
  428. SET PC, POP
  429.  
  430. REM #################################
  431. REM Parses the input out of this line
  432. REM #################################
  433. ParseLineInput:
  434. SET PUSH, A
  435. SET PUSH, B
  436.  
  437. SET A, @Input
  438. SET B, [@NextPrint]
  439. ADD B, 2
  440.  
  441. ParseLineInput_loop:
  442. IFE [B], 0x1920
  443.   MOV ParseLineInput_end
  444.  
  445. SET [A], [B]
  446. AND [A], 0x00ff
  447. ADD A, 1
  448. ADD B, 1
  449. MOV ParseLineInput_loop
  450.  
  451. ParseLineInput_end:
  452. REM # End with null
  453. SET [A], 0
  454.  
  455. SET B, POP
  456. SET A, POP
  457. SET PC, POP
  458.  
  459. REM ##################################
  460. REM Set an event for a keyboard event
  461. REM Will MOV to C when event triggers
  462. REM Hangs until @KeyHandler == 0
  463. REM ##################################
  464. SetKeyCallback:
  465. SET PUSH, A
  466. SET PUSH, B
  467.  
  468. SET A, 3
  469. SET B, C
  470. SET [@KeyHandler], B
  471. HWI 1
  472.  
  473. SET B, POP
  474. SET A, POP
  475.  
  476. REM # Wait for the state to update
  477. SetKeyCallback_wait:
  478. IFN [@KeyHandler], 0
  479.   MOV SetKeyCallback_wait
  480.  
  481. SET PC, POP
  482.  
  483. REM ##################################
  484. REM Clears a KeyCallback
  485. REM ##################################
  486. ClearKeyCallback:
  487. SET PUSH, A
  488. SET PUSH, B
  489. SET [@KeyHandler], 0x0000
  490.  
  491. SET A, 0
  492. HWI 1
  493.  
  494. SET A, 3
  495. SET B, 0
  496. HWI 1
  497.  
  498. SET A, POP
  499. SET B, POP
  500. SET PC, POP
  501.  
  502. REM ##################################
  503. REM Set event for receiving a packet
  504. REM Will MOV to C when event triggers
  505. REM ##################################
  506. SetPacketCallback:
  507. SET PUSH, A
  508. SET PUSH, B
  509.  
  510. SET A, 0xC
  511. SET B, C
  512. SET [@PacketHandler], B
  513. HWI 4
  514.  
  515. SET B, POP
  516. SET A, POP
  517. SET PC, POP
  518.  
  519. REM ##################################
  520. REM Clears a PacketCallback
  521. REM ##################################
  522. ClearPacketCallback:
  523. SET PUSH, A
  524. SET PUSH, B
  525. SET [@PacketHandler], 0x0000
  526.  
  527. REM # Clear packet buffer
  528. SET A, 0xB
  529. HWI 4
  530.  
  531. REM # Clear message
  532. SET A, 0xC
  533. SET B, 0
  534. HWI 4
  535.  
  536. SET A, POP
  537. SET B, POP
  538. SET PC, POP
  539.  
  540. REM ##################################
  541. REM Starts a timeout which will MOV
  542. REM to B after A seconds
  543. REM * NOTE:
  544. REM    Timeout is not a handler,
  545. REM    will permanently move to the
  546. REM    timeout address  
  547. REM ##################################
  548. SetTimeout:
  549. SET PUSH, A
  550. SET PUSH, B
  551.  
  552. SET [@TimeoutTicks], A
  553. SET [@TimeoutAddress], B
  554.  
  555. REM # Int with message B
  556. SET A, 2
  557. HWI 2
  558.  
  559. REM # Start timer
  560. SET A, 0
  561. SET B, 60
  562. HWI 2
  563.  
  564. SET B, POP
  565. SET A, POP
  566. SET PC, POP
  567.  
  568. REM ##################################
  569. REM Checks for timeout completion, if
  570. REM done moves to the timeout address
  571. REM ##################################
  572. DoTimeout:
  573. SET PUSH, C
  574.  
  575. REM # Get ticks
  576. SET A, 1
  577. HWI 2
  578.  
  579. REM # If less than timeout ticks skip
  580. IFL C, [@TimeoutTicks]
  581.   MOV DoTimeout_notyet
  582. SET C, POP
  583.  
  584. REM # Clear the timeout
  585. SET [@HandlerReg], [@TimeoutAddress]
  586. JSR ClearTimeout
  587.  
  588. REM # Reset from JSR
  589. SET [@ClearPC], POP
  590.  
  591. REM # Reset from interrupt
  592. SET A, POP
  593. SET [@ClearPC], POP
  594. IAQ 0
  595.  
  596. REM # Move to the handler
  597. SET PC, [@HandlerReg]
  598.  
  599. DoTimeout_notyet:
  600. SET C, POP
  601. SET PC, POP
  602.  
  603. REM ##################################
  604. REM Clears the current timeout
  605. REM ##################################
  606. ClearTimeout:
  607. SET PUSH, A
  608. SET PUSH, B
  609. SET [@TimeoutAddress], 0
  610.  
  611. REM # Stop timer
  612. SET A, 0
  613. SET B, 0
  614. HWI 2
  615.  
  616. REM # Clear message
  617. SET A, 2
  618. SET B, 0
  619. HWI 2
  620.  
  621. SET B, POP
  622. SET A, POP
  623. SET PC, POP
  624.  
  625. REM ##################################
  626. REM The hardware interrupt handler
  627. REM ##################################
  628. HardwareIterrupt:
  629. IFE A, 0
  630.   RFI 0x0
  631.  
  632. REM # Event handlers
  633. IFE A, [@KeyHandler]
  634.   JSR [@KeyHandler]
  635.  
  636. IFE A, [@PacketHandler]
  637.   JSR [@PacketHandler]
  638.  
  639. REM # Insertion handlers
  640. IFE A, [@TimeoutAddress]
  641.   JSR DoTimeout
  642.  
  643. RFI 0x0
  644.  
  645. FILL NETLIB, 512
  646.  
  647. REM ===================================
  648. REM * NETServ v1.0
  649. REM Host a dedicated server
  650. REM ===================================
  651. NETServ:
  652. SET A, $NetServ
  653. JSR PrintLn
  654.  
  655. SET A, $Connecting
  656. JSR PrintLn
  657.  
  658. JSR WaitForIP
  659.  
  660. REM # Print "Connected IP: #"
  661. SET B, [@NextPrint]
  662. SET A, $Connected
  663. JSR PrintStr
  664.  
  665. SET A, [@IP]
  666. JSR PrintHex
  667.  
  668. REM # Open ping port
  669. SET A, 0
  670. SET B, 64
  671. HWI 4
  672.  
  673. REM # Listen for packet
  674. SET C, &ServerPacket
  675. JSR SetPacketCallback
  676.  
  677. SET A, $ServerStarted
  678. JSR PrintLn
  679.  
  680. Server_loop:
  681. SET PC, Server_loop
  682.  
  683. NETServ_exit:
  684. HNG
  685.  
  686. REM ##########################
  687. REM # Server packet handler
  688. REM ##########################
  689. &ServerPacket:
  690. REM # Prepare to read
  691. SET A, 6
  692. HWI 4
  693.  
  694. REM # Check for ping packet
  695. SET A, 8
  696.   REM # First word 0x0001
  697.   HWI 4
  698.   IFN B, 1
  699.     MOV &ServerPacket_exit
  700.  
  701.   REM # Reached end
  702.   HWI 4
  703.   IFN B, 0
  704.     MOV &ServerPacket_exit
  705.  
  706. REM # Get sender IP
  707. SET A, 7
  708. HWI 4
  709. SET I, B
  710.  
  711. REM # Set target
  712. SET B, I
  713. SET A, 3
  714. SET C, 64
  715. HWI 4
  716.  
  717. REM # Append 2
  718. SET A, 4
  719. SET B, 2
  720. HWI 4
  721.  
  722. REM # Send packet
  723. SET A, 5
  724. HWI 4
  725.  
  726. REM # Print sender IP
  727. SET A, I
  728. JSR LoadNextLine
  729. JSR PrintHex
  730.  
  731. &ServerPacket_exit:
  732. SET PC, POP
  733.  
  734. REM ##############
  735. REM Strings
  736. REM  < 32 chars
  737. REM ##############
  738. $NetServ:
  739. DAT "NETServ v1.0", 0x0
  740.  
  741. $Connecting:
  742. DAT "Connecting...", 0x0
  743.  
  744. $Connected:
  745. DAT "Connected IP: ", 0x0
  746.  
  747. $ServerStarted:
  748. DAT "Server started", 0x0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement