CSR Registers¶
CSR are… registers. Nothing to say more than that !
csr_registers¶
csr_registers.vhd
csr_registers module
Imports¶
Imports 3 elements
ieee std_logic_1164 numeric_std
work common
Parameters¶
Parameters 6 elements
- • XLEN Type : INTEGER Default : 32
! @brief Configure the data width in the core.
- • MSTATUS_W_MASK Type : STD_LOGIC_VECTOR Default : 0x30856
-
- • MIE_W_MASK Type : STD_LOGIC_VECTOR Default : 0x4294903944
-
- • MTVEC_W_MASK Type : STD_LOGIC_VECTOR Default : 0x4294967041
! @brief Write mask for MEPC register ! @details ! Bitmask defining which bits in MEPC are writable. ! Each bit corresponds to one bit position in the CSR.
- • MEPC_W_MASK Type : STD_LOGIC_VECTOR Default : 0x4294967294
-
- • MCAUSE_W_MASK Type : STD_LOGIC_VECTOR Default : 0x2147483679
-
Ports¶
Ports list 15 elements
Name |
Direction |
Description |
Type |
Size |
Kind |
Polarity |
Fanout |
|---|---|---|---|---|---|---|---|
clock |
Input |
! @brief clock enable from the core clock controller. Used to not create two clock domains from the master clock and the auxilliary clock. |
|
- |
Combinatorial |
- |
Fanout : 0 |
clock_en |
Input |
! @brief reset input, active low. When held to '0', the system will remain in the reset state until set to '1'. |
|
- |
Register clock |
- |
Fanout : 0 |
nRST |
Input |
|
|
- |
Combinatorial |
Negative |
Fanout : 0 |
we |
Input |
! @brief Write address, as an integer. |
|
- |
Register clock |
- |
Fanout : 0 |
wa |
Input |
! @brief Write data, expressed as a vector of the same length as the the default value. |
|
- |
Register clock |
- |
Fanout : 0 |
wd |
Input |
|
|
|
Register clock |
- |
Fanout : 0 |
ra1 |
Input |
! @brief Data for the read port |
|
- |
Combinatorial |
- |
Fanout : 0 |
rd1 |
Output |
|
|
|
Combinatorial |
- |
Fanout : 0 |
int 3 elements
Name |
Direction |
Description |
Type |
Size |
Kind |
Polarity |
Fanout |
|---|---|---|---|---|---|---|---|
int_vec |
Input |
! @brief Interrupt out, as MIP(7). Only the master external interrupt is handled, thus the interrupt_controller peripheral is required. |
|
|
Register clock |
- |
Fanout : 0 |
int_out |
Output |
! @brief Interrupt enable bit, as an output MIE(3) . Indicate that the core must account for an interrupt. |
|
- |
Combinatorial |
- |
Fanout : 0 |
int_en |
Output |
|
|
- |
Combinatorial |
- |
Fanout : 0 |
in 4 elements
Name |
Direction |
Description |
Type |
Size |
Kind |
Polarity |
Fanout |
|---|---|---|---|---|---|---|---|
in_cycleh |
Input |
! @brief Cycle counter 32 LSB Input. |
|
|
Register clock |
- |
Fanout : 0 |
in_cyclel |
Input |
! @brief Instruction counter 32 MSB Input. |
|
|
Register clock |
- |
Fanout : 0 |
in_instrh |
Input |
! @brief Instruction counter 32 LSB Input. |
|
|
Register clock |
- |
Fanout : 0 |
in_instrl |
Input |
|
|
|
Register clock |
- |
Fanout : 0 |
Processes¶
Process list 1 element
• P1 Clocked Clocked by clock Reset by nRST
- -
Signals¶
Signals list 12 elements
Name |
Description |
Type |
Size |
Kind |
Fanout |
|---|---|---|---|---|---|
mstatus |
! @brief Register mstatus. |
|
|
Register clock nRST |
Fanout : 0 |
misa |
! @brief Register misa. |
|
|
Register clock nRST |
Fanout : 0 |
mie |
! @brief Register mie. |
|
|
Register clock nRST |
Fanout : 0 |
mtvec |
! @brief Register mtvec. |
|
|
Register clock nRST |
Fanout : 0 |
mscratch |
! @brief Register mscratch. |
|
|
Register clock nRST |
Fanout : 0 |
mepc |
! @brief Register mepc. |
|
|
Register clock nRST |
Fanout : 0 |
mcause |
! @brief Register mcause. |
|
|
Register clock nRST |
Fanout : 0 |
mip |
! @brief Register mtval. SIGNAL mtval : STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0); ! @brief Register mip. |
|
|
Register clock nRST |
Fanout : 0 |
cycleh |
! @brief Register cycleh |
|
|
Register clock nRST |
Fanout : 0 |
cyclel |
! @brief Register cyclel |
|
|
Register clock nRST |
Fanout : 0 |
instrh |
! @brief Register instrh |
|
|
Register clock nRST |
Fanout : 0 |
instrl |
! @brief Register instrl |
|
|
Register clock nRST |
Fanout : 0 |
Assignments¶
Assignment list 3 elements
- • rd1 Combinatorial
asynchronous reads (combinational muxes)
<br>Sources : cycleh cyclel instrh instrl mcause mepc mie mip misa mscratch mstatus mtvec r_cycle r_cycleh r_instr r_instrh r_mcause r_mepc r_mie r_mip r_misa r_mscratch r_mstatus r_mtvec ra1- • int_out Combinatorial
always read the MIP port.
<br>Source : mip- • int_en Combinatorial
Source : mstatus
Instantiation templates¶
Instantiation templates 2 elements
csr_registers <name> #(
.XLEN ( 32),
.MSTATUS_W_MASK (0x30856),
.MIE_W_MASK (0x4294903944),
.MTVEC_W_MASK (0x4294967041),
.MEPC_W_MASK (0x4294967294),
.MCAUSE_W_MASK (0x2147483679)
)(
.clock (i_clock),
.clock_en (i_clock_en),
.nRST (i_nRST),
.we (i_we),
.wa (i_wa),
.wd (i_wd),
.ra1 (i_ra1),
.rd1 (i_rd1),
.int_vec (i_int_vec),
.int_out (i_int_out),
.int_en (i_int_en),
.in_cycleh (i_in_cycleh),
.in_cyclel (i_in_cyclel),
.in_instrh (i_in_instrh),
.in_instrl (i_in_instrl)
);
<name>: entity work.csr_registers
generic map (
XLEN => 32,
MSTATUS_W_MASK => 0x30856,
MIE_W_MASK => 0x4294903944,
MTVEC_W_MASK => 0x4294967041,
MEPC_W_MASK => 0x4294967294,
MCAUSE_W_MASK => 0x2147483679
)
port map (
clock => i_clock,
clock_en => i_clock_en,
nRST => i_nRST,
we => i_we,
wa => i_wa,
wd => i_wd,
ra1 => i_ra1,
rd1 => i_rd1,
int_vec => i_int_vec,
int_out => i_int_out,
int_en => i_int_en,
in_cycleh => i_in_cycleh,
in_cyclel => i_in_cyclel,
in_instrh => i_in_instrh,
in_instrl => i_in_instrl
);
Source¶
Raw source Full file
--! @file src/core/core.vhd
--! @brief The base file that assemble all of the components of the core. Does not include any form of memory.
--! @author l.heywang <leonard.heywang@proton.me>
--! @date 05-10-2025
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
USE ieee.numeric_std.ALL;
USE work.common.ALL;
ENTITY csr_registers IS
GENERIC (
--! @brief Configure the data width in the core.
XLEN : INTEGER := 32
);
PORT (
--------------------------------------------------------------------------------------------------------
-- Clocks & controls
--------------------------------------------------------------------------------------------------------
--! @brief clock input of the core. Must match the INPUT_FREQ generics within some tolerance.
clock : IN STD_LOGIC;
--! @brief clock enable from the core clock controller. Used to not create two clock domains from the master clock and the auxilliary clock.
clock_en : IN STD_LOGIC;
--! @brief reset input, active low. When held to '0', the system will remain in the reset state until set to '1'.
nRST : IN STD_LOGIC;
--------------------------------------------------------------------------------------------------------
-- Writing port
--------------------------------------------------------------------------------------------------------
--! @brief Write enable pin. Active high. Set to '1' to enable any write operation on the CSR register file.
we : IN STD_LOGIC;
--! @brief Write address, as an integer.
wa : IN csr_register;
--! @brief Write data, expressed as a vector of the same length as the the default value.
wd : IN STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0);
--------------------------------------------------------------------------------------------------------
-- Reading ports
--------------------------------------------------------------------------------------------------------
--! @brief Address for the read port
ra1 : IN csr_register;
--! @brief Data for the read port
rd1 : OUT STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0);
--------------------------------------------------------------------------------------------------------
-- Interrupt related IOs
--------------------------------------------------------------------------------------------------------
--! @brief Interrupt vector, as input. The MIE interrupt mask will be applied to it, and result placed into MIP.
int_vec : IN STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0);
--! @brief Interrupt out, as MIP(7). Only the master external interrupt is handled, thus the interrupt_controller peripheral is required.
int_out : OUT STD_LOGIC;
--! @brief Interrupt enable bit, as an output MIE(3) . Indicate that the core must account for an interrupt.
int_en : OUT STD_LOGIC;
--------------------------------------------------------------------------------------------------------
-- Interrupt related IOs
--------------------------------------------------------------------------------------------------------
--! @brief Cycle counter 32 MSB Input.
in_cycleh : IN STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0);
--! @brief Cycle counter 32 LSB Input.
in_cyclel : IN STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0);
--! @brief Instruction counter 32 MSB Input.
in_instrh : IN STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0);
--! @brief Instruction counter 32 LSB Input.
in_instrl : IN STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0)
);
END ENTITY;
ARCHITECTURE rtl OF csr_registers IS
--! @brief Write mask for MSTATUS register
--! @details
--! Bitmask defining which bits in **MSTATUS** are writable.
--! Each bit corresponds to one bit position in the CSR.
CONSTANT MSTATUS_W_MASK : STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0) := "00000000000000000111100010001000";
--! @brief Write mask for MIE register
--! @details
--! Bitmask defining which bits in **MIE** are writable.
--! Each bit corresponds to one bit position in the CSR.
CONSTANT MIE_W_MASK : STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0) := "11111111111111110000100010001000";
--! @brief Write mask for MTVEC register
--! @details
--! Bitmask defining which bits in **MTVEC** are writable.
--! Each bit corresponds to one bit position in the CSR.
CONSTANT MTVEC_W_MASK : STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0) := "11111111111111111111111100000001";
--! @brief Write mask for MEPC register
--! @details
--! Bitmask defining which bits in **MEPC** are writable.
--! Each bit corresponds to one bit position in the CSR.
CONSTANT MEPC_W_MASK : STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0) := "11111111111111111111111111111110";
--! @brief Write mask for MCAUSE register
--! @details
--! Bitmask defining which bits in **MCAUSE** are writable.
--! Each bit corresponds to one bit position in the CSR.
CONSTANT MCAUSE_W_MASK : STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0) := "10000000000000000000000000011111";
--! @brief Register mstatus.
SIGNAL mstatus : STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0);
--! @brief Register misa.
SIGNAL misa : STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0);
--! @brief Register mie.
SIGNAL mie : STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0);
--! @brief Register mtvec.
SIGNAL mtvec : STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0);
--! @brief Register mscratch.
SIGNAL mscratch : STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0);
--! @brief Register mepc.
SIGNAL mepc : STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0);
--! @brief Register mcause.
SIGNAL mcause : STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0);
--! @brief Register mtval.
-- SIGNAL mtval : STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0);
--! @brief Register mip.
SIGNAL mip : STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0);
--! @brief Register cycleh
SIGNAL cycleh : STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0);
--! @brief Register cyclel
SIGNAL cyclel : STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0);
--! @brief Register instrh
SIGNAL instrh : STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0);
--! @brief Register instrl
SIGNAL instrl : STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0);
--! @brief Compute the new register value by applying pure logic to the inputs, while accouting for write masks.
--! @details
--! performs the operation OUT <= (IN & MASK) | (OLD & ~MASK)
--! This enable to overwrite the bits with the new value regarless of their previous state, while preserving protected bits.
FUNCTION update_bits(
write_in : STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0);
write_mask : STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0);
write_old : STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0))
RETURN STD_LOGIC_VECTOR IS
BEGIN
RETURN (write_in AND write_mask) OR (write_old AND (NOT write_mask));
END FUNCTION;
BEGIN
--========================================================================================
--! @brief P1 handle all of the write parts (since reading are done asynchronously).
--! @details
--! On each authorized rising edges, if we need to write (WE = '1') and the write
--! address is not 0 (as per the spec, this register could not be written),
--! update the register file.
--! On reset, the register file is initialized to 0x00000000 for all registers.
--! The biggest difference with the register file is the fact that the mask is applied to ANy writes.
--========================================================================================
P1 : PROCESS (clock, nRST, int_vec)
BEGIN
-- Handle reset
IF (nRST = '0') THEN
mstatus <= X"0000_1800";
misa <= X"4000_0100";
mie <= (OTHERS => '0');
mtvec <= (OTHERS => '0');
mscratch <= (OTHERS => '0');
mepc <= (OTHERS => '0');
mcause <= (OTHERS => '0');
-- mtval <= (OTHERS => '0');
mip <= (OTHERS => '0');
cycleh <= (OTHERS => '0');
cyclel <= (OTHERS => '0');
instrl <= (OTHERS => '0');
instrh <= (OTHERS => '0');
-- Handle standard writes
ELSIF rising_edge(clock) AND (clock_en = '1') THEN
-- Synchronizing the MIP register
mip <= int_vec AND mie;
-- Copying the values of the counters
cyclel <= in_cyclel;
cycleh <= in_cycleh;
instrl <= in_instrl;
instrh <= in_instrh;
-- Updating the others registers.
IF (we = '1') THEN
CASE wa IS
WHEN r_MSTATUS =>
mstatus <= update_bits(wd, MSTATUS_W_MASK, mstatus);
-- when r_MISA =>
-- misa <= update_bits(wd, MISA_W_MASK, misa); -- Mask is 0
WHEN r_MIE =>
mie <= update_bits(wd, MIE_W_MASK, mie);
WHEN r_MTVEC =>
mtvec <= update_bits(wd, MTVEC_W_MASK, mtvec);
WHEN r_MSCRATCH =>
-- mscratch <= update_bits(wd, MSCRATCH_W_MASK, mscratch);
mscratch <= wd; -- Mask is FFFF_FFFF, so direct copy
WHEN r_MEPC =>
mepc <= update_bits(wd, MEPC_W_MASK, mepc);
WHEN r_MCAUSE =>
mcause <= update_bits(wd, MCAUSE_W_MASK, mcause);
-- when r_MTVAL =>
-- mtval <= update_bits(wd, MTVAL_W_MASK, mtval); -- Mask is 0
-- when r_MIP =>
-- mip <= update_bits(wd, MIP_W_MASK, mcause); -- Mask is 0
WHEN OTHERS =>
-- do nothing
END CASE;
END IF;
END IF;
END PROCESS;
-- asynchronous reads (combinational muxes)
rd1 <= mstatus WHEN (ra1 = r_MSTATUS) ELSE
misa WHEN (ra1 = r_MISA) ELSE
mie WHEN (ra1 = r_MIE) ELSE
mtvec WHEN (ra1 = r_MTVEC) ELSE
mscratch WHEN (ra1 = r_MSCRATCH) ELSE
mepc WHEN (ra1 = r_MEPC) ELSE
mcause WHEN (ra1 = r_MCAUSE) ELSE
-- mtval WHEN (ra1 = r_MTVAL) ELSE -- Reset to 0, can never been update --> return 0x00000000 on the other close
mip WHEN (ra1 = r_MIP) ELSE
cycleh WHEN (ra1 = r_CYCLEH) ELSE
cyclel WHEN (ra1 = r_CYCLE) ELSE
instrh WHEN (ra1 = r_INSTRH) ELSE
instrl WHEN (ra1 = r_INSTR) ELSE
-- Remaining registers are known, but unused and thus return 0 :
-- mvendorid (0x00000000)
-- marchid (0x00000000)
-- mimpid (0x00000000)
-- mhartid (0x00000000)
(OTHERS => '0');
-- always read the MIP port.
int_out <= mip(11);
int_en <= mstatus(3);
END ARCHITECTURE;
csr_registers_tb¶
csr_registers_tb.vhd
csr_registers_tb testbench
Imports¶
Imports 3 elements
ieee std_logic_1164 numeric_std
work common
Signals¶
Signals list 10 elements
Name |
Description |
Type |
Size |
Kind |
Fanout |
|---|---|---|---|---|---|
nRST_t |
|
|
- |
Combinatorial |
Fanout : 0 |
we_t |
|
|
- |
Combinatorial |
Fanout : 0 |
wa_t |
|
|
- |
Combinatorial |
Fanout : 0 |
wd_t |
|
|
|
Combinatorial |
Fanout : 0 |
ra1_t |
|
|
- |
Combinatorial |
Fanout : 0 |
rd1_t |
|
|
|
Combinatorial |
Fanout : 0 |
clock 2 elements
Name |
Description |
Type |
Size |
Kind |
Fanout |
|---|---|---|---|---|---|
clock_t |
|
|
- |
Combinatorial |
Fanout : 0 |
clock_en_t |
|
|
- |
Register clk rst_n |
Fanout : 0 |
int 2 elements
Name |
Description |
Type |
Size |
Kind |
Fanout |
|---|---|---|---|---|---|
int_vec_t |
|
|
|
Combinatorial |
Fanout : 0 |
int_out_t |
|
|
- |
Combinatorial |
Fanout : 0 |
Modules¶
Processes¶
Process list 5 elements
• P1
clock
• P2
nRST
• P3
Write data
• P4
Read data
• P5
Emulate interrupt
Source¶
Raw source Full file
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
USE ieee.numeric_std.ALL;
USE work.common.ALL;
ENTITY csr_registers_tb IS
END ENTITY;
ARCHITECTURE behavioral OF csr_registers_tb IS
SIGNAL clock_t : STD_LOGIC := '0';
SIGNAL clock_en_t : STD_LOGIC := '0';
SIGNAL nRST_t : STD_LOGIC := '0';
SIGNAL we_t : STD_LOGIC := '0';
SIGNAL wa_t : csr_register := r_MSTATUS;
SIGNAL wd_t : STD_LOGIC_VECTOR(31 DOWNTO 0) := (OTHERS => '1');
SIGNAL ra1_t : csr_register := r_MSTATUS;
SIGNAL rd1_t : STD_LOGIC_VECTOR(31 DOWNTO 0) := (OTHERS => '0');
SIGNAL int_vec_t : STD_LOGIC_VECTOR(31 DOWNTO 0) := (OTHERS => '0');
SIGNAL int_out_t : STD_LOGIC := '0';
BEGIN
U1 : ENTITY work.clock(behavioral)
PORT MAP(
clk => clock_t,
nRST => nRST_t,
clk_en => clock_en_t
);
U2 : ENTITY work.csr_registers(rtl)
PORT MAP(
clock => clock_t,
clock_en => clock_en_t,
nRST => nRST_t,
we => we_t,
wa => wa_t,
wd => wd_t,
ra1 => ra1_t,
rd1 => rd1_t,
int_vec => int_vec_t,
int_out => int_out_t
);
-- clock
P1 : PROCESS
BEGIN
WAIT FOR 5 ns;
clock_t <= NOT clock_t;
END PROCESS;
-- nRST
P2 : PROCESS
BEGIN
nRST_t <= '0';
WAIT FOR 15 ns;
nRST_t <= '1';
WAIT;
END PROCESS;
-- Write data
P3 : PROCESS
BEGIN
WAIT FOR 15 ns;
WAIT FOR 200 ns;
wa_t <= r_MSTATUS;
we_t <= '1';
WAIT FOR 20 ns;
wa_t <= r_MISA;
WAIT FOR 20 ns;
wa_t <= r_MIE;
WAIT FOR 20 ns;
wa_t <= r_MTVEC;
WAIT FOR 20 ns;
wa_t <= r_MSCRATCH;
WAIT FOR 20 ns;
wa_t <= r_MEPC;
WAIT FOR 20 ns;
wa_t <= r_MCAUSE;
WAIT FOR 20 ns;
wa_t <= r_MTVAL;
WAIT FOR 20 ns;
wa_t <= r_MIP;
WAIT FOR 20 ns;
we_t <= '0';
wd_t <= X"0000_0000";
WAIT FOR 200 ns;
END PROCESS;
-- Read data
P4 : PROCESS
BEGIN
WAIT FOR 20 ns;
ra1_t <= r_MSTATUS;
WAIT FOR 20 ns;
ra1_t <= r_MISA;
WAIT FOR 20 ns;
ra1_t <= r_MIE;
WAIT FOR 20 ns;
ra1_t <= r_MTVEC;
WAIT FOR 20 ns;
ra1_t <= r_MSCRATCH;
WAIT FOR 20 ns;
ra1_t <= r_MEPC;
WAIT FOR 20 ns;
ra1_t <= r_MCAUSE;
WAIT FOR 20 ns;
ra1_t <= r_MTVAL;
WAIT FOR 20 ns;
ra1_t <= r_MIP;
WAIT FOR 20 ns;
END PROCESS;
-- Emulate interrupt
P5 : PROCESS
BEGIN
WAIT FOR 15 ns;
WAIT FOR 600 ns;
WAIT FOR 18 ns;
int_vec_t <= X"FFFF_FFFF";
WAIT;
END PROCESS;
END ARCHITECTURE;