OpenCores
URL https://opencores.org/ocsvn/hf-risc/hf-risc/trunk

Subversion Repositories hf-risc

[/] [hf-risc/] [trunk/] [hf-riscv/] [core_rv32i/] [peripherals_busmux.vhd] - Blame information for rev 19

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 18 serginhofr
-- HF-RISCV v1.4
2 13 serginhofr
-- Sergio Johann Filho, 2015 - 2016
3
--
4
-- *This is a quick and dirty organization of a 3-stage pipelined RISC-V microprocessor. All registers / memory
5
--  accesses are synchronized to the rising edge of clock. The same processor could be designed with only 2
6
--  pipeline stages, but this would require memories to be either asynchronous (as presented on comp arq text
7
--  books), double clocked or operating on the opposite edge. Pipeline stages are:
8
--
9
--  FETCH: instruction memory is accessed (address is PC), data becomes available in one cycle. PC is updated.
10
--  DECODE: an instruction is fed into the decoding / control logic and values are registered for the next
11
--  stage. pipeline stalls, as well as bubble insertion is performed in this stage.
12
--  EXECUTE: the register file is accessed and the ALU calculates the result. data access is performed (loads
13
--  and stores) or simply the result (or pc) is written to the register file (normal operations). branch target
14
--  and outcome are calculated.
15
--
16
-- *This design is a compromise between performance, area and complexity.
17
-- *Only the RV32I base instruction set is implemented. FENCE and SYSTEM instructions are missing. SYSTEM
18
--  instructions always trap the processor and can be handled in software.
19
-- *Memory is accessed in little endian mode.
20
-- *No co-processor is implemented and all peripherals are memory mapped.
21 18 serginhofr
-- *Loads and stores take 3 cycles. This version is organized as a Von Neumann machine, so there is only one
22
--  memory interface that is shared betweeen code and data accesses.
23 13 serginhofr
-- *Branches have a 1 cycle delay (not taken) or 3 cycle dalay (taken), including two branch delay slots.
24
--  This is a side effect of the pipeline refill and memory access policy. All other instructions are single
25
--  cycle. No branch predictor is implemented (default branch target is 'not taken').
26
-- *Interrupts are handled using VECTOR, CAUSE, MASK, STATUS and EPC registers. The VECTOR register is used to hold
27
--  the address of the default (non-vectored) interrupt handler. The CAUSE register is read only and peripheral
28
--  interrupt lines are connected to this register. The MASK register is read/write and holds the interrupt mask
29
--  for the CAUSE register. The interrupt STATUS register is automatically cleared on interrupts, and is set by
30
--  software when returning from interrupts - this works as a global interrupt enable/disable flag. This register is
31
--  read and write capable, so it can also be cleared by software. Setting this register just before returning
32
--  from interrupts (enable is delayed in a few cycles) re-enables interrupts. The EPC register holds the program
33
--  counter when the processor is interrupted (we should re-execute the last instruction (EPC-4), as it was not
34
--  commited yet). EPC is a read only register, and is used to return from an interrupt using simple LW / JR
35
--  instructions. As an interrupt is accepted, the processor jumps to VECTOR address where the first level of irq
36
--  handling is done. A second level handler (in C) implements the interrupt priority mechanism and calls the
37
--  appropriate ISR for each interrupt.
38
-- *Built in peripherals: running counter (32 bit), two counter comparators (32 and 24 bit), I/O ports and UART. the
39
--  UART baud rate is defined in a 16 bit divisor register. Two counter bits (bits 18 and 16 and their complements) are
40
--  tied to interrupt lines, so are the two counter comparators and the UART.
41
--
42
-- Memory map:
43
--
44
-- ROM                                  0x00000000 - 0x1fffffff (512MB)
45
-- System                               0x20000000 - 0x3fffffff (512MB)
46
-- SRAM                                 0x40000000 - 0x5fffffff (512MB)
47
-- External RAM / device                0x60000000 - 0x9fffffff (1GB)
48
-- External RAM / device                0xa0000000 - 0xdfffffff (1GB)           (uncached)
49
-- External Peripheral                  0xe0000000 - 0xefffffff (256MB)         (uncached)
50
-- Peripheral (core)                    0xf0000000 - 0xf7ffffff (128MB)         (uncached)
51
-- Peripheral (extended)                0xf8000000 - 0xffffffff (128MB)         (uncached)
52
--
53
--   IRQ_VECTOR                 0xf0000000
54
--   IRQ_CAUSE                  0xf0000010
55
--   IRQ_MASK                   0xf0000020
56
--   IRQ_STATUS                 0xf0000030
57
--   IRQ_EPC                    0xf0000040
58
--   COUNTER                    0xf0000050
59
--   COMPARE                    0xf0000060
60
--   COMPARE2                   0xf0000070
61
--   EXTIO_IN                   0xf0000080
62
--   EXTIO_OUT                  0xf0000090
63
--   DEBUG                      0xf00000d0
64
--   UART_WRITE / UART_READ     0xf00000e0
65
--   UART_DIVISOR               0xf00000f0
66
--
67
-- Interrupt masks:
68
--
69 19 serginhofr
-- IRQ_COUNTER                  0x00000001              (bit 18 of the counter is set)
70
-- IRQ_COUNTER_NOT              0x00000002              (bit 18 of the counter is clear)
71
-- IRQ_COUNTER2                 0x00000004              (bit 16 of the counter is set)
72
-- IRQ_COUNTER2_NOT             0x00000008              (bit 16 of the counter is clear)
73
-- IRQ_COMPARE                  0x00000010              (counter is equal to compare, clears irq when updated)
74
-- IRQ_COMPARE2                 0x00000020              (counter bits 23 to 0 are equal to compare2, clears irq when updated)
75
-- IRQ_UART_READ_AVAILABLE      0x00000040              (there is data available for reading on the UART)
76
-- IRQ_UART_WRITE_AVAILABLE     0x00000080              (UART is not busy)
77
-- EXT_IRQ0                     0x00010000              (external interrupts on extio_in, high level triggered)
78
-- EXT_IRQ1                     0x00020000
79
-- EXT_IRQ2                     0x00040000
80
-- EXT_IRQ3                     0x00080000
81
-- EXT_IRQ4                     0x00100000
82
-- EXT_IRQ5                     0x00200000
83
-- EXT_IRQ6                     0x00400000
84
-- EXT_IRQ7                     0x00800000
85
-- EXT_IRQ0_NOT                 0x01000000              (external interrupts on extio_in, low level triggered)
86
-- EXT_IRQ1_NOT                 0x02000000
87
-- EXT_IRQ2_NOT                 0x04000000
88
-- EXT_IRQ3_NOT                 0x08000000
89
-- EXT_IRQ4_NOT                 0x10000000
90
-- EXT_IRQ5_NOT                 0x20000000
91
-- EXT_IRQ6_NOT                 0x40000000
92
-- EXT_IRQ7_NOT                 0x80000000
93 13 serginhofr
 
