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

Subversion Repositories hf-risc

[/] [hf-risc/] [trunk/] [hf-risc/] [ucore/] [peripherals_busmux.vhd] - Blame information for rev 13

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 13 serginhofr
-- HF-RISC v3.3
2
-- Sergio Johann Filho, 2011 - 2016
3
--
4
-- *This is a quick and dirty organization of a 3-stage pipelined MIPS 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 absolutely *needed* MIPS-I opcodes are implemented. This core was implemented with the C programming
18
--  language in mind, so opcodes which cause overflows on integer operations (add, addi, sub) were not included
19
--  for obvious reasons.
20
-- *Memory is accessed in big endian mode.
21
-- *No unaligned loads/stores.
22
-- *No co-processor is implemented and all peripherals are memory mapped.
23
-- *Loads and stores take 2/1 cycles with separated code/data memories and 3 cycles otherwise. This version is organized
24
--  as a Von Neumann machine, so there is only one memory interface that is shared betweeen code and data accesses.
25
--  No load delay slots are needed in code.
26
-- *Branches have a 1 cycle delay (not taken) or 3 cycle dalay (taken), including two branch delay slots.
27
--  This is a side effect of the pipeline refill and memory access policy. All other instructions are single
28
--  cycle. The first delay slot can be filled with an instruction, reducing the cost to 2 cycles. The
29
--  second delay slot is completely useless and the instruction in this slot is discarded. No branch predictor
30
--  is implemented (default branch target is 'not taken'). Minor modifications in the datapath can turn the second
31
--  branch delay slot usable, but the current toolchain isn't compatible with this behavior, so a bubble is inserted.
32
-- *Interrupts are handled using VECTOR, CAUSE, MASK, STATUS and EPC registers. The VECTOR register is used to hold
33
--  the address of the default (non-vectored) interrupt handler. The CAUSE register is read only and peripheral
34
--  interrupt lines are connected to this register. The MASK register is read/write and holds the interrupt mask
35
--  for the CAUSE register. The interrupt STATUS register is automatically cleared on interrupts, and is set by
36
--  software when returning from interrupts - this works as a global interrupt enable/disable flag. This register is
37
--  read and write capable, so it can also be cleared by software. Setting this register on return from interrupts
38
--  (normally in the branch delay slot) re-enables interrupts. The EPC register holds the program counter when
39
--  the processor is interrupted (we should re-execute the last instruction (EPC-4), as it was not commited yet).
40
--  EPC is a read only register, and is used to return from an interrupt using simple LW / JR --  instructions.
41
--  As an interrupt is accepted, the processor jumps to VECTOR address where the first level of irq handling is
42
--  done. A second level handler (in C) implements the interrupt priority mechanism and calls the appropriate
43
--  ISR for each interrupt.
44
-- *Built in peripherals: running counter (32 bit), two counter comparators (32 and 24 bit), I/O ports and UART. the
45
--  UART baud rate is defined in a 16 bit divisor register. Two counter bits (bits 18 and 16 and their complements) are
46
--  tied to interrupt lines, so are the two counter comparators and the UART.
47
--
48
-- *Compiler:
49
--  Patched GCC version 4.9.3.
50
--  Mandatory gcc options are: -mips1(**) -mpatfree -mfix-r4000 -mno-check-zero-division -msoft-float -fshort-double
51
--               -nostdinc -fno-builtin -fomit-frame-pointer -G 0 -mnohwmult -mnohwdiv -ffixed-lo -ffixed-hi
52
--
53
-- (**) "-mips2 -mno-branch-likely" can be used instead of "-mips1". the result is similar to the code generated
54
--      with "-mips1", but no useless nops are inserted after loads on data hazards (load delay slots).
55
--
56
-- *The following instructions from the MIPS I instruction set were implemented (41 opcodes):
57
--     Arithmetic Instructions: addiu, addu, subu
58
--     Logic Instructions: and, andi, nor, or, ori, xor, xori
59
--     Shift Instructions: sll, sra, srl, sllv, srav, srlv
60
--     Comparison Instructions: slt, sltu, slti, sltiu
61
--     Load/Store Instructions: lui, lb, lbu, lh, lhu, lw, sb, sh, sw
62
--     Branch Instructions: beq, bne, bgez, bgezal, bgtz, blez, bltzal, bltz
63
--     Jump Instructions: j, jal, jr, jalr
64
--
65
--
66
-- Memory map:
67
--
68
-- ROM                                  0x00000000 - 0x1fffffff (512MB)
69
-- System                               0x20000000 - 0x3fffffff (512MB)
70
-- SRAM                                 0x40000000 - 0x5fffffff (512MB)
71
-- External RAM / device                0x60000000 - 0x9fffffff (1GB)
72
-- External RAM / device                0xa0000000 - 0xdfffffff (1GB)           (uncached)
73
-- External Peripheral                  0xe0000000 - 0xefffffff (256MB)         (uncached)
74
-- Peripheral (core)                    0xf0000000 - 0xf7ffffff (128MB)         (uncached)
75
-- Peripheral (extended)                0xf8000000 - 0xffffffff (128MB)         (uncached)
76
--
77
--   IRQ_VECTOR                 0xf0000000
78
--   IRQ_CAUSE                  0xf0000010
79
--   IRQ_MASK                   0xf0000020
80
--   IRQ_STATUS                 0xf0000030
81
--   IRQ_EPC                    0xf0000040
82
--   COUNTER                    0xf0000050
83
--   COMPARE                    0xf0000060
84
--   COMPARE2                   0xf0000070
85
--   EXTIO_IN                   0xf0000080
86
--   EXTIO_OUT                  0xf0000090
87
--   DEBUG                      0xf00000d0
88
--   UART_WRITE / UART_READ     0xf00000e0
89
--   UART_DIVISOR               0xf00000f0
90
--
91
-- Interrupt masks:
92
--
93
-- IRQ_COUNTER                  0x0001          (bit 18 of the counter is set)
94
-- IRQ_COUNTER_NOT              0x0002          (bit 18 of the counter is clear)
95
-- IRQ_COUNTER2                 0x0004          (bit 16 of the counter is set)
96
-- IRQ_COUNTER2_NOT             0x0008          (bit 16 of the counter is clear)
97
-- IRQ_COMPARE                  0x0010          (counter is equal to compare, clears irq when updated)
98
-- IRQ_COMPARE2                 0x0020          (counter bits 23 to 0 are equal to compare2, clears irq when updated)
99
-- IRQ_UART_READ_AVAILABLE      0x0040          (there is data available for reading on the UART)
100
-- IRQ_UART_WRITE_AVAILABLE     0x0080          (UART is not busy)
101
-- EXT_IRQ0                     0x0100          (external interrupts on extio_in, 'high' level triggered)
102
-- EXT_IRQ1                     0x0200
103
-- EXT_IRQ2                     0x0400
104
-- EXT_IRQ3                     0x0800
105
-- EXT_IRQ4                     0x1000
106
-- EXT_IRQ5                     0x2000
107
-- EXT_IRQ6                     0x4000
108
-- EXT_IRQ7                     0x8000
109
 
110
library ieee;
111
use ieee.std_logic_1164.all;
112
use ieee.std_logic_unsigned.all;
113
use ieee.std_logic_arith.all;
114
 
115
entity busmux is
116
        generic(
117
                log_file: string := "UNUSED";                   -- options are "out.txt" and "UNUSED"
118
                uart_support: string := "no"                    -- options are "yes" and "no".
119
        );
120
        port (  clock:          in std_logic;
121
                reset:          in std_logic;
122
 
123
                stall:          in std_logic;
124
 
125
                stall_cpu:      out std_logic;
126
                busy_cpu:       out std_logic;
127
                irq_vector_cpu: out std_logic_vector(31 downto 0);
128
                irq_cpu:        out std_logic;
129
                irq_ack_cpu:    in std_logic;
130
                inst_addr_cpu:  in std_logic_vector(31 downto 0);
131
                inst_in_cpu:    out std_logic_vector(31 downto 0);
132
                data_addr_cpu:  in std_logic_vector(31 downto 0);
133
                data_in_cpu:    out std_logic_vector(31 downto 0);
134
                data_out_cpu:   in std_logic_vector(31 downto 0);
135
                data_w_cpu:     in std_logic_vector(3 downto 0);
136
                data_access_cpu:        in std_logic;
137
 
138
                addr_mem:       out std_logic_vector(31 downto 0);
139
                data_read_mem:  in std_logic_vector(31 downto 0);
140
                data_write_mem: out std_logic_vector(31 downto 0);
141
                data_we_mem:    out std_logic_vector(3 downto 0);
142
 
143
                extio_in:       in std_logic_vector(7 downto 0);
144
                extio_out:      out std_logic_vector(7 downto 0);
145
                uart_read:      in std_logic;
146
                uart_write:     out std_logic
147
        );
148
end busmux;
149
 
150
architecture arch of busmux is
151
        signal write_enable: std_logic;
152
        signal irq_cause, irq_mask_reg, uart_divisor: std_logic_vector(15 downto 0);
153
        signal irq_status_reg, extio_out_reg: std_logic_vector(7 downto 0);
154
        signal irq_vector_reg, irq_epc_reg, compare_reg, counter_reg: std_logic_vector(31 downto 0);
155
        signal compare2_reg: std_logic_vector(23 downto 0);
156
        signal interrupt, irq, irq_counter, irq_counter_not, irq_counter2, irq_counter2_not, irq_compare, irq_compare2, compare_trig, compare2_trig: std_logic;
157
        signal data_read_uart, data_write_uart: std_logic_vector(7 downto 0);
158
        signal enable_uart, enable_uart_read, enable_uart_write, uart_write_busy, uart_data_avail: std_logic;
159
 
160
        type pulse_state_type is (irq_idle, irq_intdly1, irq_intdly2, irq_int, irq_req, irq_ackn, irq_done);
161
        signal pulse_state: pulse_state_type;
162
        signal pulse_next_state: pulse_state_type;
163
 
164
        signal periph_access, periph_access_we, data_access_cpu_dly, data_access_cpu_dly2: std_logic;
165
        signal data_we_mem_s: std_logic_vector(3 downto 0);
166
 
167
begin
168
        -- address decoder, read from peripheral registers
169
        process(data_addr_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)
170
        begin
171
                case data_addr_cpu(31 downto 27) is
172
                        when "11110" =>                         -- Peripherals     (f000 0000 - f7ff ffff)
173
                                case data_addr_cpu(7 downto 4) is
174
                                        when "0000" =>          -- IRQ_VECTOR           (RW)
175
                                                data_in_cpu <= irq_vector_reg;
176
                                        when "0001" =>          -- IRQ_CAUSE            (RO)
177
                                                data_in_cpu <= x"0000" & irq_cause;
178
                                        when "0010" =>          -- IRQ_MASK             (RW)
179
                                                data_in_cpu <= x"0000" & irq_mask_reg;
180
                                        when "0011" =>          -- IRQ_STATUS           (RW)
181
                                                data_in_cpu <= x"000000" & irq_status_reg;
182
                                        when "0100" =>          -- IRQ_EPC              (RO)
183
                                                data_in_cpu <= irq_epc_reg;
184
                                        when "0101" =>          -- COUNTER              (RO)
185
                                                data_in_cpu <= counter_reg;
186
                                        when "0110" =>          -- IRQ_COMPARE          (RW)
187
                                                data_in_cpu <= compare_reg;
188
                                        when "0111" =>          -- IRQ_COMPARE2         (RW)
189
                                                data_in_cpu <= x"00" & compare2_reg;
190
                                        when "1000" =>          -- EXTIO_IN             (RO)
191
                                                data_in_cpu <= x"000000" & extio_in;
192
                                        when "1001" =>          -- EXTIO_OUT            (RW)
193
                                                data_in_cpu <= x"000000" & extio_out_reg;
194
                                        when "1110" =>          -- UART                 (RW)
195
                                                data_in_cpu <= x"000000" & data_read_uart;
196
                                        when "1111" =>          -- UART_DIVISOR         (RW)
197
                                                data_in_cpu <= x"0000" & uart_divisor;
198
                                        when others =>
199
                                                data_in_cpu <= data_read_mem;
200
                                end case;
201
                        when others =>                          -- ROM / RAM area, external peripherals (f800 0000 - ffff fffc)
202
                                data_in_cpu <= data_read_mem;
203
                end case;
204
        end process;
205
 
206
        inst_in_cpu <= data_read_mem;
207
 
208
        -- peripheral register logic, write to peripheral registers
209
        process(clock, reset, counter_reg, data_addr_cpu, data_out_cpu, periph_access, periph_access_we, irq)
210
        begin
211
                if reset = '1' then
212
                        irq_vector_reg <= x"00000000";
213
                        irq_mask_reg <= x"0000";
214
                        irq_status_reg <= x"00";
215
                        counter_reg <= x"00000000";
216
                        compare_reg <= x"00000000";
217
                        compare_trig <= '0';
218
                        compare2_reg <= x"000000";
219
                        compare2_trig <= '0';
220
                        extio_out_reg <= x"00";
221
                        uart_divisor <= x"0000";
222
                elsif clock'event and clock = '1' then
223
                        counter_reg <= counter_reg + 1;
224
                        if compare_reg = counter_reg then
225
                                compare_trig <= '1';
226
                        end if;
227
                        if compare2_reg = counter_reg(23 downto 0) then
228
                                compare2_trig <= '1';
229
                        end if;
230
                        if irq = '0' then
231
                                if periph_access = '1' and periph_access_we = '1' then
232
                                        case data_addr_cpu(7 downto 4) is
233
                                                when "0000" =>  -- IRQ_VECTOR
234
                                                        irq_vector_reg <= data_out_cpu;
235
                                                when "0010" =>  -- IRQ_MASK
236
                                                        irq_mask_reg <= data_out_cpu(15 downto 0);
237
                                                when "0011" =>  -- IRQ_STATUS
238
                                                        irq_status_reg <= data_out_cpu(7 downto 0);
239
                                                when "0110" =>  -- IRQ_COMPARE
240
                                                        compare_reg <= data_out_cpu;
241
                                                        compare_trig <= '0';
242
                                                when "0111" =>  -- IRQ_COMPARE2
243
                                                        compare2_reg <= data_out_cpu(23 downto 0);
244
                                                        compare2_trig <= '0';
245
                                                when "1001" =>  -- EXTIO_OUT
246
                                                        extio_out_reg <= data_out_cpu(7 downto 0);
247
                                                when "1111" =>  -- UART_DIVISOR
248
                                                        uart_divisor <= data_out_cpu(15 downto 0);
249
                                                when others =>
250
                                        end case;
251
                                end if;
252
                        else
253
                                irq_status_reg(0) <= '0';         -- IRQ_STATUS (clear master int bit on interrupt)
254
                        end if;
255
                end if;
256
        end process;
257
 
258
        -- EPC register register load on interrupts
259
        process(clock, reset, inst_addr_cpu, irq)
260
        begin
261
                if reset = '1' then
262
                        irq_epc_reg <= x"00000000";
263
                elsif clock'event and clock = '1' then
264
                        if irq = '1' and irq_ack_cpu = '0' then
265
                                irq_epc_reg <= inst_addr_cpu;
266
                        end if;
267
                end if;
268
        end process;
269
 
270
        -- interrupt state machine
271
        process(clock, reset, pulse_state, interrupt, irq_status_reg, stall)
272
        begin
273
                if reset = '1' then
274
                        pulse_state <= irq_idle;
275
                        pulse_next_state <= irq_idle;
276
                        irq <= '0';
277
                elsif clock'event and clock = '1' then
278
                        if stall = '0' then
279
                                pulse_state <= pulse_next_state;
280
                                case pulse_state is
281
                                        when irq_idle =>
282
                                                if interrupt = '1' then
283
                                                        pulse_next_state <= irq_intdly1;
284
                                                end if;
285
                                        when irq_intdly1 =>
286
                                                if irq_status_reg(0) = '1' then
287
                                                        pulse_next_state <= irq_intdly2;
288
                                                end if;
289
                                        when irq_intdly2 =>
290
                                                pulse_next_state <= irq_int;
291
                                        when irq_int =>
292
                                                irq <= '1';
293
                                                pulse_next_state <= irq_req;
294
                                        when irq_req =>
295
                                                if irq_ack_cpu = '1' then
296
                                                        irq <= '0';
297
                                                        pulse_next_state <= irq_ackn;
298
                                                end if;
299
                                        when irq_ackn =>
300
                                                pulse_next_state <= irq_done;
301
                                        when irq_done =>
302
                                                if irq_status_reg(0) = '1' then
