Advertisement
Comatose-1990

NETLIB v2.0

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