94
library ieee;
95
use ieee.std_logic_1164.all;
96
use ieee.std_logic_unsigned.all;
97
use ieee.std_logic_arith.all;
98
 
99
entity busmux is
100
        generic(
101
                log_file: string := "UNUSED";                   -- options are "out.txt" and "UNUSED"
102
                uart_support: string := "no"                    -- options are "yes" and "no".
103
        );
104
        port (  clock:          in std_logic;
105
                reset:          in std_logic;
106
 
107
                stall:          in std_logic;
108
 
109
                stall_cpu:      out std_logic;
110
                irq_vector_cpu: out std_logic_vector(31 downto 0);
111
                irq_cpu:        out std_logic;
112
                irq_ack_cpu:    in std_logic;
113
                exception_cpu:  in std_logic;
114 18 serginhofr
                address_cpu:    in std_logic_vector(31 downto 0);
115 13 serginhofr
                data_in_cpu:    out std_logic_vector(31 downto 0);
116
                data_out_cpu:   in std_logic_vector(31 downto 0);
117
                data_w_cpu:     in std_logic_vector(3 downto 0);
118
                data_access_cpu:        in std_logic;
119
 
120
                addr_mem:       out std_logic_vector(31 downto 0);
121
                data_read_mem:  in std_logic_vector(31 downto 0);
122
                data_write_mem: out std_logic_vector(31 downto 0);
123
                data_we_mem:    out std_logic_vector(3 downto 0);
124
 
125
                extio_in:       in std_logic_vector(7 downto 0);
126
                extio_out:      out std_logic_vector(7 downto 0);
127
                uart_read:      in std_logic;
128
                uart_write:     out std_logic
129
        );
130
end busmux;
131
 
132
architecture arch of busmux is
133
        signal write_enable: std_logic;
134 19 serginhofr
        signal uart_divisor: std_logic_vector(15 downto 0);
135 13 serginhofr
        signal irq_status_reg, extio_out_reg: std_logic_vector(7 downto 0);
136 19 serginhofr
        signal periph_data, irq_vector_reg, irq_cause, irq_mask_reg, irq_epc_reg, compare_reg, counter_reg: std_logic_vector(31 downto 0);
