Mirror of CollapseOS
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

152 wiersze
2.9KB

  1. #include "user.inc"
  2. .org USER_CODE
  3. call parseLine
  4. ld b, 0
  5. ld c, a ; written bytes
  6. ret
  7. ; Sets Z is A is ' ', CR, LF, or null.
  8. isSep:
  9. cp ' '
  10. ret z
  11. cp 0
  12. ret z
  13. cp 0x0d
  14. ret z
  15. cp 0x0a
  16. ret
  17. ; read word in (HL) and put it in curWord, null terminated. A is the read
  18. ; length.
  19. readWord:
  20. push bc
  21. push de
  22. push hl
  23. ld de, curWord
  24. ld b, 4
  25. .loop:
  26. ld a, (hl)
  27. call isSep
  28. jr z, .success
  29. call JUMP_UPCASE
  30. ld (de), a
  31. inc hl
  32. inc de
  33. djnz .loop
  34. .success:
  35. xor a
  36. ld (de), a
  37. ld a, 4
  38. sub a, b
  39. jr .end
  40. .error:
  41. xor a
  42. ld (de), a
  43. .end:
  44. pop hl
  45. pop de
  46. pop bc
  47. ret
  48. ; Compare primary row at (DE) with string at curWord. Sets Z flag if there's a
  49. ; match, reset if not.
  50. matchPrimaryRow:
  51. push hl
  52. ld hl, curWord
  53. ld a, 4
  54. call JUMP_STRNCMP
  55. pop hl
  56. ret
  57. ; Parse line at (HL) and write resulting opcode(s) in (DE). Returns the number
  58. ; of bytes written in A.
  59. parseLine:
  60. call readWord
  61. push de
  62. ld de, instTBlPrimary
  63. .loop:
  64. ld a, (de)
  65. cp 0
  66. jr z, .nomatch ; we reached last entry
  67. call matchPrimaryRow
  68. jr z, .match
  69. ld a, 7
  70. call JUMP_ADDDE
  71. jr .loop
  72. .nomatch:
  73. xor a
  74. pop de
  75. ret
  76. .match:
  77. ld a, 6 ; upcode is on 7th byte
  78. call JUMP_ADDDE
  79. ld a, (de)
  80. pop de
  81. ld (de), a
  82. ld a, 1
  83. ret
  84. ; This is a list of primary instructions (single upcode) that lead to a
  85. ; constant (no group code to insert).
  86. ; That doesn't mean that they don't take any argument though. For example,
  87. ; "DEC IX" leads to a special upcode. These kind of constants are indicated
  88. ; as a single byte to save space. Meaning:
  89. ;
  90. ; All single char registers (A/B/C etc) -> themselves
  91. ; HL -> h
  92. ; (HL) -> l
  93. ; DE -> d
  94. ; (DE) -> e
  95. ; BC -> b
  96. ; (BC) -> c
  97. ; IX -> X
  98. ; (IX) -> x
  99. ; IY -> Y
  100. ; (IY) -> y
  101. ; AF -> a
  102. ; AF' -> f
  103. ; SP -> s
  104. ; (SP) -> p
  105. ; None -> 0
  106. ;
  107. ; This is a sorted list of "primary" (single byte) instructions along with
  108. ; metadata
  109. ; 4 bytes for the name (fill with zero)
  110. ; 1 byte for arg constant
  111. ; 1 byte for 2nd arg constant
  112. ; 1 byte for upcode
  113. instTBlPrimary:
  114. .db "ADD", 0, 'A', 'h', 0x86 ; ADD A, HL
  115. .db "CCF", 0, 0, 0, 0x3f ; CCF
  116. .db "CPL", 0, 0, 0, 0x2f ; CPL
  117. .db "DAA", 0, 0, 0, 0x27 ; DAA
  118. .db "DI",0,0, 0, 0, 0xf3 ; DI
  119. .db "EI",0,0, 0, 0, 0xfb ; EI
  120. .db "EX",0,0, 'p', 'h', 0xe3 ; EX (SP), HL
  121. .db "EX",0,0, 'a', 'f', 0x08 ; EX AF, AF'
  122. .db "EX",0,0, 'd', 'h', 0xeb ; EX DE, HL
  123. .db "EXX", 0, 0, 0, 0xd9 ; EXX
  124. .db "HALT", 0, 0, 0x76 ; HALT
  125. .db "INC", 0, 'l', 0, 0x34 ; INC (HL)
  126. .db "JP",0,0, 'l', 0, 0xe9 ; JP (HL)
  127. .db "LD",0,0, 'c', 'A', 0x02 ; LD (BC), A
  128. .db "LD",0,0, 'e', 'A', 0x12 ; LD (DE), A
  129. .db "LD",0,0, 'A', 'c', 0x0a ; LD A, (BC)
  130. .db "LD",0,0, 'A', 'e', 0x0a ; LD A, (DE)
  131. .db "LD",0,0, 's', 'h', 0x0a ; LD SP, HL
  132. .db "NOP", 0, 0, 0, 0x00 ; NOP
  133. .db "RET", 0, 0, 0, 0xc9 ; RET
  134. .db "RLA", 0, 0, 0, 0x17 ; RLA
  135. .db "RLCA", 0, 0, 0x07 ; RLCA
  136. .db "RRA", 0, 0, 0, 0x1f ; RRA
  137. .db "RRCA", 0, 0, 0x0f ; RRCA
  138. .db "SCF", 0, 0, 0, 0x37 ; SCF
  139. .db 0
  140. ; *** Variables ***
  141. ; enough space for 4 chars and a null
  142. curWord:
  143. .db 0, 0, 0, 0, 0