add: performance values for Lattice MachXO2
[zpu.git] / zpu / hdl / zealot / helpers / zpu_med1.vhdl
bloba0cbcb2348b4d492cf55a3e84f273443433b44fa
1 ------------------------------------------------------------------------------
2 ----                                                                      ----
3 ----  ZPU Medium + PHI I/O + BRAM                                         ----
4 ----                                                                      ----
5 ----  http://www.opencores.org/                                           ----
6 ----                                                                      ----
7 ----  Description:                                                        ----
8 ----  ZPU is a 32 bits small stack cpu. This is a helper that joins the   ----
9 ----  medium version, the PHI I/O basic layout and a program BRAM.        ----
10 ----                                                                      ----
11 ----  To Do:                                                              ----
12 ----  -                                                                   ----
13 ----                                                                      ----
14 ----  Author:                                                             ----
15 ----    - Salvador E. Tropea, salvador inti.gob.ar                        ----
16 ----                                                                      ----
17 ------------------------------------------------------------------------------
18 ----                                                                      ----
19 ---- Copyright (c) 2008 Salvador E. Tropea <salvador inti.gob.ar>         ----
20 ---- Copyright (c) 2008 Instituto Nacional de TecnologĂ­a Industrial       ----
21 ----                                                                      ----
22 ---- Distributed under the BSD license                                    ----
23 ----                                                                      ----
24 ------------------------------------------------------------------------------
25 ----                                                                      ----
26 ---- Design unit:      ZPU_Med1(Structural) (Entity and architecture)     ----
27 ---- File name:        zpu_med1.vhdl                                      ----
28 ---- Note:             None                                               ----
29 ---- Limitations:      None known                                         ----
30 ---- Errors:           None known                                         ----
31 ---- Library:          work                                               ----
32 ---- Dependencies:     IEEE.std_logic_1164                                ----
33 ----                   IEEE.numeric_std                                   ----
34 ----                   zpu.zpupkg                                         ----
35 ----                   work.zpu_memory                                    ----
36 ---- Target FPGA:      Spartan 3 (XC3S1500-4-FG456)                       ----
37 ---- Language:         VHDL                                               ----
38 ---- Wishbone:         No                                                 ----
39 ---- Synthesis tools:  Xilinx Release 9.2.03i - xst J.39                  ----
40 ---- Simulation tools: GHDL [Sokcho edition] (0.2x)                       ----
41 ---- Text editor:      SETEdit 0.5.x                                      ----
42 ----                                                                      ----
43 ------------------------------------------------------------------------------
45 library IEEE;
46 use IEEE.std_logic_1164.all;
47 use IEEE.numeric_std.all;
49 library zpu;
50 use zpu.zpupkg.all;
52 -- RAM declaration
53 library work;
54 use work.zpu_memory.all;
56 entity ZPU_Med1 is
57    generic(
58       WORD_SIZE  : natural:=32;  -- 32 bits data path
59       D_CARE_VAL : std_logic:='X'; -- Fill value
60       CLK_FREQ   : positive:=50; -- 50 MHz clock
61       BRATE      : positive:=9600; -- RS232 baudrate
62       ADDR_W     : natural:=18;  -- 18 bits address space=256 kB, 128 kB I/O
63       BRAM_W     : natural:=15); -- 15 bits RAM space=32 kB
64    port(
65       clk_i      : in  std_logic;  -- CPU clock
66       rst_i      : in  std_logic;  -- Reset
67       break_o    : out std_logic;  -- Break executed
68       dbg_o      : out zpu_dbgo_t; -- Debug info
69       rs232_tx_o : out std_logic;  -- UART Tx
70       rs232_rx_i : in  std_logic;  -- UART Rx
71       gpio_in    : in  std_logic_vector(31 downto 0);
72       gpio_out   : out std_logic_vector(31 downto 0);
73       gpio_dir   : out std_logic_vector(31 downto 0)  -- 1 = in, 0 = out
74       );
75 end entity ZPU_Med1;
77 architecture Structural of ZPU_Med1 is
78    constant BYTE_BITS  : integer:=WORD_SIZE/16; -- # of bits in a word that addresses bytes
79    constant IO_BIT     : integer:=ADDR_W-1; -- Address bit to determine this is an I/O
80    constant BRDIVISOR  : positive:=CLK_FREQ*1e6/BRATE/4;
82    -- I/O & memory (ZPU)
83    signal mem_busy     : std_logic;
84    signal mem_read     : unsigned(WORD_SIZE-1 downto 0);
85    signal mem_write    : unsigned(WORD_SIZE-1 downto 0);
86    signal mem_addr     : unsigned(ADDR_W-1 downto 0);
87    signal mem_we       : std_logic;
88    signal mem_re       : std_logic;
90    -- Memory (SinglePort_RAM)
91    signal ram_busy     : std_logic;
92    signal ram_read     : unsigned(WORD_SIZE-1 downto 0);
93    signal ram_addr     : unsigned(BRAM_W-1 downto BYTE_BITS);
94    signal ram_we       : std_logic;
95    signal ram_re       : std_logic;
96    signal ram_ready_r  : std_logic:='0';
98    -- I/O (ZPU_IO)
99    signal io_busy      : std_logic;
100    signal io_re        : std_logic;
101    signal io_we        : std_logic;
102    signal io_read      : unsigned(WORD_SIZE-1 downto 0);
103    signal io_ready     : std_logic;
104    signal io_reading_r : std_logic:='0';
105    signal io_addr      : unsigned(2 downto 0);
106 begin
107    memory: SinglePortRAM
108       generic map(
109          WORD_SIZE => WORD_SIZE, BYTE_BITS => BYTE_BITS, BRAM_W => BRAM_W)
110       port map(
111          clk_i => clk_i,
112          we_i => ram_we, re_i => ram_re, addr_i => ram_addr,
113          write_i => mem_write, read_o => ram_read, busy_o => ram_busy);
114    ram_addr <= mem_addr(BRAM_W-1 downto BYTE_BITS);
115    ram_we   <= mem_we and not(mem_addr(IO_BIT));
116    ram_re   <= mem_re and not(mem_addr(IO_BIT));
118    -- I/O: Phi layout
119    io_map: ZPUPhiIO
120       generic map(
121          BRDIVISOR => BRDIVISOR, 
122          LOG_FILE  => "zpu_med1_io.log"
123          )
124       port map(
125          clk_i      => clk_i, 
126          reset_i    => rst_i, 
127          busy_o     => io_busy, 
128          we_i       => io_we,
129          re_i       => io_re, 
130          data_i     => mem_write, 
131          data_o     => io_read,
132          addr_i     => io_addr, 
133          rs232_rx_i => rs232_rx_i, 
134          rs232_tx_o => rs232_tx_o,
135          br_clk_i   => '1',
136          gpio_in    => gpio_in,
137          gpio_out   => gpio_out,
138          gpio_dir   => gpio_dir
139          );
140    io_addr  <= mem_addr(4 downto 2);
141    -- Here we decode 0x8xxxx as I/O and not just 0x80A00xx
142    -- Note: We define the address space as 256 kB, so writing to 0x80A00xx
143    -- will be as wrting to 0x200xx and hence we decode it as I/O space.
144    io_we    <= mem_we and mem_addr(IO_BIT);
145    io_re    <= mem_re and mem_addr(IO_BIT);
146    io_ready <= (io_reading_r or io_re) and not io_busy;
148    zpu : ZPUMediumCore
149       generic map(
150          WORD_SIZE => WORD_SIZE, ADDR_W => ADDR_W, MEM_W => BRAM_W,
151          D_CARE_VAL => D_CARE_VAL)
152       port map(
153          clk_i => clk_i, reset_i => rst_i, enable_i => '1',
154          break_o => break_o, dbg_o => dbg_o,
155          -- Memory
156          mem_busy_i => mem_busy, data_i => mem_read, data_o => mem_write,
157          addr_o => mem_addr, write_en_o => mem_we, read_en_o => mem_re);
158    mem_busy <= io_busy or ram_busy;
160    -- Memory reads either come from IO or DRAM. We need to pick the right one.
161    memory_control:
162    process (ram_read, ram_ready_r, io_ready, io_read)
163    begin
164       mem_read <= (others => '0');
165       if ram_ready_r='1' then
166          mem_read <= ram_read;
167       end if;
168       if io_ready='1' then
169          mem_read <= io_read;
170       end if;
171    end process memory_control;
173    memory_control_sync:
174    process (clk_i)
175    begin
176       if rising_edge(clk_i) then
177          if rst_i='1' then
178             io_reading_r <= '0';
179             ram_ready_r  <= '0';
180          else
181             io_reading_r <= io_busy or io_re;
182             ram_ready_r  <= ram_re;
183          end if;
184       end if;
185    end process memory_control_sync;
186 end architecture Structural; -- Entity: ZPU_Med1