137 13 serginhofr
        signal compare2_reg: std_logic_vector(23 downto 0);
138
        signal interrupt, irq, irq_counter, irq_counter_not, irq_counter2, irq_counter2_not, irq_compare, irq_compare2, compare_trig, compare2_trig: std_logic;
139
        signal data_read_uart, data_write_uart: std_logic_vector(7 downto 0);
140
        signal enable_uart, enable_uart_read, enable_uart_write, uart_write_busy, uart_data_avail: std_logic;
141
 
142 17 serginhofr
        type pulse_state_type is (irq_idle, irq_int, irq_req, irq_ackn, irq_done);
143 13 serginhofr
        signal pulse_state: pulse_state_type;
144
        signal pulse_next_state: pulse_state_type;
145
 
146 18 serginhofr
        signal periph_access, periph_access_dly, periph_access_we: std_logic;
147 13 serginhofr
        signal data_we_mem_s: std_logic_vector(3 downto 0);
148
 
149
begin
150
        -- address decoder, read from peripheral registers
151 18 serginhofr
        process(clock, reset, periph_access, address_cpu, irq_vector_reg, irq_cause, irq_mask_reg, irq_status_reg, irq_epc_reg, compare_reg, compare2_reg, counter_reg, data_read_uart, uart_divisor, data_read_mem, extio_in, extio_out_reg)
152 13 serginhofr
        begin
153 18 serginhofr
                if reset = '1' then
154
                        periph_data <= (others => '0');
155
                elsif clock'event and clock = '1' then
156
                        if periph_access = '1' then
157
                                case address_cpu(7 downto 4) is
158 13 serginhofr
                                        when "0000" =>          -- IRQ_VECTOR           (RW)
159 18 serginhofr
                                                periph_data <= irq_vector_reg;
160 13 serginhofr
                                        when "0001" =>          -- IRQ_CAUSE            (RO)
161 19 serginhofr
                                                periph_data <= irq_cause(7 downto 0) & irq_cause(15 downto 8) & irq_cause(23 downto 16) & irq_cause(31 downto 24);
162 13 serginhofr
                                        when "0010" =>          -- IRQ_MASK             (RW)
163 19 serginhofr
                                                periph_data <= irq_mask_reg(7 downto 0) & irq_mask_reg(15 downto 8) & irq_mask_reg(23 downto 16) & irq_mask_reg(31 downto 24);
164 13 serginhofr
                                        when "0011" =>          -- IRQ_STATUS           (RW)
165 18 serginhofr
                                                periph_data <= irq_status_reg & x"000000";
166 13 serginhofr
                                        when "0100" =>          -- IRQ_EPC              (RO)
167 18 serginhofr
                                                periph_data <= irq_epc_reg(7 downto 0) & irq_epc_reg(15 downto 8) & irq_epc_reg(23 downto 16) & irq_epc_reg(31 downto 24);
168 13 serginhofr
                                        when "0101" =>          -- COUNTER              (RO)
169 18 serginhofr
                                                periph_data <= counter_reg(7 downto 0) & counter_reg(15 downto 8) & counter_reg(23 downto 16) & counter_reg(31 downto 24);
170 13 serginhofr
                                        when "0110" =>          -- IRQ_COMPARE          (RW)
171 18 serginhofr
                                                periph_data <= compare_reg(7 downto 0) & compare_reg(15 downto 8) & compare_reg(23 downto 16) & compare_reg(31 downto 24);
172 13 serginhofr
                                        when "0111" =>          -- IRQ_COMPARE2         (RW)
173 18 serginhofr
                                                periph_data <= compare_reg(15 downto 8) & compare_reg(23 downto 16) & compare_reg(31 downto 24) & x"00";
174 13 serginhofr
                                        when "1000" =>          -- EXTIO_IN             (RO)
175 18 serginhofr
                                                periph_data <= extio_in & x"000000";
176 13 serginhofr
                                        when "1001" =>          -- EXTIO_OUT            (RW)
177 18 serginhofr
                                                periph_data <= extio_out_reg & x"000000";
178 13 serginhofr
                                        when "1110" =>          -- UART                 (RW)
179 18 serginhofr
                                                periph_data <= data_read_uart & x"000000";
180 13 serginhofr
                                        when "1111" =>          -- UART_DIVISOR         (RW)
181 18 serginhofr
                                                periph_data <= uart_divisor(7 downto 0) & uart_divisor(15 downto 8) & x"0000";
182 13 serginhofr
                                        when others =>
183 18 serginhofr
                                                periph_data <= data_read_mem;
184 13 serginhofr
                                end case;
185 18 serginhofr
                        end if;
186
                end if;
187 13 serginhofr
        end process;
188
 
189 18 serginhofr
        data_in_cpu <= data_read_mem when periph_access_dly = '0' else periph_data;
190 13 serginhofr
 
191
        -- peripheral register logic, write to peripheral registers
192 18 serginhofr
        process(clock, reset, counter_reg, address_cpu, data_out_cpu, periph_access, periph_access_we, irq_ack_cpu)
193 13 serginhofr
        begin
194
                if reset = '1' then
195 19 serginhofr
                        irq_vector_reg <= (others => '0');
196
                        irq_mask_reg <= (others => '0');
197
                        irq_status_reg <= (others => '0');
198
                        counter_reg <= (others => '0');
199
                        compare_reg <= (others => '0');
200 13 serginhofr
                        compare_trig <= '0';
201 19 serginhofr
                        compare2_reg <= (others => '0');
202 13 serginhofr
                        compare2_trig <= '0';
203 19 serginhofr
                        extio_out_reg <= (others => '0');
204
                        uart_divisor <= (others => '0');
205 13 serginhofr
                elsif clock'event and clock = '1' then
206
                        counter_reg <= counter_reg + 1;
207
                        if compare_reg = counter_reg then
208
                                compare_trig <= '1';
209
                        end if;
210
                        if compare2_reg = counter_reg(23 downto 0) then
211
                                compare2_trig <= '1';
212
                        end if;
213 17 serginhofr
                        if periph_access = '1' and periph_access_we = '1' then
214 18 serginhofr
                                case address_cpu(7 downto 4) is
215 17 serginhofr
                                        when "0000" =>  -- IRQ_VECTOR
216
                                                irq_vector_reg <= data_out_cpu(7 downto 0) & data_out_cpu(15 downto 8) & data_out_cpu(23 downto 16) & data_out_cpu(31 downto 24);
217
                                        when "0010" =>  -- IRQ_MASK
218 19 serginhofr
                                                irq_mask_reg <= data_out_cpu(7 downto 0) & data_out_cpu(15 downto 8) & data_out_cpu(23 downto 16) & data_out_cpu(31 downto 24);
219 17 serginhofr
                                        when "0011" =>  -- IRQ_STATUS
220
                                                irq_status_reg <= data_out_cpu(31 downto 24);
221
                                        when "0110" =>  -- IRQ_COMPARE
222
                                                compare_reg <= data_out_cpu(7 downto 0) & data_out_cpu(15 downto 8) & data_out_cpu(23 downto 16) & data_out_cpu(31 downto 24);
223
                                                compare_trig <= '0';
224
                                        when "0111" =>  -- IRQ_COMPARE2
225
                                                compare2_reg <= data_out_cpu(15 downto 8) & data_out_cpu(23 downto 16) & data_out_cpu(31 downto 24);
226
                                                compare2_trig <= '0';
227
                                        when "1001" =>  -- EXTIO_OUT
228
                                                extio_out_reg <= data_out_cpu(31 downto 24);
229
                                        when "1111" =>  -- UART_DIVISOR
230
                                                uart_divisor <= data_out_cpu(23 downto 16) & data_out_cpu(31 downto 24);
231
                                        when others =>
232
                                end case;
233
                        end if;
234
                        if irq_ack_cpu = '1' or exception_cpu = '1' then
235 13 serginhofr
                                irq_status_reg(0) <= '0';         -- IRQ_STATUS (clear master int bit on interrupt)
236
                        end if;
237
                end if;
238
        end process;
239
 
240
        -- EPC register register load on interrupts
241 18 serginhofr
        process(clock, reset, address_cpu, irq, irq_ack_cpu)
242 13 serginhofr
        begin
243
                if reset = '1' then
244
                        irq_epc_reg <= x"00000000";
245
                elsif clock'event and clock = '1' then
246
                        if ((irq = '1' and irq_ack_cpu = '0') or exception_cpu = '1') then
247 18 serginhofr
                                irq_epc_reg <= address_cpu;
248 13 serginhofr
                        end if;
249
                end if;
250
        end process;
251
 
252
        -- interrupt state machine
253
        process(clock, reset, pulse_state, interrupt, irq_status_reg, stall)
254
        begin
255
                if reset = '1' then
256
                        pulse_state <= irq_idle;
257
                        pulse_next_state <= irq_idle;
258
                        irq <= '0';
259
                elsif clock'event and clock = '1' then
260
                        if stall = '0' then
261
                                pulse_state <= pulse_next_state;
262
                                case pulse_state is
263
                                        when irq_idle =>
