CA Lab Manual 7
CA Lab Manual 7
R
file. These registers have their 12-bit address defined as part of the RISC-V specification. The
register file will have the address of the CSR register, the value of PC during the
Memory-Writeback stage, the CSR register write control, the CSR register read control, the
interrupt/exception pins and the CSR register write data at its input. The register file will have the
H
CSR read data and the exception PC at its output. This can be illustrated by Figure 9.1.
,L
ET
Listing 9.1 illustrates the implementation of the CSR Register file read and write operations. In the
illustrated code we can see that the read and write operation will depend on the 12-bit address of
the CSR registers. For checking the values address of the CSR registers refer to Table 2.5 of the
RISC-V privileged specifications manual.
EE
Copyright © 2022 “University of Engineering and Technology, Lahore” All rights reserved.
36
R
...
CSR_ADDR_MEPC : csr_mepc_wr_flag = 1'b1;
endcase // exu2csr_data.csr_addr
H
end // exe2csr_ctrl.csr_wr_req
end
,L
// Update the mip (machine interrupt pending) CSR
always_ff @(negedge rst_n, posedge clk) begin
if (~rst_n) begin
csr_mip_ff <= {`XLEN{1'b0}};
ET
end else if (csr_mip_wr_flag) begin
csr_mip_ff <= csr_wdata;
end
end
...
// Update the mtvec CSR
U
Tasks
- Implement the CSR register file while complying with the specifications manual and
simulate it to check the read and write operation.
Copyright © 2022 “University of Engineering and Technology, Lahore” All rights reserved.
37
Implementation of CSRRW
R
For implementing these instructions, we will be required to integrate the CSR register file in our
datapath. We will also modify our controller for adding two new control signals for the read and
H
write operation of the CSR register file. The address of the CSR register file will be given by the
immediate value generator and the data to be written to the CSR register will be given by the
output of the rs1 forwarding mux. The read data output of the CSR register file will be connected to
,L
the writeback mux. These changes have been illustrated in Figure 9.3.
ET
U
EE
Copyright © 2022 “University of Engineering and Technology, Lahore” All rights reserved.
38
Tasks
- Integrate the CSR register file and implement the CSRRW instruction in your processor.
- Write a simple assembly code to test whether the CSR instruction is working correctly or
not. A sample code has been provided in Listing 9.2.
li x10,1
li x12,10
csrrw x11,mtvec,x10
csrrw x13,mtvec,x12
R
Implementation of MRET
Now we will also modify our controller for adding a new multiplexor and new control signals for
H
detecting the mret instruction. The CSR register file receives this control signal from the pipeline
register. In case the mret instruction is received by the CSR register file the value of mepc register
will be loaded in the epc/evec output of the register file and epc_taken flag of the mux will be
asserted. ,L
ET
U
EE
Copyright © 2022 “University of Engineering and Technology, Lahore” All rights reserved.
39
Interrupt Handling
Whenever an interrupt arrives, the interrupt will be registered at the positive edge of the clock in
the corresponding bit of the mip register based on the source of the interrupt. If the corresponding
bit of the mie register is high and the MIE bit of the mstatus register is high then we say that the
Exception/interrupt has occurred and the value of PC in Memory-Writeback stage will be saved in
the mepc register.
R
H
,L
ET
Figure 9.5. Interrupt Handling in CSR Register File.
When the Exception/interrupt has occurred epc_taken flag in datapath will be asserted and the
cause of the interrupt will be saved in the mcause register by the hardware. Based on the mode of
the mtvec register, the epc/evec address will be calculated as the base_address and
base_address+cause*4 in non-vector mode and vector mode respectively. Please note that the
mie, mstatus and mtvec register will be configured by software using csrrw instruction.
U
EE
Copyright © 2022 “University of Engineering and Technology, Lahore” All rights reserved.
40
Tasks
- Modify the CSR register file for handling single interrupt. Generate the interrupts using
testbenches.
- Write a simple assembly code to test whether the processor is handling interrupt. Consult
the RISC-V specifications for configuring the CSR registers. A sample code is shown in
Listing 9.3.
j main
j interrupt_handler
main:
li x11,(calculate your own value)
R
li x12,(calculate your own value)
li x13,(calculate your own value)
csrrw x0,mie,x11
H
csrrw x0,mstatus,x12
csrrw x0,mtvec,x13
loop:
addi x14,x14,1
j loop
,L
ET
interrupt_handler:
csrrw x0,mie,x0
li x2,0xFFFFFFFF
xor x3,x3,x2
csrrw x0,mie,x11
U
mret
Copyright © 2022 “University of Engineering and Technology, Lahore” All rights reserved.