1 |
7 |
jesus |
--
|
2 |
|
|
-- 6502 compatible microprocessor core
|
3 |
|
|
--
|
4 |
|
|
-- Version : 0245
|
5 |
|
|
--
|
6 |
|
|
-- Copyright (c) 2002 Daniel Wallner (jesus@opencores.org)
|
7 |
|
|
--
|
8 |
|
|
-- All rights reserved
|
9 |
|
|
--
|
10 |
|
|
-- Redistribution and use in source and synthezised forms, with or without
|
11 |
|
|
-- modification, are permitted provided that the following conditions are met:
|
12 |
|
|
--
|
13 |
|
|
-- Redistributions of source code must retain the above copyright notice,
|
14 |
|
|
-- this list of conditions and the following disclaimer.
|
15 |
|
|
--
|
16 |
|
|
-- Redistributions in synthesized form must reproduce the above copyright
|
17 |
|
|
-- notice, this list of conditions and the following disclaimer in the
|
18 |
|
|
-- documentation and/or other materials provided with the distribution.
|
19 |
|
|
--
|
20 |
|
|
-- Neither the name of the author nor the names of other contributors may
|
21 |
|
|
-- be used to endorse or promote products derived from this software without
|
22 |
|
|
-- specific prior written permission.
|
23 |
|
|
--
|
24 |
|
|
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
25 |
|
|
-- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
26 |
|
|
-- THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
27 |
|
|
-- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
|
28 |
|
|
-- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
29 |
|
|
-- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
30 |
|
|
-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
31 |
|
|
-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
32 |
|
|
-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
33 |
|
|
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
34 |
|
|
-- POSSIBILITY OF SUCH DAMAGE.
|
35 |
|
|
--
|
36 |
|
|
-- Please report bugs to the author, but before you do so, please
|
37 |
|
|
-- make sure that this is not a derivative work and that
|
38 |
|
|
-- you have the latest version of this file.
|
39 |
|
|
--
|
40 |
|
|
-- The latest version of this file can be found at:
|
41 |
|
|
-- http://www.opencores.org/cvsweb.shtml/t65/
|
42 |
|
|
--
|
43 |
|
|
-- Limitations :
|
44 |
|
|
--
|
45 |
|
|
-- File history :
|
46 |
|
|
--
|
47 |
|
|
-- 0245 : First version
|
48 |
|
|
--
|
49 |
|
|
|
50 |
|
|
library IEEE;
|
51 |
|
|
use IEEE.std_logic_1164.all;
|
52 |
|
|
use IEEE.numeric_std.all;
|
53 |
|
|
use work.T65_Pack.all;
|
54 |
|
|
|
55 |
|
|
entity T65_ALU is
|
56 |
|
|
port(
|
57 |
|
|
Mode : in std_logic_vector(1 downto 0); -- "00" => 6502, "01" => 65C02, "10" => 65816
|
58 |
|
|
Op : in std_logic_vector(3 downto 0);
|
59 |
|
|
BusA : in std_logic_vector(7 downto 0);
|
60 |
|
|
BusB : in std_logic_vector(7 downto 0);
|
61 |
|
|
P_In : in std_logic_vector(7 downto 0);
|
62 |
|
|
P_Out : out std_logic_vector(7 downto 0);
|
63 |
|
|
Q : out std_logic_vector(7 downto 0)
|
64 |
|
|
);
|
65 |
|
|
end T65_ALU;
|
66 |
|
|
|
67 |
|
|
architecture rtl of T65_ALU is
|
68 |
|
|
|
69 |
|
|
-- AddSub variables (temporary signals)
|
70 |
|
|
signal ADC_Z : std_logic;
|
71 |
|
|
signal ADC_C : std_logic;
|
72 |
|
|
signal ADC_V : std_logic;
|
73 |
|
|
signal ADC_N : std_logic;
|
74 |
|
|
signal ADC_Q : std_logic_vector(7 downto 0);
|
75 |
|
|
signal SBC_Z : std_logic;
|
76 |
|
|
signal SBC_C : std_logic;
|
77 |
|
|
signal SBC_V : std_logic;
|
78 |
|
|
signal SBC_N : std_logic;
|
79 |
|
|
signal SBC_Q : std_logic_vector(7 downto 0);
|
80 |
|
|
|
81 |
|
|
begin
|
82 |
|
|
|
83 |
|
|
process (P_In, BusA, BusB)
|
84 |
|
|
variable AL : unsigned(6 downto 0);
|
85 |
|
|
variable AH : unsigned(6 downto 0);
|
86 |
|
|
variable C : std_logic;
|
87 |
|
|
begin
|
88 |
|
|
AL := resize(unsigned(BusA(3 downto 0) & P_In(Flag_C)), 7) + resize(unsigned(BusB(3 downto 0) & "1"), 7);
|
89 |
|
|
AH := resize(unsigned(BusA(7 downto 4) & AL(5)), 7) + resize(unsigned(BusB(7 downto 4) & "1"), 7);
|
90 |
|
|
|
91 |
|
|
-- pragma translate_off
|
92 |
|
|
if is_x(std_logic_vector(AL)) then AL := "0000000"; end if;
|
93 |
|
|
if is_x(std_logic_vector(AH)) then AH := "0000000"; end if;
|
94 |
|
|
-- pragma translate_on
|
95 |
|
|
|
96 |
|
|
if AL(4 downto 1) = 0 and AH(4 downto 1) = 0 then
|
97 |
|
|
ADC_Z <= '1';
|
98 |
|
|
else
|
99 |
|
|
ADC_Z <= '0';
|
100 |
|
|
end if;
|
101 |
|
|
|
102 |
|
|
if AL(5 downto 1) > 9 and P_In(Flag_D) = '1' then
|
103 |
|
|
AL(6 downto 1) := AL(6 downto 1) + 6;
|
104 |
|
|
end if;
|
105 |
|
|
|
106 |
|
|
C := AL(6) or AL(5);
|
107 |
|
|
AH := resize(unsigned(BusA(7 downto 4) & C), 7) + resize(unsigned(BusB(7 downto 4) & "1"), 7);
|
108 |
|
|
|
109 |
|
|
ADC_N <= AH(4);
|
110 |
|
|
ADC_V <= (AH(4) xor BusA(7)) and not (BusA(7) xor BusB(7));
|
111 |
|
|
|
112 |
|
|
-- pragma translate_off
|
113 |
|
|
if is_x(std_logic_vector(AH)) then AH := "0000000"; end if;
|
114 |
|
|
-- pragma translate_on
|
115 |
|
|
|
116 |
|
|
if AH(5 downto 1) > 9 and P_In(Flag_D) = '1' then
|
117 |
|
|
AH(6 downto 1) := AH(6 downto 1) + 6;
|
118 |
|
|
end if;
|
119 |
|
|
|
120 |
|
|
ADC_C <= AH(6) or AH(5);
|
121 |
|
|
|
122 |
|
|
ADC_Q <= std_logic_vector(AH(4 downto 1) & AL(4 downto 1));
|
123 |
|
|
end process;
|
124 |
|
|
|
125 |
|
|
process (Op, P_In, BusA, BusB)
|
126 |
|
|
variable AL : unsigned(6 downto 0);
|
127 |
|
|
variable AH : unsigned(5 downto 0);
|
128 |
|
|
variable C : std_logic;
|
129 |
|
|
begin
|
130 |
|
|
C := P_In(Flag_C) or not Op(0);
|
131 |
|
|
AL := resize(unsigned(BusA(3 downto 0) & C), 7) - resize(unsigned(BusB(3 downto 0) & "1"), 6);
|
132 |
|
|
AH := resize(unsigned(BusA(7 downto 4) & "0"), 6) - resize(unsigned(BusB(7 downto 4) & AL(5)), 6);
|
133 |
|
|
|
134 |
|
|
-- pragma translate_off
|
135 |
|
|
if is_x(std_logic_vector(AL)) then AL := "0000000"; end if;
|
136 |
|
|
if is_x(std_logic_vector(AH)) then AH := "000000"; end if;
|
137 |
|
|
-- pragma translate_on
|
138 |
|
|
|
139 |
|
|
if AL(4 downto 1) = 0 and AH(4 downto 1) = 0 then
|
140 |
|
|
SBC_Z <= '1';
|
141 |
|
|
else
|
142 |
|
|
SBC_Z <= '0';
|
143 |
|
|
end if;
|
144 |
|
|
|
145 |
|
|
SBC_C <= not AH(5);
|
146 |
|
|
SBC_V <= (AH(4) xor BusA(7)) and (BusA(7) xor BusB(7));
|
147 |
|
|
SBC_N <= AH(4);
|
148 |
|
|
|
149 |
|
|
if P_In(Flag_D) = '1' then
|
150 |
|
|
if AL(5) = '1' then
|
151 |
|
|
AL(5 downto 1) := AL(5 downto 1) - 6;
|
152 |
|
|
end if;
|
153 |
|
|
AH := resize(unsigned(BusA(7 downto 4) & "0"), 6) - resize(unsigned(BusB(7 downto 4) & AL(6)), 6);
|
154 |
|
|
if AH(5) = '1' then
|
155 |
|
|
AH(5 downto 1) := AH(5 downto 1) - 6;
|
156 |
|
|
end if;
|
157 |
|
|
end if;
|
158 |
|
|
|
159 |
|
|
SBC_Q <= std_logic_vector(AH(4 downto 1) & AL(4 downto 1));
|
160 |
|
|
end process;
|
161 |
|
|
|
162 |
|
|
process (Op, P_In, BusA, BusB,
|
163 |
|
|
ADC_Z, ADC_C, ADC_V, ADC_N, ADC_Q,
|
164 |
|
|
SBC_Z, SBC_C, SBC_V, SBC_N, SBC_Q)
|
165 |
|
|
variable Q_t : std_logic_vector(7 downto 0);
|
166 |
|
|
begin
|
167 |
|
|
-- ORA, AND, EOR, ADC, NOP, LD, CMP, SBC
|
168 |
|
|
-- ASL, ROL, LSR, ROR, BIT, LD, DEC, INC
|
169 |
|
|
P_Out <= P_In;
|
170 |
|
|
Q_t := BusA;
|
171 |
|
|
case Op(3 downto 0) is
|
172 |
|
|
when "0000" =>
|
173 |
|
|
-- ORA
|
174 |
|
|
Q_t := BusA or BusB;
|
175 |
|
|
when "0001" =>
|
176 |
|
|
-- AND
|
177 |
|
|
Q_t := BusA and BusB;
|
178 |
|
|
when "0010" =>
|
179 |
|
|
-- EOR
|
180 |
|
|
Q_t := BusA xor BusB;
|
181 |
|
|
when "0011" =>
|
182 |
|
|
-- ADC
|
183 |
|
|
P_Out(Flag_V) <= ADC_V;
|
184 |
|
|
P_Out(Flag_C) <= ADC_C;
|
185 |
|
|
Q_t := ADC_Q;
|
186 |
|
|
when "0101" | "1101" =>
|
187 |
|
|
-- LDA
|
188 |
|
|
when "0110" =>
|
189 |
|
|
-- CMP
|
190 |
|
|
P_Out(Flag_C) <= SBC_C;
|
191 |
|
|
when "0111" =>
|
192 |
|
|
-- SBC
|
193 |
|
|
P_Out(Flag_V) <= SBC_V;
|
194 |
|
|
P_Out(Flag_C) <= SBC_C;
|
195 |
|
|
Q_t := SBC_Q;
|
196 |
|
|
when "1000" =>
|
197 |
|
|
-- ASL
|
198 |
|
|
Q_t := BusA(6 downto 0) & "0";
|
199 |
|
|
P_Out(Flag_C) <= BusA(7);
|
200 |
|
|
when "1001" =>
|
201 |
|
|
-- ROL
|
202 |
|
|
Q_t := BusA(6 downto 0) & P_In(Flag_C);
|
203 |
|
|
P_Out(Flag_C) <= BusA(7);
|
204 |
|
|
when "1010" =>
|
205 |
|
|
-- LSR
|
206 |
|
|
Q_t := "0" & BusA(7 downto 1);
|
207 |
|
|
P_Out(Flag_C) <= BusA(0);
|
208 |
|
|
when "1011" =>
|
209 |
|
|
-- ROR
|
210 |
|
|
Q_t := P_In(Flag_C) & BusA(7 downto 1);
|
211 |
|
|
P_Out(Flag_C) <= BusA(0);
|
212 |
|
|
when "1100" =>
|
213 |
|
|
-- BIT
|
214 |
|
|
P_Out(Flag_V) <= BusB(6);
|
215 |
|
|
when "1110" =>
|
216 |
|
|
-- DEC
|
217 |
|
|
Q_t := std_logic_vector(unsigned(BusA) - 1);
|
218 |
|
|
when "1111" =>
|
219 |
|
|
-- INC
|
220 |
|
|
Q_t := std_logic_vector(unsigned(BusA) + 1);
|
221 |
|
|
when others =>
|
222 |
|
|
end case;
|
223 |
|
|
|
224 |
|
|
case Op(3 downto 0) is
|
225 |
|
|
when "0011" =>
|
226 |
|
|
P_Out(Flag_N) <= ADC_N;
|
227 |
|
|
P_Out(Flag_Z) <= ADC_Z;
|
228 |
|
|
when "0110" | "0111" =>
|
229 |
|
|
P_Out(Flag_N) <= SBC_N;
|
230 |
|
|
P_Out(Flag_Z) <= SBC_Z;
|
231 |
|
|
when "0100" =>
|
232 |
|
|
when "1100" =>
|
233 |
|
|
P_Out(Flag_N) <= BusB(7);
|
234 |
|
|
if (BusA and BusB) = "00000000" then
|
235 |
|
|
P_Out(Flag_Z) <= '1';
|
236 |
|
|
else
|
237 |
|
|
P_Out(Flag_Z) <= '0';
|
238 |
|
|
end if;
|
239 |
|
|
when others =>
|
240 |
|
|
P_Out(Flag_N) <= Q_t(7);
|
241 |
|
|
if Q_t = "00000000" then
|
242 |
|
|
P_Out(Flag_Z) <= '1';
|
243 |
|
|
else
|
244 |
|
|
P_Out(Flag_Z) <= '0';
|
245 |
|
|
end if;
|
246 |
|
|
end case;
|
247 |
|
|
|
248 |
|
|
Q <= Q_t;
|
249 |
|
|
end process;
|
250 |
|
|
|
251 |
|
|
end;
|