risc8.asm 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. %define COM_UARTF 0x03
  2. %define COM_UARTW 0x05
  3. %define COM_UARTR 0x04
  4. %define COM_LED 0x06
  5. %define COM_DIP 0x08
  6. %define COM_UARTIN 0x09
  7. %define NULL 0x000000
  8. %define LF 0x0a
  9. %define CR 0x0d
  10. %define LFCR 0x0a0d
  11. %define COM_UART_TRANS 0b0000_0001
  12. %define COM_UART_RECV 0b0000_0010
  13. %define COM_UART_ECHO 0b0000_0100
  14. %define COM_UART_RR 0b0000_1000 ; Read Ready
  15. section .data 2x3x8192
  16. intro_text DB 0x1B63,0x1B5B324A,0x1B5B34303B33313B316D,"GLaDOS v0.0",0x1B5B303B33323B34306D," starting..",LFCR,0
  17. eq1_text DB LFCR,"1) 0x1194 * 0x2710 = 0x",0
  18. eq2_text DB LFCR,"2) 3*2=",0
  19. eq3_text DB LFCR,"3) 3*2=",0
  20. initial_vt100 DB 0x1B,'c',0x1B,"[2J",0x1B,"[40;32m",0
  21. error_text DB LFCR,"Invalid operation: ",0
  22. error0 DB "Buffer is full",0
  23. mul16buf DBE 8
  24. termHiFlag DB 0x0000
  25. termBuff DB 10,0
  26. DBE 10
  27. mallocPointer DB 0
  28. section .text 1x2x4096
  29. setup
  30. ; Run init
  31. INTRE interrupt
  32. ;CALL read_char
  33. ;CALL print_char
  34. ;.echo
  35. ; JUMP .echo
  36. ; CALL read_char
  37. ; CALL print_char
  38. CPY1 intro_text@0
  39. CPY2 intro_text@1
  40. CALL print_msg
  41. ;CALL read_char
  42. ;CPY1 COM_UART_ECHO
  43. ;COM r1,COM_UARTF
  44. CPY1 eq1_text@0
  45. CPY2 eq1_text@1
  46. CALL print_msg
  47. CPY0 0x11 ; 4500
  48. CPY1 0x94
  49. CPY2 0x27 ; 10000
  50. CPY3 0x10
  51. CALL mulU16
  52. CALL printhex
  53. MOVE r0,r1
  54. CALL printhex
  55. MOVE r0,r2
  56. CALL printhex
  57. MOVE r0,r3
  58. CALL printhex
  59. CALL println
  60. loop
  61. CPY0 0
  62. PUSH r0
  63. CPY0 '>'
  64. CALL print_char
  65. CPY0 0x20 ; space
  66. CALL print_char
  67. ;.loop
  68. ; CALL read_char
  69. ; CALL print_char
  70. ; ;BEQ r2,0x0a,.start
  71. ; BEQ r0,0x0d,.error
  72. ; JUMP .loop
  73. ;.error
  74. ; CPY1 error_text@0
  75. ; CPY2 error_text@1
  76. ; CALL print_msg
  77. ; JUMP .start
  78. .read
  79. CALL read_char
  80. BGT r0,57,.readc0
  81. BGT r0,48,.readadd
  82. .readc0
  83. BEQ r0,0x0d,.done
  84. ;BEQ r0,0x20,.echo
  85. BEQ r0,0x08,.backspace
  86. BEQ r0,0x7F,.backspace
  87. BEQ r0,0x2A,.readadd ; *
  88. BEQ r0,0x2B,.readadd ; +
  89. BEQ r0,0x2D,.readadd ; -
  90. BEQ r0,0x2F,.readadd ; /
  91. JUMP .read
  92. .backspace
  93. ; POP r1
  94. ; BEQ r1,0,.backspaceCheck
  95. ; CPY0 0x1B
  96. ; CALL print_char
  97. ; CPY0 'c'
  98. ; CALL print_char
  99. ; JUMP .read
  100. ;.backspaceCheck
  101. ; PUSH r1
  102. JUMP .read
  103. .readadd
  104. LWLO r3,termHiFlag
  105. BZ r3,.readadd0
  106. JUMP .readstore
  107. .readadd0
  108. CPY3 1
  109. SWHI r0
  110. SWLO r3,termHiFlag
  111. JUMP .echo
  112. .readstore
  113. CPY2 0
  114. LWHI r3,termHiFlag
  115. SWLO r2,termHiFlag
  116. MOVE r2,r0
  117. CPY0 termBuff@0
  118. CPY1 termBuff@1
  119. CALL arrayPush
  120. ; Check if buffer overflow
  121. BZ r0,.readstore0
  122. MOVE r0,r2
  123. JUMP .echo
  124. .readstore0
  125. MOVE r0,r2
  126. BZ r1,.error0
  127. .echo
  128. CALL print_char
  129. JUMP .read
  130. .done
  131. ; if odd number of chars, push r0, 0 to array
  132. LWLO r3,termHiFlag
  133. BZ r3,.doneC
  134. ;LWHI r2,termHiFlag
  135. ;CPY3 0
  136. ;CPY0 termBuff@0
  137. ;CPY1 termBuff@1
  138. ;CALL arrayPush
  139. .doneC
  140. CPY1 termBuff@0
  141. CPY2 termBuff@1
  142. CALL Process
  143. CPY0 termBuff@0
  144. CPY1 termBuff@1
  145. CALL arrayClear
  146. JUMP loop
  147. .done0
  148. POP r0
  149. BEQ r0,0,.done1
  150. CALL print_char
  151. JUMP .done0
  152. .done1
  153. CALL println
  154. JUMP loop
  155. .error0
  156. CALL println
  157. CPY1 error0@0
  158. CPY2 error0@1
  159. CALL print_msg
  160. CALL println
  161. JUMP loop
  162. MOVE r1,r0
  163. SUBI r1,48
  164. CALL print_char
  165. CALL read_char
  166. MOVE r2,r0
  167. CALL print_char
  168. CALL read_char
  169. MOVE r3,r0
  170. SUBI r3,48
  171. CALL print_char
  172. BEQ r2,'+',.add
  173. BEQ r2,'-',.sub
  174. JUMP .invalid
  175. .add
  176. ADD r1,r3
  177. JUMP .result
  178. .sub
  179. SUB r1,r3
  180. JUMP .result
  181. .invalid
  182. CPY1 error_text@0
  183. CPY2 error_text@1
  184. CALL print_msg
  185. JUMP .done
  186. .result
  187. PUSH r1
  188. CPY0 '='
  189. CALL print_char
  190. POP r0
  191. ADDI r0,48
  192. CALL print_char
  193. Process
  194. ; Input array *{r2 r1}
  195. ;CPY1 termBuff@0
  196. ;CPY2 termBuff@1
  197. .readnext
  198. INC r1
  199. ADDC r2
  200. CI1 r2
  201. CI0 r1
  202. SWLO r0,NULL
  203. .isdigit
  204. ;BGT r0,57,.issymbol
  205. ;BGT r0,48,.digit
  206. .issymbol
  207. ;JUMP .error
  208. .digit
  209. ;CI1 r2
  210. ;CI0 r1
  211. ;SWHI r0
  212. .error
  213. CPY1 error_text@0
  214. CPY2 error_text@1
  215. CALL print_msg
  216. CPY1 termBuff@0
  217. CPY2 termBuff@1
  218. INC r1
  219. ADDC r2
  220. CALL print_msg
  221. .end
  222. CALL println
  223. RET
  224. printbin
  225. ; print r0 as binary
  226. PUSH r2
  227. PUSH r1
  228. CPY2 0b1000_0000
  229. MOVE r1,r0
  230. .start
  231. AND r0,r2
  232. BZ r0,.print0
  233. .print1
  234. CPY0 '1'
  235. CALL print_char
  236. JUMP .end
  237. .print0
  238. CPY0 '0'
  239. CALL print_char
  240. .end
  241. SRL r2,1
  242. BZ r2,.done
  243. MOVE r0,r1
  244. JUMP .start
  245. .done
  246. POP r1
  247. POP r2
  248. RET
  249. printhex
  250. ; print r0 as hex
  251. PUSH r0
  252. PUSH r1
  253. MOVE r1,r0
  254. SRL r0,4
  255. BGT r0,9,.p0
  256. ADDI r0,48
  257. JUMP .p1
  258. .p0
  259. ADDI r0,55
  260. .p1
  261. CALL print_char
  262. MOVE r0,r1
  263. POP r1
  264. ANDI r0,0b0000_1111
  265. BGT r0,9,.p2
  266. ADDI r0,48
  267. JUMP .p3
  268. .p2
  269. ADDI r0,55
  270. .p3
  271. CALL print_char
  272. POP r0
  273. RET
  274. ;.done
  275. ;; Equation 1
  276. ;CPY1 eq1_text@0
  277. ;CPY2 eq1_text@1
  278. ;CALL print_msg
  279. ;CPY0 3
  280. ;ADDI r0,2
  281. ;ADDI r0,48 ; Convert to ascii
  282. ;CALL print_char
  283. ;
  284. ;; Equation 2
  285. ;CPY1 eq2_text@0
  286. ;CPY2 eq2_text@1
  287. ;CALL print_msg
  288. ;CPY0 6
  289. ;SUBI r0,2
  290. ;ADDI r0,48 ; Convert to ascii
  291. ;CALL print_char
  292. ;; Equation 3
  293. ;CPY1 eq3_text@0
  294. ;CPY2 eq3_text@1
  295. ;CALL print_msg
  296. ;CPY0 3
  297. ;CPY1 2
  298. ;MUL r0,r1
  299. ;ADDI r0,48 ; Convert to ascii
  300. ;CALL print_char
  301. ;
  302. ;CPY1 eq3x_text@0
  303. ;CPY2 eq3x_text@1
  304. ;CALL print_msg
  305. ;GETAH r0
  306. ;ADDI r0,48 ; Convert to ascii
  307. ;CALL print_char
  308. ;; Equation 4
  309. ;CPY1 eq4_text@0
  310. ;CPY2 eq4_text@1
  311. ;CALL print_msg
  312. ;CPY0 9
  313. ;CPY1 2
  314. ;DIV r0,r1
  315. ;ADDI r0,48 ; Convert to ascii
  316. ;CALL print_char
  317. ;; Equation 5
  318. ;CPY1 eq5_text@0
  319. ;CPY2 eq5_text@1
  320. ;CALL print_msg
  321. ;GETAH r0
  322. ;ADDI r0,48 ; Convert to ascii
  323. ;CALL print_char
  324. ;CALL println
  325. ;JUMP .start
  326. ;CPY3 0
  327. start
  328. INC r3
  329. JUMP start
  330. interrupt
  331. PUSH r0
  332. GETIF r0
  333. POP r0
  334. RETI
  335. println
  336. PUSH r0
  337. CPY0 LF
  338. CALL print_char
  339. CPY0 CR
  340. CALL print_char
  341. POP r0
  342. RET
  343. mulU16
  344. ; Multiply 2 unsigned 16-bit int
  345. ; {r0 r1} * {r2 r3}
  346. ; A B * X Y
  347. ; Result:
  348. ; r3 = BY0
  349. ; r2 = BY1+BX0+AY0
  350. ; r1 = BX1+AY1+AX0
  351. ; r0 = AX1
  352. ; Carryout must be included to higher bytes
  353. PUSH r3
  354. MUL r3,r1 ; BY0
  355. SWHI r3
  356. GETAH r3 ; BY1
  357. SWLO r3,mul16buf
  358. POP r3 ; Buffer = [BY0 BY1]
  359. PUSH r2
  360. MUL r2,r1 ; BX0
  361. SWHI r2
  362. GETAH r2 ; BX1
  363. SWLO r2,mul16buf+1
  364. POP r2 ; Buffer = [BY0 BY1 BX0 BX1]
  365. PUSH r3
  366. MUL r3,r0 ; AY0
  367. SWHI r3
  368. GETAH r3 ; AY1
  369. SWLO r3,mul16buf+2
  370. POP r3 ; Buffer = [BY0 BY1 BX0 BX1 AY0 AY1]
  371. PUSH r2
  372. MUL r2,r0 ; AX0
  373. SWHI r2
  374. GETAH r2 ; AX1
  375. SWLO r2,mul16buf+3
  376. POP r2 ; Buffer = [BY0 BY1 BX0 BX1 AY0 AY1 AX0 AX1]
  377. ; { 0 } { 1 } { 2 } { 3 }
  378. ; r3 will be used as spare register as it's equal to BY0
  379. LWLO r2,mul16buf ; r2=BY1
  380. LWLO r1,mul16buf+1 ; r1=BX1
  381. LWLO r0,mul16buf+3 ; r0=AX1
  382. LWHI r3,mul16buf+1 ; t=BX0
  383. ADD r2,r3
  384. ADDC r1
  385. ADDC r0
  386. LWHI r3,mul16buf+2 ; t=AY0
  387. ADD r2,r3
  388. ADDC r1
  389. ADDC r0
  390. LWLO r3,mul16buf+2 ; t=AY1
  391. ADD r1,r3
  392. ADDC r0
  393. LWHI r3,mul16buf+3 ; t=AX0
  394. ADD r1,r3
  395. ADDC r0
  396. LWHI r3,mul16buf ; r3=BY0
  397. RET
  398. printU8
  399. ; Assuing number is 128
  400. ; a = 128%10 = 8
  401. ; a = 128%100 - a // 10 = 2
  402. ; a = 128%1000 - a // 100 = 1
  403. ; in case of 8bit number quicker is to do 2 if statements for 100s
  404. ; input argument is in r0
  405. PUSH r0
  406. PUSH r1
  407. PUSH r2
  408. PUSH r3
  409. CPY2 10
  410. MOVE r1,r0
  411. DIV r1,r2
  412. GETAH r1
  413. PUSH r1 ; Stored last digit
  414. BGE r0,10,.ge10
  415. JUMP .p3
  416. .ge10
  417. CPY2 100
  418. MOVE r3,r0
  419. DIV r3,r2
  420. GETAH r3
  421. SUB r3,r1
  422. CPY2 10
  423. DIV r3,r2
  424. PUSH r3 ; Stored middle digit
  425. BGE r0,100,.ge100
  426. JUMP .p2
  427. .ge100
  428. CPY2 200
  429. CI2 r2
  430. BGE r0,0,.s2
  431. CPY2 100
  432. CI2 r2
  433. BGE r0,0,.s1
  434. JUMP .p2
  435. .s1 CPY0 '1'
  436. JUMP .p0
  437. .s2 CPY0 '2'
  438. .p0 CALL print_char
  439. .p2
  440. POP r0
  441. ADDI r0,48
  442. CALL print_char
  443. .p3
  444. POP r0
  445. ADDI r0,48
  446. CALL print_char
  447. POP r3
  448. POP r2
  449. POP r1
  450. POP r0
  451. RET
  452. arrayClear
  453. ; Clear array at *{r1 r0}
  454. PUSH r2
  455. CI1 r1
  456. CI0 r0
  457. LWHI r2,NULL
  458. SWHI r2
  459. CPY2 0
  460. CI1 r1
  461. CI0 r0
  462. SWLO r2,NULL
  463. POP r2
  464. RET
  465. arrayPush
  466. ; Push to array *{r1 r0} value {r3 r2}
  467. ; If full, changes r1 r0 to 0x0000
  468. PUSH r2
  469. PUSH r3
  470. CI1 r1
  471. CI0 r0
  472. LWLO r2,NULL
  473. CI1 r1
  474. CI0 r0
  475. LWHI r3,NULL ; Stores cap
  476. ; r2=size, r3=cap
  477. CI2 r3
  478. BGE r2,0xFF,.full
  479. INC r2
  480. SWHI r3
  481. CI1 r1
  482. CI0 r0
  483. SWLO r2,NULL
  484. ADD r0,r2
  485. ADDC r1
  486. POP r3
  487. SWHI r3
  488. POP r2
  489. CI1 r1
  490. CI0 r0
  491. SWLO r2,NULL
  492. RET
  493. .full
  494. POP r3
  495. POP r2
  496. CPY0 0
  497. CPY1 0
  498. RET
  499. arrayPop
  500. ; Pop from array *{r1 r0} value {r3 r2}
  501. ; If empty, changes all regs to 0x00
  502. CI1 r1
  503. CI0 r0
  504. LWLO r2,NULL
  505. BEQ r2,0,.empty
  506. DEC r2
  507. CI1 r1
  508. CI0 r0
  509. SWLO r2,NULL
  510. INC r2
  511. ADD r0,r2
  512. ADDC r1
  513. CI1 r1
  514. CI0 r0
  515. LWHI r3,NULL
  516. CI1 r1
  517. CI0 r0
  518. LWLO r2,NULL
  519. RET
  520. .empty
  521. CPY3 0
  522. CPY2 0
  523. CPY1 0
  524. CPY0 0
  525. RET
  526. read_char ; read char to r0
  527. COM r0,COM_UARTR
  528. ANDI r0,0b0000_1000
  529. BZ r0,read_char
  530. COM r0,COM_UARTIN
  531. RET
  532. malloc ; returns memory free memory locaion
  533. ; r0 = size to allocate
  534. ; {r2, r1} memory pointer
  535. LWHI r2,mallocPointer
  536. LWLO r1,mallocPointer
  537. PUSH r2
  538. PUSH r1
  539. ADD r1,r0
  540. ADDC r2
  541. SWHI r2
  542. SWLO r1,mallocPointer
  543. POP r1
  544. POP r2
  545. RET
  546. print_msg ; print value in mem pinter {r2 r1}
  547. PUSH r0
  548. .loop
  549. CI1 r2
  550. CI0 r1
  551. LWHI r0,NULL
  552. BZ r0,.end
  553. CALL print_char
  554. CI1 r2
  555. CI0 r1
  556. LWLO r0,NULL
  557. BZ r0,.end
  558. CALL print_char
  559. INC r1
  560. ADDC r2
  561. JUMP .loop
  562. .end
  563. POP r0
  564. RET
  565. print_char ; print value in r0
  566. PUSH r0
  567. .loop
  568. COM r0,COM_UARTR
  569. ANDI r0,0b0000_0010
  570. XORI r0,0b0000_0010 ; invert
  571. BZ r0,.loop
  572. POP r0
  573. COM r0,COM_UARTW
  574. RET