Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.41 KB | None | 0 0
  1. .globl mecall
  2. .globl handler
  3.  
  4.  
  5. handler:
  6. addi sp sp, -16 #Create stack space #@ NEW, MIGHT NEED POP STACK CAUSE MECALL WILL RUN AFTER HANDLER,
  7. #@ and i think this ceate stack space twice
  8. #and we need to pop stack before creating space again
  9. sw ra, 12(sp) #Preserve caller's return address
  10. sw t0, 8(sp)
  11.  
  12.  
  13. csrrw s1, 66, zero #Load ucause into s1
  14.  
  15. la s10, ExceptionTbl #Load address exceptions
  16. addi s9, zero, 4
  17. mul s1, s1, s9 #Mult ucause # by by 4 to get offset
  18. add s10, s10, s1 #Increment subroutine by offset
  19. lw, s8, (s10) #Load word at address s10 (exceptiontbl + offset)
  20. jalr s8
  21.  
  22.  
  23.  
  24.  
  25. mecall: #a7 contains num to indicate which subroutine to use
  26.  
  27.  
  28. mv a7, a6
  29.  
  30.  
  31. #USE CALLSTACK INSTEAD
  32. addi sp sp, -16 #Create stack space
  33. sw ra, 12(sp) #Preserve caller's return address
  34. sw t0, 8(sp) #@ ADDED B/C PLANCK USES T0 IN MAIN
  35. #sw t1, 52(sp)
  36. #sw t2, 48(sp)
  37. #sw t3, 44(sp)
  38. #sw t4, 40(sp)
  39. #sw t5, 36(sp)
  40. #sw t6, 32(sp)
  41. #sw a0, 28(sp)
  42. #sw a1, 24(sp)
  43. #sw a2, 20(sp)
  44. #sw a3, 16(sp)
  45. #sw a4, 12(sp)
  46. #sw a5 8(sp)
  47. #sw a6, 4(sp)
  48. #sw a7, (sp)
  49.  
  50. #sw s0, 8(sp) #Preserve frame pointer (Dont think i need this
  51. #sw s1, 36(sp) #Preserve s1, (do i need this?)
  52. #addi s0, sp, 12 #Set frame pointer (do i need this?)
  53.  
  54.  
  55. #JUMP TO VECTOR TABLE
  56.  
  57.  
  58. #FOR UCAUSE 0 AND 1, DONT NEED TO INCREMENT EPC BY 4 BECAUSE JUMP AUTOMATICALLY DOES THIS?
  59.  
  60.  
  61.  
  62.  
  63. ENVCALL:
  64. #Identify subroutine. RUN THIS IF UCAUSE IS 8
  65. la s10 Subroutines #Load address of Subroutines into x18
  66. mv s11, a7 #load a7 into other reg so we don't mess it up
  67. addi s9, zero, 4
  68. mul s11, s11, s9 #multiply s11 by 4 to get offset
  69. add s10, s10, s11 #Increment Subroutine address by offset
  70. lw s8, (s10) #load word at address s10
  71. jalr s8
  72.  
  73.  
  74. #@ LOOK AT BIT 31 TO SEE IF CAUSED BY INT OR EXC.
  75.  
  76.  
  77. PrintStringHelper: #Same as PrintChar but returns to PrintString instead of returning to inttest
  78. li s6, 1 #holds immediate val 1
  79. lw s2, TCR #load value of word at label TCR (which is actual address of TCR) into s2
  80. lw s1, (s2) #loads value from TCR into s1
  81. bne s1, s6, PrintChar #loop if TCR not ready to display
  82. lw s5, TDR #Load address TDR into t0
  83. sw a0(s5) #Store a0 to TDR #THIS LINE IS BREAKINNG THINGS. A0
  84.  
  85. ret
  86.  
  87. PrintChar:
  88. li s6, 1 #holds immediate val 1
  89. lw s2, TCR #load value of word at label TCR (which is actual address of TCR) into s2
  90. lw s1, (s2) #loads value from TCR into s1
  91. bne s1, s6, PrintChar #loop if TCR not ready to display
  92. lw s5, TDR #Load address TDR
  93. sw a0(s5) #Store a0 to TDR
  94.  
  95. b teardown #Return to inttest
  96.  
  97.  
  98.  
  99. ReadInt:
  100. lw s0, RCR #Load RCR address
  101. lw s1, (s0) #Load value of RCR
  102. beqz s1, ReadInt #Branch if keyboard has not entered character
  103. lw s2, RDR #load address of RDR
  104. lb a0, (s2) #Load value RDR into a0
  105. addi a0, a0 -48 #convert to decimal from hex
  106.  
  107. ret
  108.  
  109. ReadChar:
  110. lw s0, RCR #Load RCR address
  111. lw s1, (s0) #Load RCR value
  112. beqz s1, ReadChar #branch if keyboard has not received char
  113. lw s2, RDR #load address of RDR
  114. lb a0, (s2) #Load value of RDR
  115.  
  116. b teardown
  117.  
  118. #Only s0-s11. These are preserved across calls and (And I don't use them in quiz.asm)
  119. #If I used registers that aren't preserved (a0-17, t0-t6), store the values of these registers into memory using sw (the values they contain from quiz.asm)
  120. #Then use them as needed in mecall file
  121. #Finally load the values back into register before returning to quiz.asm
  122.  
  123. ReadString:
  124.  
  125. lw s1, enter #load x31 with enter key
  126. li s8, 1 #Start counter at 1
  127. li s9, 20
  128.  
  129. ReceiverVerify: #x28 holds immediate 1
  130. #bgt s8, a1, EnterKeyPressed #branch if counter > max # of chars read #WHY??
  131. #beq s8, a1, NullChar #if max num? #DONT THINK I NEED THIS, JUST BRANCH WHEN ENTER KEY PRESSED
  132. lw s3, RCR #load address RCR
  133. lw s4, (s3) #load value RCR into s4 (RCR is 1 when keyboard receives new char)
  134. bnez s4, CharTyped #b if RCR is 1 (keyboard has received char)
  135.  
  136. #@ The Ready bit is automatically reset to 0 when the program reads the Receiver Data using an 'lw' instruction.
  137.  
  138.  
  139.  
  140. b ReceiverVerify #if keyboard has not received new char, loop
  141.  
  142.  
  143. CharTyped:
  144. lw s5, RDR #load address RDR
  145. lw s6, (s5) #load value RDR (last character typed), this resets RCR back o 0
  146.  
  147. beq s6, s1, EnterKeyPressed
  148.  
  149. sb s6, (a0) #Store letter into space for string #@ PRETTY SURE I SHOULD BE STORING IN A0, SHOULD ONLY STORE WHEN NEW CHAR TYPED
  150.  
  151. addi a0, a0, 1 #Increment string space pointer
  152.  
  153. addi s8, s8, 1 #Increment total counter
  154.  
  155. b ReceiverVerify
  156.  
  157. NullChar:
  158. li s6, 0
  159. sb s6(s7) #Store NullChar
  160.  
  161. EnterKeyPressed: #end str
  162. b teardown
  163.  
  164.  
  165.  
  166. Exit:
  167. la a0, ExitMessage #Use Trap 4 (PrintString) to print Exit Message
  168. li a6, 4
  169. jal mecall
  170. ret
  171.  
  172.  
  173. Undefined:
  174. la a0, Error #Use Trap 4 (PrintString) to print Error Message
  175. li a6, 4
  176. jal mecall
  177. li a7, 10 #Use exit routin
  178. ecall
  179.  
  180.  
  181. PrintString:
  182. #la a0, test_str #This is for testing. For asgn8, a str is already loaded into a0
  183. li a7, 4
  184. #jal mecall
  185.  
  186. mv t1, a0
  187.  
  188. printstrloop:
  189. lb a0, (t1) #AFTER THIS STEP DO I OUTPUT TO TDR?
  190.  
  191. beq zero, a0, teardown #Works successfuly, branches at end of string #@ !!! maybe i need to change this to add new line
  192. jal PrintStringHelper #previously, this jumped to PrintChar
  193. addi t1, t1, 1 #Get next char into t1 PROBLEM: After nullchar, execution resumes here
  194. b printstrloop
  195.  
  196. #addnewline:
  197. #li a0, 0x0A #Putnewchar in a0
  198. #lw s5, TDR #Load address TDR
  199. #sw a0(s5) #Store a0
  200.  
  201. #UCAUSE 0
  202. IAM:
  203. la a0, IAM_MSG
  204. li a6, 4 #PrintString
  205. #COPY RA INTO EPC
  206. lw ra, 12(sp)
  207. addi ra, ra, -4 #Decrement RA because teardown increments it
  208. csrrw zero, 65, ra #Load RA into EPC
  209. jal mecall
  210.  
  211.  
  212. IAF:
  213. la a0, IAF_MSG
  214. li a6, 4 #PrintString
  215. lw ra, 12(sp)
  216. addi ra, ra, -4 #Decrement RA because teardown increments it
  217. csrrw zero, 65, ra #Load RA into EPC
  218. jal mecall
  219.  
  220. LAM:
  221. ebreak
  222. la a0, LAM_MSG1
  223. li a6, 4 #PrintString
  224. #lw ra, 12(sp)
  225. #addi ra, ra, -4 #Decrement RA because teardown increments it
  226. #csrrw zero, 65, ra #Load RA into EPC
  227. addi a5, zero, 1 #***STORE 1 IN A5*** IMPORTANT FOR TEARDOWN!!!
  228. jal mecall #need to return here. this add saved when i JAL
  229. csrrw a0, 65, zero #store epc in a0
  230. b hex_init
  231. #2nd address in utval (67)
  232.  
  233. #t3 = s8
  234.  
  235. hex_init:
  236. addi sp, sp, -32
  237. sw ra, 28(sp)
  238. sw t0, 24(sp) #@ ADDED B/C PLANCK USES T0 IN MAIN
  239. sw t1, 20(sp)
  240. sw t2, 16(sp)
  241. sw t3, 12(sp)
  242. sw t4, 8(sp)
  243. sw t5, 4(sp)
  244. sw t6, (sp)
  245.  
  246. la t0, hex_buffer
  247. li t1, 0
  248. li t2, 28
  249. li t3, 32
  250.  
  251. #mine - hers
  252. #t1 - t3
  253. #t2 - t4
  254. #t3 - t5
  255. #t4 - t1
  256. #t5 - t2
  257. printhex:
  258. ebreak
  259. andi t4, t4, 0 #clear t4
  260. sll t4, a0, t1 #store epc in t4
  261. srl, t4, t4, t2 #Shift right 28 bits
  262.  
  263. li t5, 9 #String converter
  264. ble t4, t5, inc_epc
  265. addi t4, t4, 87
  266. b store_byte
  267.  
  268. inc_epc:
  269. addi t4, t4, 48
  270.  
  271. store_byte:
  272. sb t4, (t0) #store into buffer
  273. addi t0, t0, 1 #Point to next byte
  274. addi t1, t1, 4
  275. bne t1, t3, printhex #print another char if we havent reached 32nd bit
  276. ebreak #HERE I NEED TO B BRACK TO LAMV
  277.  
  278. print_epc:
  279. la a0, hex_buffer
  280. li a6, 4 #load printstr
  281. addi a5, zero, 1 #***STORE 1 IN A5*** IMPORTANT FOR TEARDOWN!!!
  282. jal mecall
  283. LAM2:
  284.  
  285. la a0, LAM_MSG2
  286. li a6, 4 #PrintString
  287. addi a5, zero, 1 #***STORE 1 IN A5*** IMPORTANT FOR TEARDOWN!!
  288. jal mecall
  289.  
  290. csrrw a0, 67, zero #store utval in a0
  291. b hex_init
  292.  
  293.  
  294.  
  295. LAF:
  296. addi x0, x0, 4
  297. b LAF
  298.  
  299. SAM:
  300. addi x0, x0, 5
  301. b SAM
  302.  
  303. SAF:
  304.  
  305. BAD:
  306.  
  307. None:
  308.  
  309.  
  310.  
  311.  
  312. teardown:
  313. lw ra, 12(sp) #Retrieve RA
  314. lw t0, 8(sp) #Retrieve t0
  315. #addi sp, sp, 16 #Pop current stack frame
  316. #handler: # Just ignore it by moving epc (65) to the next instruction
  317. #Add 4 to epc, so uret returns to line AFTER ecall
  318. #lw t1, 52(sp)
  319. #lw t2, 48(sp)
  320. #lw t3, 44(sp)
  321. #lw t4, 40(sp)
  322. #lw t5, 36(sp)
  323. #lw t6, 32(sp)
  324. #lw a0, 28(sp)
  325. #lw a1, 24(sp)
  326. #lw a2, 20(sp)
  327. #lw a3, 16(sp)
  328. #lw a4, 12(sp)
  329. #lw a5, 8(sp)
  330. #lw a6, 4(sp)
  331. #lw a7, (sp)
  332. csrrw t6, 65, zero
  333. addi t6, t6, 4
  334. csrrw zero, 65, t6
  335. addi a7, zero 0 #Clear a7 ecall won't jump to mecall if a7 contains something
  336. #ebreak
  337.  
  338. bgt a5, zero, a5_triggered #IF a5 is 1, store ra in epc (LAM)
  339. uret #Returns from handling an interrupt, used instead of jumping to uepc (the user-level exception program counter)
  340.  
  341. a5_triggered:
  342. csrrw zero, 65, ra
  343. andi a5, a5, 0 #Clear a5
  344. uret
  345.  
  346.  
  347.  
  348. #8 is ID of ecall exception. ucause should be 8?
  349. #lw zero, 0
  350. # trigger trap for load access fault? #@ What does this mean? Dont need this. it causes exception
  351.  
  352. #To add external device interrupts. The “User External Interrupt Enable” (UEIP) bit must be set in the mie/uie register (CSR 4).
  353. #csrrsi zero, 4, 1
  354.  
  355.  
  356. #Set scratch reg to starting address of system stack?
  357.  
  358. #leave a7 as 0, load 4 into a6
  359.  
  360. .data #Use num in a7 as index to table
  361. test_str:
  362. .string "hi" #printstring called"
  363.  
  364. Subroutines:
  365. .word Undefined Undefined Undefined Undefined PrintString ReadInt Undefined Undefined ReadString Undefined Exit PrintChar ReadChar
  366.  
  367.  
  368. Error:
  369. .string "invalid or unimplemented syscall service"
  370.  
  371. ExitMessage:
  372. .string "-- program is finished running --"
  373.  
  374.  
  375. enter:
  376. .word 0x0A #Holds enter key
  377.  
  378. ReadStringBuffer:
  379. .space 20
  380.  
  381. TDR: #Transmitter Data Register - Characters written here will be displayed on screen
  382. .word 0xffff000c
  383.  
  384. TCR: #Transmitter Control Register - set to 1 when device ready to display character on screen
  385. .word 0xffff0008
  386.  
  387. RDR: #Receiver Data Register - Characters typed are stored here
  388. .word 0xffff0004
  389.  
  390. RCR: #Receiver Control Register - Bit [0] is one when keyboard has received a new character.
  391. .word 0xffff0000
  392.  
  393.  
  394. #IndexTbl:
  395. #.word 0, 1, 4, 5, 6, 7, 8, -1 #WHY -1?
  396.  
  397. ExceptionTbl:
  398. .word IAM IAF None None LAM LAF SAM SAF ENVCALL BAD #WHY BAD?
  399.  
  400. IAM_MSG:
  401. .string "Error in : Instruction load alignment error\n"
  402.  
  403. IAF_MSG:
  404. .string "Error in : Instruction load access error\n"
  405.  
  406. None_MSG:
  407. .string "?"
  408.  
  409. LAM_MSG1:
  410. .string "Runtime exception at "
  411.  
  412. LAM_MSG2:
  413. .string ": Load address not aligned to word boundary "
  414.  
  415. LAF_MSG1:
  416. .string "Runtime exception at "
  417.  
  418. LAF_MSG2:
  419. .string ": Cannot read directly from text segment!"
  420.  
  421. SAM_MSG1:
  422. .string "Runtime exception at 0x00400004"
  423.  
  424. SAM_MSG2:
  425. .string ": Store address not aligned to word boundary "
  426.  
  427. SAF_MSG1:
  428. .string "Runtime exception at "
  429.  
  430. SAF_MSG2:
  431. .string ": Store address not aligned to word boundary "
  432.  
  433. hex_buffer:
  434. .space 32
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement