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

Subversion Repositories RISCMCU

[/] [RISCMCU/] [tags/] [arelease/] [v_alu.vhd] - Blame information for rev 28

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 yapzihe
----------------------------------------------------------------------------
2
----                                                                    ----
3
---- WISHBONE RISCMCU IP Core                                           ----
4
----                                                                    ----
5
---- This file is part of the RISCMCU project                           ----
6
---- http://www.opencores.org/projects/riscmcu/                         ----
7
----                                                                    ----
8
---- Description                                                        ----
9
---- Implementation of a RISC Microcontroller based on Atmel AVR        ----
10
---- AT90S1200 instruction set and features with Altera Flex10k20 FPGA. ----
11
----                                                                    ----
12
---- Author(s):                                                         ----
13
----    - Yap Zi He, yapzihe@hotmail.com                                ----
14
----                                                                    ----
15
----------------------------------------------------------------------------
16
----                                                                    ----
17
---- Copyright (C) 2001 Authors and OPENCORES.ORG                       ----
18
----                                                                    ----
19
---- This source file may be used and distributed without               ----
20
---- restriction provided that this copyright statement is not          ----
21
---- removed from the file and that any derivative work contains        ----
22
---- the original copyright notice and the associated disclaimer.       ----
23
----                                                                    ----
24
---- This source file is free software; you can redistribute it         ----
25
---- and/or modify it under the terms of the GNU Lesser General         ----
26
---- Public License as published by the Free Software Foundation;       ----
27
---- either version 2.1 of the License, or (at your option) any         ----
28
---- later version.                                                     ----
29
----                                                                    ----
30
---- This source is distributed in the hope that it will be             ----
31
---- useful, but WITHOUT ANY WARRANTY; without even the implied         ----
32
---- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR            ----
33
---- PURPOSE. See the GNU Lesser General Public License for more        ----
34
---- details.                                                           ----
35
----                                                                    ----
36
---- You should have received a copy of the GNU Lesser General          ----
37
---- Public License along with this source; if not, download it         ----
38
---- from http://www.opencores.org/lgpl.shtml                           ----
39
----                                                                    ----
40
----------------------------------------------------------------------------
41
 
42
library ieee;
43
use ieee.std_logic_1164.all;
44
use ieee.std_logic_arith.all;
45
use ieee.std_logic_unsigned.all;
46
library lpm;
47
use lpm.lpm_components.all;
48
 
49
entity v_alu is
50
 port(  reg_rd, reg_rr, imm_value : in std_logic_vector(7 downto 0);
51
                c2a, c2b : in std_logic;
52
                asel : in integer range 0 to 1;
53
                bsel : in integer range 0 to 3;
54
 
55
                bitsel : in integer range 0 to 7;
56
                set : in std_logic;
57
                c_flag, t_flag : in std_logic;
58
 
59
                add, subcp, logic, right, dir, bld, cbisbi, pass_a : in std_logic;
60
                cpse, skiptest : in std_logic;
61
 
62
                wcarry : in std_logic;
63
                logicsel : in integer range 0 to 3;
64
                rightsel : in integer range 0 to 2;
65
                dirsel : in integer range 0 to 1;
66
 
67
                clk, clrn : in std_logic;
68
 
69
                c : buffer std_logic_vector(7 downto 0);
70
                tosr : buffer std_logic_vector (6 downto 0);
71
                skip : out std_logic
72
 );
73
 
74
end v_alu;
75
 
76
architecture alu of v_alu is
77
 
78
signal a, b : std_logic_vector(7 downto 0);
79
 
80
signal sr : std_logic_vector(6 downto 0);
81
 
82
signal cin, overflow, cout : std_logic;
83
 
84
signal sum, logic_out, right_out, dir_out, bldcbi_out : std_logic_vector(7 downto 0);
85
 
86
begin
87
 
88
-- Fetch Operand To Buffer ------------------------------------------
89
process(clrn, clk)
90
begin
91
        if clrn = '0' then
92
                a <= "00000000";
93
                b <= "00000000";
94
        elsif clk'event and clk = '1' then
95
                case asel is
96
                        when 0 =>
97
                                if c2a = '1' then
98
                                        a <= c;
99
                                else
100
                                        a <= reg_rd;
101
                                end if;
102
                        when 1 =>
103
                                a <= "00000000";
104
                end case;
105
 
106
                case bsel is
107
                        when 0 =>
108
                                if c2b = '1' then
109
                                        b <= c;
110
                                else
111
                                        b <= reg_rr;
112
                                end if;
113
                        when 1 =>
114
                                b <= reg_rd;
115
                        when 2 =>
116
                                b <= imm_value;
117
                        when 3 =>
118
                                b <= "00000001";
119
                end case;
120
        end if;
121
end process;
122
 
123
 
124
-- ALU START------------------------------------------------------------
125
 
126
 
127
cin <= c_flag when add = '1' and wcarry = '1' else
128
                '0' when add = '1' and wcarry = '0' else
129
                not c_flag when wcarry = '1' else
130
                '1';
131
 
132
-- Adder, Logic, Shift Right, Direct, Bld, Cbisbi ---------------------------
133
 
134
adder1 : lpm_add_sub
135
        generic map(lpm_width => 8)
136
        port map (dataa => a, datab => b, cin => cin, add_sub => add, result => sum, cout => cout, overflow => overflow);
137
 
