OpenCores
URL https://opencores.org/ocsvn/tinycpu/tinycpu/trunk

Subversion Repositories tinycpu

[/] [tinycpu/] [trunk/] [src/] [memory.vhd] - Blame information for rev 37

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

Line No. Rev Author Line
1 18 earlz
--Memory management component
2
--By having this separate, it should be fairly easy to add RAMs or ROMs later
3
--This basically lets the CPU not have to worry about how memory "Really" works
4
--currently just one RAM. 1024 byte blockram.vhd mapped as 0 - 1023
5 6 earlz
 
6 4 earlz
library IEEE;
7
use IEEE.STD_LOGIC_1164.ALL;
8
use IEEE.NUMERIC_STD.ALL;
9
 
10 6 earlz
 
11
 
12 4 earlz
entity memory is
13
  port(
14 18 earlz
    Address: in std_logic_vector(15 downto 0); --memory address (in bytes)
15
    WriteWord: in std_logic; --if set, will write a full 16-bit word instead of a byte. Address must be aligned to 16-bit address. (bottom bit must be 0)
16
    WriteEnable: in std_logic;
17 4 earlz
    Clock: in std_logic;
18
    DataIn: in std_logic_vector(15 downto 0);
19 37 earlz
    DataOut: out std_logic_vector(15 downto 0);
20
 
21
    Port0: inout std_logic_vector(7 downto 0)
22 18 earlz
--    Reset: in std_logic
23
 
24
    --RAM/ROM interface (RAMA is built in to here
25
    --RAMBDataIn: out std_logic_vector(15 downto 0);
26
    --RAMBDataOut: in std_logic_vector(15 downto 0);
27
    --RAMBAddress: out std_logic_vector(15 downto 0);
28
    --RAMBWriteEnable: out std_logic_vector(1 downto 0);
29 4 earlz
  );
30
end memory;
31
 
32
architecture Behavioral of memory is
33 18 earlz
 
34
  component blockram
35
    port(
36
      Address: in std_logic_vector(7 downto 0); --memory address
37
      WriteEnable: in std_logic_vector(1 downto 0); --write or read
38
      Enable: in std_logic;
39
      Clock: in std_logic;
40
      DataIn: in std_logic_vector(15 downto 0);
41
      DataOut: out std_logic_vector(15 downto 0)
42
    );
43
  end component;
44
 
45 37 earlz
  constant R1START: integer := 15;
46
  constant R1END: integer := 1023+15;
47 18 earlz
  signal addr: std_logic_vector(15 downto 0) := (others => '0');
48
  signal R1addr: std_logic_vector(7 downto 0);
49
  signal we: std_logic_vector(1 downto 0);
50
  signal datawrite: std_logic_vector(15 downto 0);
51
  signal dataread: std_logic_vector(15 downto 0);
52
  --signal en: std_logic;
53
  signal R1we: std_logic_vector(1 downto 0);
54
  signal R1en: std_logic;
55
  signal R1in: std_logic_vector(15 downto 0);
56
  signal R1out: std_logic_vector(15 downto 0);
57 37 earlz
 
58
  signal port0we: std_logic_vector(7 downto 0);
59
  signal port0temp: std_logic_vector(7 downto 0);
60 4 earlz
begin
61 18 earlz
  R1: blockram port map (R1addr, R1we, R1en, Clock, R1in, R1out);
62
  addrwe: process(Address, WriteWord, WriteEnable, DataIn)
63 4 earlz
  begin
64 18 earlz
    addr <= Address(15 downto 1) & '0';
65
    if WriteEnable='1' then
66
      if WriteWord='1' then
67
        we <= "11";
68
        datawrite <= DataIn;
69
      else
70
        if Address(0)='0' then
71
          we <= "01";
72
          datawrite <= x"00" & DataIn(7 downto 0); --not really necessary
73
        else
74
          we <= "10";
75
          datawrite <= DataIn(7 downto 0) & x"00";
76 4 earlz
        end if;
77
      end if;
78 18 earlz
    else
79 19 earlz
      datawrite <= x"0000";
80 18 earlz
      we <= "00";
81 4 earlz
    end if;
82
  end process;
83 18 earlz
 
84 37 earlz
  assignram: process (we, datawrite, addr, r1out, port0, WriteEnable, Address)
85 18 earlz
  variable tmp: integer;
86 37 earlz
  variable tmp2: integer;
87 18 earlz
  variable found: boolean := false;
88 4 earlz
  begin
89 18 earlz
    tmp := to_integer(unsigned(addr));
90 37 earlz
    tmp2 := to_integer(unsigned(Address));
91
    if tmp2 <= 15 then --internal registers/mapped IO
92
      if WriteWord='0' then
93
        if tmp2=0 then
94
          dataread <= x"0000";
95
          gen: for I in 0 to 7 loop
96
            if WriteEnable='1' then
97
              if port0we(I)='1' then --1-bit port set to WRITE mode
98
                port0(I) <= DataIn(I);
99
                port0temp(I) <= DataIn(I);
100
              else
101
                port0(I) <= 'Z';
102
              end if;
103
            else --not WE
104
              if port0we(I)='0' then --1-bit-port set to READ mode
105
                dataread(I) <= port0(I);
106
              else
107
                dataread(I) <= port0temp(I);
108
              end if;
109
            end if;
110
          end loop gen;
111
        elsif tmp2=1 then
112
          dataread <= x"00" & port0we;
113
          if WriteEnable='1' then
114
            port0we <= DataIn(7 downto 0);
115
            setwe: for I in 0 to 7 loop
116
              if DataIn(I)='0' then
117
                port0(I) <= 'Z';
118
              end if;
119
            end loop setwe;
120
          else
121
            dataread <= x"00" & port0we;
122
          end if;
123
        else
124
          --synthesis off
125
          report "Memory address is outside of bounds of RAM and registers" severity warning;
126
          --synthesis on
127
        end if;
128
      else
129
        --synthesis off
130
        report "WriteWord is not allowed in register area. Ignoring access" severity warning;
131
        --synthesis on
132
      end if;
133
    elsif tmp >= R1START and tmp <= R1END then --RAM bank1
134 18 earlz
      --map all to R1
135
      found := true;
136
      R1en <= '1';
137
      R1we <= we;
138
      R1in <= datawrite;
139
      dataread <= R1out;
140
      R1addr <= addr(8 downto 1);
141
    else
142
      R1en <= '0';
143
      R1we <= "00";
144
      R1in <= x"0000";
145
      R1addr <= x"00";
146
      dataread <= x"0000";
147 4 earlz
    end if;
148
  end process;
149 18 earlz
 
150
  readdata: process(Address, dataread)
151
  begin
152
    if Address(0) = '0' then
153
      DataOut <= dataread;
154
    else
155
      DataOut <= x"00" & dataread(15 downto 8);
156
    end if;
157
  end process;
158 4 earlz
end Behavioral;

powered by: WebSVN 2.1.0

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