Advertisement
Comatose-1990

NETOS v1.2b

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