303
                                                        pulse_next_state <= irq_idle;
304
                                                end if;
305
                                        when others =>
306
                                                pulse_next_state <= irq_idle;
307
                                end case;
308
                        end if;
309
                end if;
310
        end process;
311
 
312
        -- data / peripheral access delay
313
        process(clock, reset, irq_ack_cpu, data_access_cpu, stall)
314
        begin
315
                if reset = '1' then
316
                        data_access_cpu_dly <= '0';
317
                        data_access_cpu_dly2 <= '0';
318
                elsif clock'event and clock = '1' then
319
                        if stall = '0' then
320
                                data_access_cpu_dly2 <= data_access_cpu_dly;
321
                                if data_access_cpu = '1' and data_access_cpu_dly = '0' and data_access_cpu_dly2 = '0' then
322
                                        data_access_cpu_dly <= '1';
323
                                else
324
                                        data_access_cpu_dly <= '0';
325
                                end if;
326
                        end if;
327
                end if;
328
        end process;
329
 
330
        periph_access <= '1' when data_addr_cpu(31 downto 27) = "11110" and data_access_cpu = '1' else '0';
331
        periph_access_we <= '1' when periph_access <= '1' and data_w_cpu /= "0000" else '0';
332
 
333
        -- memory address / write enable muxes and cpu stall logic
334
        addr_mem <= data_addr_cpu when data_access_cpu_dly = '0' and data_access_cpu = '1' and periph_access = '0' else inst_addr_cpu;
335
        data_write_mem <= data_out_cpu;
336
        data_we_mem_s <= data_w_cpu when data_access_cpu_dly = '0' and data_access_cpu = '1' and periph_access = '0' else "0000";
337
        data_we_mem <= data_we_mem_s;
338
 
339
        busy_cpu <= (data_access_cpu and not data_access_cpu_dly);                                      -- load/store: 1 wait cycle
340
        stall_cpu <= stall;
341
 
342
        -- interrupts and peripherals
343
        interrupt <= '0' when (irq_cause and irq_mask_reg) = x"0000" else '1';
344
        irq_cause <= extio_in & not uart_write_busy & uart_data_avail & irq_compare2 & irq_compare & irq_counter2_not & irq_counter2 & irq_counter_not & irq_counter;
345
 
346
        irq_cpu <= irq;
347
        irq_vector_cpu <= irq_vector_reg;
348
        irq_counter <= counter_reg(18);
349
        irq_counter_not <= not counter_reg(18);
350
        irq_counter2 <= counter_reg(16);
351
        irq_counter2_not <= not counter_reg(16);
352
        irq_compare <= '1' when compare_trig = '1' else '0';
353
        irq_compare2 <= '1' when compare2_trig = '1' else '0';
354
        extio_out <= extio_out_reg;
355
 
356
        write_enable <= '1' when data_we_mem_s /= "0000" else '0';
357
        data_write_uart <= data_out_cpu(7 downto 0);
358
 
359
        uart:
360
        if uart_support = "yes" generate
361
                enable_uart <= '1' when periph_access = '1' and data_addr_cpu(7 downto 4) = "1110" else '0';
362
                enable_uart_write <= enable_uart and periph_access_we;
363
                enable_uart_read <= enable_uart and not periph_access_we;
364
 
365
                -- a simple UART
366
                serial: entity work.uart
367
                generic map (log_file => log_file)
368
                port map(
369
                        clk             => clock,
370
                        reset           => reset,
371
                        divisor         => uart_divisor(11 downto 0),
372
                        enable_read     => enable_uart_read,
373
                        enable_write    => enable_uart_write,
374
                        data_in         => data_write_uart,
375
                        data_out        => data_read_uart,
376
                        uart_read       => uart_read,
377
                        uart_write      => uart_write,
378
                        busy_write      => uart_write_busy,
379
                        data_avail      => uart_data_avail
380
                );
381
        end generate;
382
 
383
        no_uart:
384
        if uart_support = "no" generate
385
                enable_uart <= '0';
386
                data_read_uart <= (others => '0');
387
                uart_write_busy <= '0';
388
                uart_data_avail <= '0';
389
        end generate;
390
 
391
end arch;
392
 

powered by: WebSVN 2.1.0

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