Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
176
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. #Note: Only IAM & IAF should load ra into EPC
  221. LAM:
  222. la a0, LAM_MSG1
  223. li a6, 4 #PrintString
  224. addi a5, zero, 1 #***STORE 1 IN A5*** IMPORTANT FOR TEARDOWN!!!
  225. jal mecall #after jal
  226. #after printing, resume execution here
  227. csrrw a0, 65, zero #store epc in a0
  228. jal hex_init
  229.  
  230.  
  231. #After store_byte, resume execution here
  232. print_epc:
  233. la a0, hex_buffer
  234. li a6, 4 #load printstr
  235. addi a5, zero, 1 #***STORE 1 IN A5*** IMPORTANT FOR TEARDOWN!!!
  236. jal mecall
  237. LAM2:
  238.  
  239. la a0, LAM_MSG2
  240. li a6, 4 #PrintString
  241. addi a5, zero, 1 #***STORE 1 IN A5*** IMPORTANT FOR TEARDOWN!!
  242. jal mecall
  243.  
  244. csrrw a0, 67, zero #store utval in a0
  245. jal hex_init
  246.  
  247. #should resume exec here
  248. print_utval:
  249. la a0, hex_buffer
  250. li a6, 4 #load printstr
  251. addi a5, zero, 1 #***STORE 1 IN A5*** IMPORTANT FOR TEARDOWN!!!
  252. jal mecall
  253.  
  254. hex_init:
  255. addi sp, sp, -32
  256. sw ra, 28(sp)
  257. sw t0, 24(sp)
  258. sw t1, 20(sp)
  259. sw t2, 16(sp)
  260. sw t3, 12(sp)
  261. sw t4, 8(sp)
  262. sw t5, 4(sp)
  263. sw t6, (sp)
  264.  
  265. la t0, hex_buffer
  266. li t1, 0
  267. li t2, 28
  268. li t3, 32
  269.  
  270. printhex:
  271. ebreak
  272. andi t4, t4, 0 #clear t4
  273. sll t4, a0, t1 #store epc in t4
  274. srl, t4, t4, t2 #Shift right 28 bits
  275.  
  276. li t5, 9 #String converter
  277. ble t4, t5, inc_epc
  278. addi t4, t4, 87
  279. b store_byte
  280.  
  281. inc_epc:
  282. addi t4, t4, 48
  283.  
  284. store_byte:
  285. sb t4, (t0) #store into buffer
  286. addi t0, t0, 1 #Point to next byte
  287. addi t1, t1, 4
  288. bne t1, t3, printhex #print another char if we havent reached 32nd bit
  289. lw ra, 28(sp) #HERE I NEED TO B BRACK TO LAMV
  290. ret
  291.  
  292.  
  293.  
  294. LAF:
  295. addi x0, x0, 4
  296. b LAF
  297.  
  298. SAM:
  299. addi x0, x0, 5
  300. b SAM
  301.  
  302. SAF:
  303.  
  304. BAD:
  305.  
  306. None:
  307.  
  308.  
  309.  
  310.  
  311. teardown:
  312. lw ra, 12(sp) #Retrieve RA
  313. lw t0, 8(sp) #Retrieve t0
  314. #addi sp, sp, 16 #Pop current stack frame
  315. #handler: # Just ignore it by moving epc (65) to the next instruction
  316. #Add 4 to epc, so uret returns to line AFTER ecall
  317. #lw t1, 52(sp)
  318. #lw t2, 48(sp)
  319. #lw t3, 44(sp)
  320. #lw t4, 40(sp)
  321. #lw t5, 36(sp)
  322. #lw t6, 32(sp)
  323. #lw a0, 28(sp)
  324. #lw a1, 24(sp)
  325. #lw a2, 20(sp)
  326. #lw a3, 16(sp)
  327. #lw a4, 12(sp)
  328. #lw a5, 8(sp)
  329. #lw a6, 4(sp)
  330. #lw a7, (sp)
  331. csrrw t6, 65, zero
  332. addi t6, t6, 4
  333. csrrw zero, 65, t6
  334. addi a7, zero 0 #Clear a7 ecall won't jump to mecall if a7 contains something
  335. #ebreak
  336.  
  337. bgt a5, zero, a5_triggered #IF a5 is 1, store ra in epc (LAM)
  338. uret #Returns from handling an interrupt, used instead of jumping to uepc (the user-level exception program counter)
  339.  
  340. a5_triggered:
  341. csrrw zero, 65, ra
  342. andi a5, a5, 0 #Clear a5
  343. uret
  344.  
  345.  
  346.  
  347. #8 is ID of ecall exception. ucause should be 8?
  348. #lw zero, 0
  349. # trigger trap for load access fault? #@ What does this mean? Dont need this. it causes exception
  350.  
  351. #To add external device interrupts. The “User External Interrupt Enable” (UEIP) bit must be set in the mie/uie register (CSR 4).
  352. #csrrsi zero, 4, 1
  353.  
  354.  
  355. #Set scratch reg to starting address of system stack?
  356.  
  357. #leave a7 as 0, load 4 into a6
  358.  
  359. .data #Use num in a7 as index to table
  360. test_str:
  361. .string "hi" #printstring called"
  362.  
  363. Subroutines:
  364. .word Undefined Undefined Undefined Undefined PrintString ReadInt Undefined Undefined ReadString Undefined Exit PrintChar ReadChar
  365.  
  366.  
  367. Error:
  368. .string "invalid or unimplemented syscall service"
  369.  
  370. ExitMessage:
  371. .string "-- program is finished running --"
  372.  
  373.  
  374. enter:
  375. .word 0x0A #Holds enter key
  376.  
  377. ReadStringBuffer:
  378. .space 20
  379.  
  380. TDR: #Transmitter Data Register - Characters written here will be displayed on screen
  381. .word 0xffff000c
  382.  
  383. TCR: #Transmitter Control Register - set to 1 when device ready to display character on screen
  384. .word 0xffff0008
  385.  
  386. RDR: #Receiver Data Register - Characters typed are stored here
  387. .word 0xffff0004
  388.  
  389. RCR: #Receiver Control Register - Bit [0] is one when keyboard has received a new character.
  390. .word 0xffff0000
  391.  
  392.  
  393. #IndexTbl:
  394. #.word 0, 1, 4, 5, 6, 7, 8, -1 #WHY -1?
  395.  
  396. ExceptionTbl:
  397. .word IAM IAF None None LAM LAF SAM SAF ENVCALL BAD #WHY BAD?
  398.  
  399. IAM_MSG:
  400. .string "Error in : Instruction load alignment error\n"
  401.  
  402. IAF_MSG:
  403. .string "Error in : Instruction load access error\n"
  404.  
  405. None_MSG:
  406. .string "?"
  407.  
  408. LAM_MSG1:
  409. .string "Runtime exception at "
  410.  
  411. LAM_MSG2:
  412. .string ": Load address not aligned to word boundary "
  413.  
  414. LAF_MSG1:
  415. .string "Runtime exception at "
  416.  
  417. LAF_MSG2:
  418. .string ": Cannot read directly from text segment!"
  419.  
  420. SAM_MSG1:
  421. .string "Runtime exception at 0x00400004"
  422.  
  423. SAM_MSG2:
  424. .string ": Store address not aligned to word boundary "
  425.  
  426. SAF_MSG1:
  427. .string "Runtime exception at "
  428.  
  429. SAF_MSG2:
  430. .string ": Store address not aligned to word boundary "
  431.  
  432. hex_buffer:
  433. .space 32
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement