1 |
2 |
skordal |
-- The Potato Processor - A simple processor for FPGAs
|
2 |
|
|
-- (c) Kristian Klomsten Skordal 2014 - 2015 <kristian.skordal@wafflemail.net>
|
3 |
3 |
skordal |
-- Report bugs and issues on <http://opencores.org/project,potato,bugtracker>
|
4 |
2 |
skordal |
|
5 |
|
|
library ieee;
|
6 |
|
|
use ieee.std_logic_1164.all;
|
7 |
|
|
|
8 |
|
|
--! @brief Package containing constants and utility functions relating to status and control registers.
|
9 |
|
|
package pp_csr is
|
10 |
|
|
|
11 |
|
|
--! Type used for specifying control and status register addresses.
|
12 |
|
|
subtype csr_address is std_logic_vector(11 downto 0);
|
13 |
|
|
|
14 |
|
|
--! Type used for exception cause values.
|
15 |
58 |
skordal |
subtype csr_exception_cause is std_logic_vector(5 downto 0); -- Upper bit is the interrupt bit
|
16 |
2 |
skordal |
|
17 |
58 |
skordal |
--! Converts an exception cause to a std_logic_vector.
|
18 |
2 |
skordal |
function to_std_logic_vector(input : in csr_exception_cause) return std_logic_vector;
|
19 |
|
|
|
20 |
|
|
--! Control/status register write mode:
|
21 |
|
|
type csr_write_mode is (
|
22 |
|
|
CSR_WRITE_NONE, CSR_WRITE_SET, CSR_WRITE_CLEAR, CSR_WRITE_REPLACE
|
23 |
|
|
);
|
24 |
|
|
|
25 |
|
|
-- Exception cause values:
|
26 |
58 |
skordal |
constant CSR_CAUSE_INSTR_MISALIGN : csr_exception_cause := b"000000";
|
27 |
|
|
constant CSR_CAUSE_INSTR_FETCH : csr_exception_cause := b"000001";
|
28 |
|
|
constant CSR_CAUSE_INVALID_INSTR : csr_exception_cause := b"000010";
|
29 |
|
|
constant CSR_CAUSE_BREAKPOINT : csr_exception_cause := b"000011";
|
30 |
|
|
constant CSR_CAUSE_LOAD_MISALIGN : csr_exception_cause := b"000100";
|
31 |
|
|
constant CSR_CAUSE_LOAD_ERROR : csr_exception_cause := b"000101";
|
32 |
|
|
constant CSR_CAUSE_STORE_MISALIGN : csr_exception_cause := b"000110";
|
33 |
|
|
constant CSR_CAUSE_STORE_ERROR : csr_exception_cause := b"000111";
|
34 |
|
|
constant CSR_CAUSE_ECALL : csr_exception_cause := b"001011";
|
35 |
|
|
constant CSR_CAUSE_NONE : csr_exception_cause := b"011111";
|
36 |
2 |
skordal |
|
37 |
58 |
skordal |
constant CSR_CAUSE_SOFTWARE_INT : csr_exception_cause := b"100000";
|
38 |
|
|
constant CSR_CAUSE_TIMER_INT : csr_exception_cause := b"100001";
|
39 |
|
|
constant CSR_CAUSE_IRQ_BASE : csr_exception_cause := b"110000";
|
40 |
2 |
skordal |
|
41 |
58 |
skordal |
-- Control register IDs, specified in the immediate field of csr* instructions:
|
42 |
2 |
skordal |
constant CSR_CYCLE : csr_address := x"c00";
|
43 |
|
|
constant CSR_CYCLEH : csr_address := x"c80";
|
44 |
|
|
constant CSR_TIME : csr_address := x"c01";
|
45 |
|
|
constant CSR_TIMEH : csr_address := x"c81";
|
46 |
|
|
constant CSR_INSTRET : csr_address := x"c02";
|
47 |
|
|
constant CSR_INSTRETH : csr_address := x"c82";
|
48 |
|
|
|
49 |
58 |
skordal |
constant CSR_MCPUID : csr_address := x"f00";
|
50 |
|
|
constant CSR_MIMPID : csr_address := x"f01";
|
51 |
|
|
constant CSR_MHARTID : csr_address := x"f10";
|
52 |
2 |
skordal |
|
53 |
58 |
skordal |
constant CSR_MSTATUS : csr_address := x"300";
|
54 |
|
|
constant CSR_MTVEC : csr_address := x"301";
|
55 |
|
|
constant CSR_MTDELEG : csr_address := x"302";
|
56 |
|
|
constant CSR_MIE : csr_address := x"304";
|
57 |
|
|
|
58 |
|
|
constant CSR_MTIMECMP : csr_address := x"321";
|
59 |
|
|
constant CSR_MTIME : csr_address := x"701";
|
60 |
|
|
|
61 |
|
|
constant CSR_MSCRATCH : csr_address := x"340";
|
62 |
|
|
constant CSR_MEPC : csr_address := x"341";
|
63 |
|
|
constant CSR_MCAUSE : csr_address := x"342";
|
64 |
|
|
constant CSR_MBADADDR : csr_address := x"343";
|
65 |
|
|
constant CSR_MIP : csr_address := x"344";
|
66 |
|
|
|
67 |
|
|
constant CSR_MTOHOST : csr_address := x"780";
|
68 |
|
|
constant CSR_MFROMHOST : csr_address := x"781";
|
69 |
|
|
|
70 |
|
|
-- Values used as control register IDs in ERET:
|
71 |
|
|
constant CSR_EPC_ERET : csr_address := x"100";
|
72 |
|
|
|
73 |
|
|
-- Offset into the exception vector for handling machine-mode exceptions:
|
74 |
|
|
constant CSR_MTVEC_M_OFFSET : natural := 192;
|
75 |
|
|
|
76 |
|
|
-- Additional CSRs from supervisor mode that aliases machine mode registers
|
77 |
|
|
-- in this implementation:
|
78 |
|
|
--constant CSR_STVEC : csr_address := x"101";
|
79 |
|
|
--constant CSR_SEPC : csr_address := x"141";
|
80 |
|
|
|
81 |
2 |
skordal |
-- Status register bit indices:
|
82 |
58 |
skordal |
constant CSR_SR_IE : natural := 0;
|
83 |
|
|
constant CSR_SR_IE1 : natural := 3;
|
84 |
2 |
skordal |
|
85 |
58 |
skordal |
-- MIE and MIP register bit indices:
|
86 |
|
|
constant CSR_MIE_MSIE : natural := 3;
|
87 |
|
|
constant CSR_MIE_MTIE : natural := 7;
|
88 |
|
|
constant CSR_MIP_MSIP : natural := CSR_MIE_MSIE;
|
89 |
|
|
constant CSR_MIP_MTIP : natural := CSR_MIE_MTIE;
|
90 |
2 |
skordal |
|
91 |
58 |
skordal |
-- Exception context; this record contains all state that can be manipulated
|
92 |
2 |
skordal |
-- when an exception is taken.
|
93 |
|
|
type csr_exception_context is
|
94 |
|
|
record
|
95 |
58 |
skordal |
ie, ie1 : std_logic; -- Enable Interrupt bits
|
96 |
|
|
cause : csr_exception_cause;
|
97 |
|
|
badaddr : std_logic_vector(31 downto 0);
|
98 |
2 |
skordal |
end record;
|
99 |
|
|
|
100 |
58 |
skordal |
--! Creates the value of the mstatus registe from the EI and EI1 bits.
|
101 |
|
|
function csr_make_mstatus(ie, ie1 : in std_logic) return std_logic_vector;
|
102 |
2 |
skordal |
|
103 |
|
|
end package pp_csr;
|
104 |
|
|
|
105 |
|
|
package body pp_csr is
|
106 |
|
|
|
107 |
|
|
function to_std_logic_vector(input : in csr_exception_cause)
|
108 |
|
|
return std_logic_vector is
|
109 |
|
|
begin
|
110 |
58 |
skordal |
return (31 => input(5), 30 downto 5 => '0') & input(4 downto 0);
|
111 |
2 |
skordal |
end function to_std_logic_vector;
|
112 |
|
|
|
113 |
58 |
skordal |
function csr_make_mstatus(ie, ie1 : in std_logic) return std_logic_vector is
|
114 |
|
|
variable retval : std_logic_vector(31 downto 0);
|
115 |
2 |
skordal |
begin
|
116 |
58 |
skordal |
retval := (
|
117 |
|
|
11 downto 10 => '1', -- PRV3
|
118 |
|
|
8 downto 7 => '1', -- PRV2
|
119 |
|
|
5 downto 4 => '1', -- PRV1
|
120 |
|
|
CSR_SR_IE1 => ie1, -- IE1
|
121 |
|
|
2 downto 1 => '1', -- PRV
|
122 |
|
|
CSR_SR_IE => ie, -- IE
|
123 |
|
|
others => '0');
|
124 |
2 |
skordal |
return retval;
|
125 |
58 |
skordal |
end function csr_make_mstatus;
|
126 |
2 |
skordal |
|
127 |
|
|
end package body pp_csr;
|