138
with logicsel select
139
        logic_out <= a and b when 0, -- and, andi
140
                                a or b when 1, -- or, ori
141
                                a xor b when 2, -- eor
142
                                not a when 3; -- com
143
 
144
right_out(6 downto 0) <= a(7 downto 1);
145
with rightsel select
146
        right_out(7) <= '0' when 0, -- lsr
147
                                        c_flag when 1, -- ror
148
                                        a(7) when 2; -- asr
149
 
150
with dirsel select
151
        dir_out <= b when 0, -- ldi, mov
152
                                (a(3 downto 0) & a(7 downto 4)) when 1; -- swap
153
 
154
process(bld, bitsel, a, t_flag, set)
155
begin
156
        for i in 0 to 7 loop
157
                if i /= bitsel then
158
                        bldcbi_out(i) <= a(i);
159
                elsif bld = '1' then
160
                        bldcbi_out(i) <= t_flag;
161
                else
162
                        bldcbi_out(i) <= set;
163
                end if;
164
        end loop;
165
end process;
166
 
167
 
168
-- Output correct result to Data Bus (C Bus) ------------------------
169
 
170
process(add, subcp, logic, right, dir, bld, cbisbi, pass_a, sum, logic_out, right_out, dir_out, bldcbi_out, a)
171
begin
172
 
173
 c <= "ZZZZZZZZ";
174
 
175
 -- add, adc, inc, sub, sbc, subi, sbci, cp, cpc, cpi, dec, neg
176
 if add = '1' or subcp = '1' then
177
        c <= sum;
178
 end if;
179
 
180
 -- and, andi, or, ori, eor, com
181
 if logic = '1' then
182
        c <= logic_out;
183
 end if;
184
 
185
 -- lsr, lsr, asr
186
 if right = '1' then
187
        c <= right_out;
188
 end if;
189
 
190
 -- ldi, mov, swap
191
 if dir = '1' then
192
        c <= dir_out;
193
 end if;
194
 
195
 -- bld, cbisbi
196
 if bld = '1' or cbisbi = '1' then
197
        c <= bldcbi_out;
198
 end if;
199
 
200
 -- out
201
 if pass_a = '1' then
202
        c <= a;
203
 end if;
204
 
205
end process;
206
---------------------------------------------------------------------------------
207
 
208
-- Perform Skip Test ----------------------------------------------------------
209
 
210
process(cpse, skiptest, a, b, set, bitsel, c)
211
begin
212
 
213
 skip <= '0';
214
 
215
 -- cpse
216
 if cpse = '1' then
217
        if a = b then
218
                skip <= '1';
219
        end if;
220
 
221
 -- sbrc, sbrs
222
 elsif skiptest = '1' then
223
        if (set = '1' and a(bitsel) = '1') or (set = '0' and a(bitsel) = '0') then
224
                skip <= '1';
225
        end if;
226
 
227
 end if;
228
end process;
229
--------------------------------------------------------------------------
230
 
231
-- Calculate Status Register's Flags -------------------------------------
232
 
233
process(add, subcp, cout, right, a, logic, a, b, sum, logic_out, right_out, c, overflow, sr, bitsel)
234
begin
235
 
236
-- C sr(0)
237
 if add = '1' then
238
        sr(0) <= cout;
239
 elsif right = '1' then
240
        sr(0) <= a(0);
241
 elsif logic = '1' then -- com
242
        sr(0) <= '1';
243
 else -- subcp
244
        sr(0) <= not cout;
245
        --sr(0) <= (not a(7) and b(7)) or (b(7) and c(7)) or (c(7) and not a(7));
246
 end if;
247
 
248
-- Z sr(1)
249
 if (add = '1' or subcp = '1') and sum = "00000000" then
250
        sr(1) <= '1';
251
 elsif logic = '1' and logic_out = "00000000" then
252
        sr(1) <= '1';
253
 elsif right = '1' and right_out = "00000000" then
254
        sr(1) <= '1';
255
 else
256
        sr(1) <= '0';
257
 end if;
258
 
259
-- N sr(2)
260
 if (add = '1' or subcp = '1') and sum(7) = '1' then
261
        sr(2) <= '1';
262
 elsif logic = '1' and logic_out(7) = '1' then
263
        sr(2) <= '1';
264
 elsif right = '1' and right_out(7) = '1' then
265
        sr(2) <= '1';
266
 else
267
        sr(2) <= '0';
268
 end if;
269
 
270
-- V sr(3)
271
 if right = '1' then
272
        sr(3) <= right_out(7) xor a(0);
273
 elsif logic = '1' then
274
        sr(3) <= '0';
275
 else
276
        sr(3) <= overflow;
277
 end if;
278
 
279
-- S sr(4)
280
 sr(4) <= sr(2) xor sr(3);
281
 
282
-- H sr(5)
283
 if add = '1' then
284
        sr(5) <= (a(3) and b(3)) or (b(3) and not sum(3)) or (not sum(3) and a(3));
285
 else -- subcp
286
        sr(5) <= (not a(3) and b(3)) or (b(3) and sum(3)) or (sum(3) and not a(3));
287
 end if;
288
 
289
-- T sr(6)
290
 sr(6) <= a(bitsel);
291
 
292
end process;
293
 
294
tosr <= sr;
295
 
296
------------------------------------------------------------------------
297
 
298
end alu;

powered by: WebSVN 2.1.0

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