NovaYoshi

C machine

Sep 23rd, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.29 KB | None | 0 0
  1. Registers:
  2. SP (stack pointer): 16-bit
  3. FP (frame pointer): 16-bit
  4. PC (program counter): 16-bit
  5. A (left reg) 16-bit
  6. B (right reg) 16-bit
  7.  
  8. Addressing modes:
  9. <fast> = FP + item from lookup table
  10. <near> = FP + 8-bit sign extended offset
  11. <far> = FP + 16-bit offset
  12. <abs> = 16-bit address
  13.  
  14. Fast stack reference:
  15. Code | location | assembler name
  16. -----+----------+----------------
  17. 0 | frame-24 | local12
  18. 1 | frame-22 | local11
  19. 2 | frame-20 | local10
  20. 3 | frame-18 | local9
  21. 4 | frame-16 | local8
  22. 5 | frame-14 | local7
  23. 6 | frame-12 | local6
  24. 7 | frame-10 | local5
  25. 8 | frame-8 | local4
  26. 9 | frame-6 | local3
  27. A | frame-4 | local2
  28. B | frame-2 | local1
  29. | frame-0 | old block pointer
  30. | frame+2 | old program counter
  31. C | frame+4 | arg1
  32. D | frame+6 | arg2
  33. E | frame+8 | arg3
  34. F | frame+10 | arg4
  35.  
  36. Instruction set:
  37. 0x : lda fast : A = <fast>
  38. 1x : ldb fast : B = <fast>
  39. 2x : push fast : push <fast> to the stack
  40. 3x : sta fast : <fast> = A
  41. 4x : lda #x : A = #x
  42. 5x : ldb #x : B = #x
  43. 6x : push #x : push #x to the stack
  44. 7x : add #x : A += #x
  45. 80 xx : lda near : A = <near>
  46. 81 xx xx : lda far : A = <far>
  47. 82 xx : ldb near : B = <near>
  48. 83 xx xx : ldb far : B = <far>
  49. 84 xx : push near : push <near> to the stack
  50. 85 xx xx : push far : push <far> to the stack
  51. 86 xx : sta near : <near> = A
  52. 87 xx xx : sta far : <far> = A
  53. 88 xx : lda #xx : A = #xx
  54. 89 xx xx : lda #xxxx : A = #xxxx
  55. 8A xx : ldb #xx : B = #xx
  56. 8B xx xx : ldb #xxxx : B = #xxxx
  57. 8C xx : push #xx : push #xx to the stack
  58. 8D xx xx : push #xxxx : push #xxxx to the stack
  59. 8E xx : add #xx : A += #xx
  60. 8F xx xx : add #xxxx : A += #xxxx
  61.  
  62. 00-3f and 40-7f share logic except for sta/add
  63. 00-7f and 80-8f share logic
  64.  
  65. 90 xx xx : lda xxxx : A = <abs>
  66. 91 xx xx : ldb xxxx : B = <abs>
  67. 92 xx xx : push xxxx : push <abs> to the stack
  68. 93 xx xx : sta xxxx : <abs> = A
  69.  
  70. 94 xx xx : blda xxxx : A = <byte abs>
  71. 95 xx xx : bldb xxxx : B = <byte abs>
  72. 96 xx xx : bpush xxxx : push <byte abs> to the stack
  73. 97 xx xx : bsta xxxx : <byte abs> = A
  74.  
  75. 98 xx xx : blda far : A = <byte far>
  76. 99 xx xx : bldb far : B = <byte far>
  77. 9A xx xx : bpush far : push <byte far> to the stack
  78. 9B xx xx : bsta far : <byte far> = A
  79.  
  80. 9C xx xx : leaa far : A = &<far>
  81. 9D xx xx : leab far : B = &<far>
  82. 9E xx xx : pea far : push &<far> to the stack
  83. 9F xx xx : ?? far : reserved
  84.  
  85. A0 : deref : A = *A
  86. A1 : popstore : pop B, *b = A
  87. A2 : bderef : A = (byte)*A
  88. A3 : bpopstore : pop B, (byte)*B = A
  89. A4 : pha : push A to the stack
  90. A5 : plb : pop B from the stack
  91. A6 xx : unstack #xx : sp += #xx (for cleaning up after a function)
  92. A7 xx xx : unstack #xxxx : sp += #xxxx
  93. A8 xx xx : call xxxx : call an address (push PC, PC = xxxx, push FP, FP = SP)
  94. A9 : callptr : call A
  95. AA xxxxyy: call xxxx, #yy : call an address, decrease the stack pointer by yy
  96. AB yy : callptr #yy : call A, decrease the stack pointer by yy
  97. AC xx xx : copy #xxxx : memcpy(b, a, #xxxx)
  98. AD xx xx : clear #xxxx : memset(b, a, #xxxx) ? maybe it'd be better as memcpy without changing b
  99. AE : return : return (SP = FP, pop FP, pop PC)
  100. AF : nop : do nothing
  101. AF : swap : A = B and B = A
  102.  
  103. B0 : ucmplt : A = A < B (unsigned)
  104. B1 : ucmple : A = A <= B (unsigned)
  105. B2 : ucmpgt : A = A > B (unsigned)
  106. B3 : ucmpge : A = A >= B (unsigned)
  107. B4 : scmplt : A = A < B (signed)
  108. B5 : scmple : A = A <= B (signed)
  109. B6 : scmpgt : A = A > B (signed)
  110. B7 : scmpge : A = A >= B (signed)
  111. B8 : cmpeq : A = A == B
  112. B9 : cmpne : A = A != B
  113. BA : not : A = !A
  114. BB : neg : A = -A
  115. BC : compl : A = ~A
  116. BD : and : A = A & B
  117. BE : or : A = A | B
  118. BF : xor : A = A ^ B
  119. B0-B7 share logic
  120. entire block sets A
  121.  
  122. C0 : inc : A++
  123. C1 : dec : A--
  124. C2 : add : A += B
  125. C3 : sub : A -= B
  126. C4 : lshift : A <<= B (unsigned)
  127. C5 : double : A <<= 1
  128. C6 : rshift : A >>= B (unsigned)
  129. C7 : rshift : A >>= B (signed)
  130. C8 : mulu : A *= B (unsigned)
  131. C9 : muls : A *= B (signed)
  132. CA : divu : A /= B (unsigned)
  133. CB : divs : A /= B (signed)
  134. CC : modu : A %= B (unsigned)
  135. CD : mods : A %= B (signed)
  136. CE : :
  137. CF : :
  138. C0,C1 share logic with C2,C3
  139. signed and unsigned opcodes share logic
  140. entire block sets A
  141.  
  142. D0 xx : jumpt xx : PC += xx + 1 IF A==1
  143. D1 xx : jumpt xx : PC -= xx - 1 IF A==1
  144. D2 xx xx : jumpt xxxx : PC = <abs> IF A==1
  145. D3 : :
  146. D4 xx : jumpf xx : PC += xx + 1 IF A==0
  147. D5 xx : jumpf xx : PC -= xx - 1 IF A==0
  148. D6 xx xx : jumpf xxxx : PC = <abs> IF A==0
  149. D7 : :
  150. D8 xx : jump xx : PC += xx + 1
  151. D9 xx : jump xx : PC -= xx - 1
  152. DA xx xx : jump xxxx : PC = <abs>
  153. DB : :
  154. DC ?? : switchrange : switch statement with a range
  155. DD ?? : switchlist : switch statement with a list of values
  156. DE : :
  157. DF : :
  158.  
  159. -switch statements-
  160. switchrange
  161. .word 1, 5 ; range is 1 to 5
  162. .word default ; for if outside 1 to 5
  163. .word was1
  164. .word was2
  165. .word was3
  166. .word was4
  167. .word was5
  168.  
  169. switchlist
  170. .word 5 ; 5 cases
  171. .word 1, was1
  172. .word 2, was2
  173. .word 3, was3
  174. .word 4, was4
  175. .word 5, was5
  176. .word default
  177.  
  178. -array reading-
  179. lda index
  180. add #array_address
  181. deref
  182.  
  183. -array writing-
  184. lda index
  185. add #array_address
  186. pha
  187. lda (whatever value to write)
  188. popstore
  189.  
  190. -loop-
  191. lda #0
  192. sta index
  193. loop:
  194. ; get place to write
  195. lda #array
  196. ldb index
  197. add
  198. pha
  199.  
  200. ; get index*index
  201. lda index
  202. ldb index
  203. mulu
  204. popstore
  205.  
  206. lda index
  207. ldb #10
  208. cmpne
  209. jumpt loop
  210.  
  211. -calling functions-
  212. push #1
  213. push #2
  214. push #3
  215. push #4
  216. call sum
  217. unstack #4*2
  218.  
  219. sum:
  220. lda local1
  221. ldb local2
  222. add
  223. ldb local3
  224. add
  225. ldb local4
  226. add
  227. return
  228.  
  229.  
  230. -passing -
  231. pea variable
  232. call function
  233. unstack #1*2
  234.  
  235. function: ; writes 1 to the passed argument
  236. push arg1
  237. lda #1
  238. popstore
  239. return
Add Comment
Please, Sign In to add comment