264 17 serginhofr
                                                if interrupt = '1' and irq_status_reg(0) = '1' then
265
                                                        pulse_next_state <= irq_int;
266 13 serginhofr
                                                end if;
267
                                        when irq_int =>
268
                                                irq <= '1';
269
                                                pulse_next_state <= irq_req;
270
                                        when irq_req =>
271
                                                if irq_ack_cpu = '1' then
272
                                                        irq <= '0';
273
                                                        pulse_next_state <= irq_ackn;
274
                                                end if;
275
                                        when irq_ackn =>
276
                                                pulse_next_state <= irq_done;
277
                                        when irq_done =>
278
                                                if irq_status_reg(0) = '1' then
279
                                                        pulse_next_state <= irq_idle;
280
                                                end if;
281
                                        when others =>
282
                                                pulse_next_state <= irq_idle;
283
                                end case;
284
                        end if;
285
                end if;
286
        end process;
287
 
288
        -- data / peripheral access delay
289 18 serginhofr
        process(clock, reset, irq_ack_cpu, periph_access, stall)
290 13 serginhofr
        begin
291
                if reset = '1' then
292 18 serginhofr
                        periph_access_dly <= '0';
293 13 serginhofr
                elsif clock'event and clock = '1' then
294
                        if stall = '0' then
295 18 serginhofr
                                periph_access_dly <= periph_access;
296 13 serginhofr
                        end if;
297
                end if;
298
        end process;
299
 
300 18 serginhofr
        periph_access <= '1' when address_cpu(31 downto 27) = "11110" and data_access_cpu = '1' else '0';
301 13 serginhofr
        periph_access_we <= '1' when periph_access <= '1' and data_w_cpu /= "0000" else '0';
302
 
303
        -- memory address / write enable muxes and cpu stall logic
304 18 serginhofr
        addr_mem <= address_cpu;
305 13 serginhofr
        data_write_mem <= data_out_cpu;
306 18 serginhofr
        data_we_mem_s <= data_w_cpu when data_access_cpu = '1' and periph_access = '0' else "0000";
307 13 serginhofr
        data_we_mem <= data_we_mem_s;
308 18 serginhofr
 
309 13 serginhofr
        stall_cpu <= stall;
310
 
311
        -- interrupts and peripherals
312
        interrupt <= '0' when (irq_cause and irq_mask_reg) = x"0000" else '1';
313 19 serginhofr
        irq_cause <= not extio_in & extio_in & x"00" & not uart_write_busy & uart_data_avail & irq_compare2 & irq_compare & irq_counter2_not & irq_counter2 & irq_counter_not & irq_counter;
314 13 serginhofr
 
315
        irq_cpu <= irq;
316
        irq_vector_cpu <= irq_vector_reg;
317
        irq_counter <= counter_reg(18);
318
        irq_counter_not <= not counter_reg(18);
319
        irq_counter2 <= counter_reg(16);
320
        irq_counter2_not <= not counter_reg(16);
321
        irq_compare <= '1' when compare_trig = '1' else '0';
322
        irq_compare2 <= '1' when compare2_trig = '1' else '0';
323
        extio_out <= extio_out_reg;
324
 
325
        write_enable <= '1' when data_we_mem_s /= "0000" else '0';
326
        data_write_uart <= data_out_cpu(31 downto 24);
327
 
328
        uart:
329
        if uart_support = "yes" generate
330 18 serginhofr
                enable_uart <= '1' when periph_access = '1' and address_cpu(7 downto 4) = "1110" else '0';
331 13 serginhofr
                enable_uart_write <= enable_uart and periph_access_we;
332
                enable_uart_read <= enable_uart and not periph_access_we;
333
 
334
                -- a simple UART
335
                serial: entity work.uart
336
                generic map (log_file => log_file)
337
                port map(
338
                        clk             => clock,
339
                        reset           => reset,
340
                        divisor         => uart_divisor(11 downto 0),
341
                        enable_read     => enable_uart_read,
342
                        enable_write    => enable_uart_write,
343
                        data_in         => data_write_uart,
344
                        data_out        => data_read_uart,
345
                        uart_read       => uart_read,
346
                        uart_write      => uart_write,
347
                        busy_write      => uart_write_busy,
348
                        data_avail      => uart_data_avail
349
                );
350
        end generate;
351
 
352
        no_uart:
353
        if uart_support = "no" generate
354
                enable_uart <= '0';
355
                data_read_uart <= (others => '0');
356
                uart_write_busy <= '0';
357
                uart_data_avail <= '0';
358
        end generate;
359
 
360
end arch;
361
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.