stage0_monitor.s 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. ; Copyright (C) 2016 Jeremiah Orians
  2. ; This file is part of stage0.
  3. ;
  4. ; stage0 is free software: you can redistribute it and/or modify
  5. ; it under the terms of the GNU General Public License as published by
  6. ; the Free Software Foundation, either version 3 of the License, or
  7. ; (at your option) any later version.
  8. ;
  9. ; stage0 is distributed in the hope that it will be useful,
  10. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ; GNU General Public License for more details.
  13. ;
  14. ; You should have received a copy of the GNU General Public License
  15. ; along with stage0. If not, see <http://www.gnu.org/licenses/>.
  16. :start
  17. LOADUI R11 1 ; Our toggle
  18. LOADUI R13 0x600 ; Where we are starting our Stack
  19. ;; R14 will be storing our condition
  20. FALSE R15 ; Our holder
  21. ;; Prep TAPE_01
  22. LOADUI R0 0x1100
  23. FOPEN_WRITE
  24. ;; Prep TAPE_02
  25. LOADUI R0 0x1101
  26. FOPEN_WRITE
  27. :loop
  28. LOADUI R1 0 ; Read from tty
  29. FGETC ; Read a Char
  30. CMPSKIPI.NE R0 13 ; Replace all CR
  31. LOADUI R0 10 ; WIth LF
  32. FPUTC ; Display the Char to User
  33. ;; Check for Ctrl-D
  34. CMPSKIPI.NE R0 4
  35. JUMP @finish
  36. ;; Check for EOF
  37. JUMP.NP R0 @finish
  38. ;; Write out unprocessed byte
  39. LOADUI R1 0x1101 ; Write to TAPE_02
  40. FPUTC ; Print the Char
  41. ;; Convert byte to nybble
  42. CALLI R13 @hex ; Convert it
  43. ;; Get another byte if nonhex
  44. JUMP.NP R0 @loop ; Don't use nonhex chars
  45. ;; Deal with the case of second nybble
  46. JUMP.Z R11 @second_nybble ; Jump if toggled
  47. ;; Process first byte of pair
  48. ANDI R15 R0 0x0F ; Store First nibble
  49. FALSE R11 ; Flip the toggle
  50. JUMP @loop
  51. ;; Combined second nybble in pair with first
  52. :second_nybble
  53. SL0I R15 4 ; Shift our first nibble
  54. ANDI R0 R0 0x0F ; Mask out top
  55. ADD R0 R0 R15 ; Combine nibbles
  56. ;; Writeout and prepare for next cycle
  57. LOADI R11 1 ; Flip the toggle
  58. LOADUI R1 0x1100 ; Write the combined byte
  59. FPUTC ; To TAPE_01
  60. JUMP @loop ; Try to get more bytes
  61. :hex
  62. ;; Deal with line comments starting with #
  63. CMPUI R14 R0 35
  64. JUMP.E R14 @ascii_comment
  65. ;; Deal with line comments starting with ;
  66. CMPUI R14 R0 59
  67. JUMP.E R14 @ascii_comment
  68. ;; Deal with all ascii less than '0'
  69. CMPUI R14 R0 48
  70. JUMP.L R14 @ascii_other
  71. ;; Deal with '0'-'9'
  72. CMPUI R14 R0 57
  73. JUMP.LE R14 @ascii_num
  74. ;; Deal with all ascii less than 'A'
  75. CMPUI R14 R0 65
  76. JUMP.L R14 @ascii_other
  77. ;; Deal with 'A'-'F'
  78. CMPUI R14 R0 70
  79. JUMP.LE R14 @ascii_high
  80. ;; Deal with all ascii less than 'a'
  81. CMPUI R14 R0 97
  82. JUMP.L R14 @ascii_other
  83. ;; Deal with 'a'-'f'
  84. CMPUI R14 R0 102
  85. JUMP.LE R14 @ascii_low
  86. ;; Ignore the rest
  87. JUMP @ascii_other
  88. :ascii_num
  89. SUBUI R0 R0 48
  90. RET R13
  91. :ascii_low
  92. SUBUI R0 R0 87
  93. RET R13
  94. :ascii_high
  95. SUBUI R0 R0 55
  96. RET R13
  97. :ascii_other
  98. TRUE R0
  99. RET R13
  100. :ascii_comment
  101. LOADUI R1 0 ; Read from tty
  102. FGETC ; Read another char
  103. CMPSKIPI.NE R0 13 ; Replace all CR
  104. LOADUI R0 10 ; WIth LF
  105. FPUTC ; Let the user see it
  106. CMPUI R14 R0 10 ; Stop at the end of line
  107. LOADUI R1 0x1101 ; Write to TAPE_02
  108. FPUTC ; The char we just read
  109. JUMP.NE R14 @ascii_comment ; Otherwise keep looping
  110. JUMP @ascii_other
  111. :finish
  112. LOADUI R0 0x1100 ; Close TAPE_01
  113. FCLOSE
  114. LOADUI R0 0x1101 ; Close TAPE_02
  115. FCLOSE
  116. HALT