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

Subversion Repositories uos_processor

[/] [vhdl/] [cpu.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 droggen
 
2
 
3
----------------------------------------------------------------------------------
4
library IEEE;
5
use IEEE.STD_LOGIC_1164.ALL;
6
use IEEE.STD_LOGIC_UNSIGNED.ALL;
7
 
8
 
9
entity cpu is
10
        generic(N : integer);
11
        port(
12
                        clk : in STD_LOGIC;
13
                        rst : in STD_LOGIC;
14
                        ext_in : in STD_LOGIC_VECTOR(7 downto 0);
15
                        ext_out : out STD_LOGIC_VECTOR(7 downto 0);
16
                        ram_we : out STD_LOGIC;
17
                        ram_address : out STD_LOGIC_VECTOR(N-1 downto 0);
18
                        ram_datawr : out STD_LOGIC_VECTOR(7 downto 0);
19
                        ram_datard : in STD_LOGIC_VECTOR(7 downto 0);
20
                        -- Only for debugging
21
                        dbg_qa : out STD_LOGIC_VECTOR(7 downto 0);
22
                        dbg_qb : out STD_LOGIC_VECTOR(7 downto 0);
23
                        dbg_qc : out STD_LOGIC_VECTOR(7 downto 0);
24
                        dbg_qd : out STD_LOGIC_VECTOR(7 downto 0);
25
                        dbg_instr : out STD_LOGIC_VECTOR(15 downto 0);
26
                        dbg_seq : out STD_LOGIC_VECTOR(1 downto 0);
27
                        dbg_flags : out STD_LOGIC_VECTOR(3 downto 0)
28
                );
29
end cpu;
30
 
31
architecture Behavioral of cpu is
32
 
33
 
34
        -- Instruction
35
        signal instruction : STD_LOGIC_VECTOR(15 downto 0);
36
 
37
        -- Helper
38
        signal source : STD_LOGIC_VECTOR(7 downto 0);
39
        signal wrdata : STD_LOGIC_VECTOR(7 downto 0);
40
 
41
        -- register bank
42
        signal regwren : STD_LOGIC;
43
        signal reg1out : STD_LOGIC_VECTOR(7 downto 0);
44
        signal reg2out : STD_LOGIC_VECTOR(7 downto 0);
45
 
46
        -- flags
47
        signal flagwren : STD_LOGIC;
48
        signal flags : STD_LOGIC_VECTOR(3 downto 0);     --  zf, ovf, cf, sf
49
        signal zf,ovf,cf,sf : STD_LOGIC;
50
 
51
        -- fetch/execute signals
52
        signal seq : STD_LOGIC_VECTOR(1 downto 0);
53
        signal execute,fetch,fetchh,fetchl : STD_LOGIC;
54
 
55
        -- Instruction pointer
56
        signal ip: STD_LOGIC_VECTOR(N-1 downto 0);
57
        signal ipnext: STD_LOGIC_VECTOR(N-1 downto 0);
58
 
59
        -- ALU input and output signals
60
        signal aluqout: STD_LOGIC_VECTOR(7 downto 0);
61
        signal alufout : STD_LOGIC_VECTOR(3 downto 0);
62
 
63
        -- Jumps
64
        signal jump : STD_LOGIC;
65
        signal jumpip: STD_LOGIC_VECTOR(N-1 downto 0);
66
        signal jumpconditionvalid : STD_LOGIC;
67
 
68
        -- External interface
69
        signal ext_wren : STD_LOGIC;
70
 
71
        -- Debug signals
72
        --signal tdbg_qa,tdbg_qb,tdbg_qc,tdbg_qd : STD_LOGIC_VECTOR(7 downto 0);
73
 
74
begin
75
 
76
 
77
 
78
        ---------------------------------------------------------------------------------------
79
        -- Fetch/Execute -- Fetch/Execute -- Fetch/Execute -- Fetch/Execute -- Fetch/Execute --
80
        ---------------------------------------------------------------------------------------
81
        -- Instantiate a fetch/exec sequencer. Seq is 00 for load1, 01 for load2, 10 for execute
82
        comp_seq: entity work.cpusequencer port map(clk=>clk,rst=>rst,en=>'1',seq=>seq);
83
        -- Binary to one hot
84
        fetchh <=       '1' when seq="00" else
85
                                        '0';
86
        fetchl <=       '1' when seq="01" else
87
                                        '0';
88
        execute <=  '1' when seq="10" else
89
                                        '0';
90
        fetch <= fetchl or fetchh;
91
        -- Instantiate two 8-bit registers to store the 16-bit instruction during the fetchl and fetchh cycles.
92
        comp_instrh: entity work.dffre generic map(N=>8) port map(clk=>clk,rst=>rst,en=>fetchh,d=>ram_datard,q=>instruction(15 downto 8));
93
        comp_instrl: entity work.dffre generic map(N=>8) port map(clk=>clk,rst=>rst,en=>fetchl,d=>ram_datard,q=>instruction(7 downto 0));
94
 
95
 
96
        ---------------------------------------------------------------------------------------
97
        -- Instruction pointer
98
        ---------------------------------------------------------------------------------------
99
        comp_ip: entity work.dffre generic map(N=>N) port map(clk=>clk,rst=>rst,en=>'1',d=>ipnext,q=>ip);
100
 
101
        ipnext <= ip+1 when fetch='1' else
102
                                 ip when jump='0' else
103
                                 jumpip;
104
 
105
 
106
 
107
        ---------------------------------------------------------------------------------------
108
        -- Register bank -- Register bank -- Register bank -- Register bank -- Register bank --
109
        ---------------------------------------------------------------------------------------
110
        -- Instantiate the register bank
111
        -- Always map the register 1 and register 2 to the source and destination registers in the instruction fields
112
        -- Always map the write register to destination register in instruction field.
113
        -- Always map the write input to wrdata
114
        comp_regs: entity work.cpuregbank port map(clk=>clk,rrd1=>instruction(9 downto 8),rrd2=>instruction(1 downto 0),rwr=>instruction(9 downto 8),rwren=>regwren,rst=>rst,d=>wrdata,q1=>reg1out,q2=>reg2out,
115
                                                                dbg_qa=>dbg_qa,dbg_qb=>dbg_qb,dbg_qc=>dbg_qc,dbg_qd=>dbg_qd);
116
 
117
        -- Write to register for move instructions with direct destination, or ALU instructions except cmp.
118
        regwren <=              '1' when execute='1' and instruction(15 downto 13) = "000" and instruction(11)='0' else                                          -- opcode 000 (move)
119
                                                '1' when execute='1' and instruction(15 downto 13) = "001" else                                                                                                         -- opcode 001 (add,sub,and,or)
120
                                                '1' when execute='1' and instruction(15 downto 13) = "010" and instruction(11 downto 10) /= "01" else           -- opcode 010 (all except cmp)
121
                                                '1' when execute='1' and instruction(15 downto 13) = "011" else                                                                                                         -- opcode 011
122
                                                '1' when execute='1' and instruction(15 downto 13) = "110" and instruction(11 downto 10) = "01" else            -- opcode 110 (io)
123
                                                '0';
124
 
125
 
126
        --------------------------------------------------------------------------------------------
127
        -- Helper -- Helper -- Helper -- Helper -- Helper -- Helper -- Helper -- Helper -- Helper --
128
        --------------------------------------------------------------------------------------------
129
        -- Almost all instructions using a source have register or immediate mode. We 
130
        source <= reg2out when instruction(12)='0' else
131
                                 instruction(7 downto 0);
132
 
133
 
134
        -------------------------------------------------------------------------------
135
        -- ALU -- ALU -- ALU -- ALU -- ALU -- ALU -- ALU -- ALU -- ALU -- ALU -- ALU --
136
        -------------------------------------------------------------------------------
137
        -- Instantiate ALU
138
        comp_alu: entity work.cpualu port map(clk=>clk,rst=>rst,op=>instruction(14 downto 10),a=>reg1out,b=>source,q=>aluqout,f=>alufout);
139
 
140
        -----------------------------------------------------------------------------------
141
        -- Flags -- Flags -- Flags -- Flags -- Flags -- Flags -- Flags -- Flags -- Flags --
142
        -----------------------------------------------------------------------------------
143
        -- instantiate register to store the flags
144
        comp_flags: entity work.dffre generic map(N=>4) port map(clk=>clk,rst=>rst,en=>flagwren,d=>alufout,q=>flags);
145
        -- When to write the flags: execute phase and compare instruction
146
        flagwren <= '1' when execute='1' and instruction(15 downto 13)="010" and instruction(11 downto 10)="01"
147
                                        else '0';
148
        -- Individual signals for each flag
149
        zf <= flags(3);
150
        ovf <= flags(2);
151
        cf <= flags(1);
152
        sf <= flags(0);
153
 
154
        -----------------------------------------------------------------------------------
155
        -- Jump -- Jump -- Jump -- Jump -- Jump -- Jump -- Jump -- Jump -- Jump -- Jump -- 
156
        -----------------------------------------------------------------------------------
157
        -- Jump destinatinon is register or immediate
158
        jumpip <=       source(N-1 downto 0);
159
        -- Do jump when the instruction is a jump and the jump condition is met
160
        jump <= '1' when instruction(15 downto 13) = "101" and jumpconditionvalid='1' else
161
                                '0';
162
        -- Jump condition
163
        jumpconditionvalid <=   '1' when instruction(11 downto 8) = "0000" else                                                                 -- Unconditional jump
164
                                                                        '1' when instruction(11 downto 8) = "0001" and zf='1' else                                      -- je/jz
165
                                                                        '1' when instruction(11 downto 8) = "1001" and zf='0'    else                                    -- jne/jnz
166
                                                                        '1' when instruction(11 downto 8) = "0010" and zf='0' and cf='0' else     -- ja
167
                                                                        '1' when instruction(11 downto 8) = "1011" and zf='0' and cf='1' else    -- jb
168
                                                                        '0';
169
 
170
 
171
        ---------------------------------------------------------------------------------------
172
        -- RAM interface -- RAM interface -- RAM interface -- RAM interface -- RAM interface --
173
        ---------------------------------------------------------------------------------------
174
        -- ram address to read instruction and read or write data
175
        ram_address <= ip when fetch='1' else
176
                                                reg2out(N-1 downto 0) when instruction(15 downto 10)="000001" else
177
                                                instruction(N-1 downto 0) when instruction(15 downto 10)="000101" else
178
                                                reg1out(N-1 downto 0) when instruction(15 downto 10)="000010" else
179
                                                reg1out(N-1 downto 0) when instruction(15 downto 10)="000110" else
180
                                                (others=>'0');
181
                --"00000";
182
        -- Enable write
183
        ram_we <=       '1' when execute='1' and instruction(15 downto 13)="000" and instruction(11 downto 10)="10" else
184
                                        '0';
185
        -- Data to write
186
        ram_datawr <= wrdata;
187
 
188
 
189
 
190
        ------------------------------------------------------------------------------------------
191
        -- External interface -- External interface -- External interface -- External interface --
192
        ------------------------------------------------------------------------------------------
193
        -- Instantiate a register to hold the output interface data
194
        comp_regextout : entity work.dffre generic map (N=>8) port map(clk=>clk,rst=>rst,en=>ext_wren,d=>source,q=>ext_out);
195
        ext_wren <=     '1' when execute='1' and instruction(15 downto 13) = "110" and instruction(11 downto 10)="00" else
196
                                                '0';
197
 
198
 
199
        --------------------------------------------------------------------------------------
200
        -- Write data -- Write data -- Write data -- Write data -- Write data -- Write data -- 
201
        --------------------------------------------------------------------------------------
202
        -- Data may be written to ram or memory. The enable signals in the ram and register instances
203
        -- control whether the write occurs.
204
        -- Here we define what to write.
205
 
206
        wrdata <=       source when instruction(15 downto 13) = "000" and instruction(11 downto 10)="00" else                   -- Move with register or immediate as source
207
                                        source when instruction(15 downto 13) = "000" and instruction(11 downto 10)="10" else                   -- Move with register or immediate as source
208
                                        ram_datard when instruction(15 downto 13) = "000" and instruction(11 downto 10)="01" else               -- Move with memory as source
209
                                        aluqout when instruction(15 downto 13) = "001" else                             -- ALU
210
                                        aluqout when instruction(15 downto 13) = "010" else                             -- ALU
211
                                        aluqout when instruction(15 downto 13) = "011" else                             -- ALU
212
                                        ext_in when instruction(15 downto 13) = "110" and instruction(11 downto 10)="01" else                   -- Instruction in: read external input
213
                                        "00000000";
214
 
215
 
216
        -- Only for debugging
217
        dbg_instr <= instruction;
218
        dbg_seq <= seq;
219
        dbg_flags <= flags;
220
 
221
 
222
 
223
end Behavioral;
224
 
225
 
226
 

powered by: WebSVN 2.1.0

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