1 |
2 |
MichaelA |
////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
//
|
3 |
|
|
// Copyright 2012-2013 by Michael A. Morris, dba M. A. Morris & Associates
|
4 |
|
|
//
|
5 |
|
|
// All rights reserved. The source code contained herein is publicly released
|
6 |
|
|
// under the terms and conditions of the GNU Lesser Public License. No part of
|
7 |
|
|
// this source code may be reproduced or transmitted in any form or by any
|
8 |
|
|
// means, electronic or mechanical, including photocopying, recording, or any
|
9 |
|
|
// information storage and retrieval system in violation of the license under
|
10 |
|
|
// which the source code is released.
|
11 |
|
|
//
|
12 |
|
|
// The source code contained herein is free; it may be redistributed and/or
|
13 |
|
|
// modified in accordance with the terms of the GNU Lesser General Public
|
14 |
|
|
// License as published by the Free Software Foundation; either version 2.1 of
|
15 |
|
|
// the GNU Lesser General Public License, or any later version.
|
16 |
|
|
//
|
17 |
|
|
// The source code contained herein is freely released WITHOUT ANY WARRANTY;
|
18 |
|
|
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
19 |
|
|
// PARTICULAR PURPOSE. (Refer to the GNU Lesser General Public License for
|
20 |
|
|
// more details.)
|
21 |
|
|
//
|
22 |
|
|
// A copy of the GNU Lesser General Public License should have been received
|
23 |
|
|
// along with the source code contained herein; if not, a copy can be obtained
|
24 |
|
|
// by writing to:
|
25 |
|
|
//
|
26 |
|
|
// Free Software Foundation, Inc.
|
27 |
|
|
// 51 Franklin Street, Fifth Floor
|
28 |
|
|
// Boston, MA 02110-1301 USA
|
29 |
|
|
//
|
30 |
|
|
// Further, no use of this source code is permitted in any form or means
|
31 |
|
|
// without inclusion of this banner prominently in any derived works.
|
32 |
|
|
//
|
33 |
|
|
// Michael A. Morris
|
34 |
|
|
// Huntsville, AL
|
35 |
|
|
//
|
36 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
37 |
|
|
|
38 |
|
|
`timescale 1ns / 1ps
|
39 |
|
|
|
40 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
41 |
|
|
// Company: M. A. Morris & Associates
|
42 |
|
|
// Engineer: Michael A. Morris
|
43 |
|
|
//
|
44 |
|
|
// Create Date: 02/14/2012
|
45 |
|
|
// Design Name: WDC W65C02 Microprocessor Re-Implementation
|
46 |
|
|
// Module Name: M65C02_BCD
|
47 |
|
|
// Project Name: C:\XProjects\ISE10.1i\MAM6502
|
48 |
|
|
// Target Devices: Generic SRAM-based FPGA
|
49 |
|
|
// Tool versions: Xilinx ISE10.1i SP3
|
50 |
|
|
//
|
51 |
|
|
// Description:
|
52 |
|
|
//
|
53 |
|
|
// Dependencies: None.
|
54 |
|
|
//
|
55 |
|
|
// Revision:
|
56 |
|
|
//
|
57 |
|
|
// 1.00 12B14 MAM Initial coding. Modified W65C02_Adder.v for BCD
|
58 |
|
|
// only operation. Removed Mode input. No other
|
59 |
|
|
// changes.
|
60 |
|
|
//
|
61 |
|
|
// 1.01 12B15 MAM Cleaned up the second digit adder. Removed the C6
|
62 |
|
|
// signal and combined the 4-bit and 2-bit adders into
|
63 |
|
|
// a single 5-bit adder to match the one used for the
|
64 |
|
|
// least significant digit.
|
65 |
|
|
//
|
66 |
|
|
// 1.02 12B19 MAM Renamed module: MAM6502 => M6502_BCD.
|
67 |
|
|
//
|
68 |
|
|
// 1.10 12K17 MAM Converted MSN_GT9, MSN_GT8, and LSN_GT9 to ROMs
|
69 |
|
|
//
|
70 |
|
|
// 1.20 13H04 MAM Converted output so it generates a 0 until En signal
|
71 |
|
|
// is asserted. Makes module compatible with an OR bus.
|
72 |
|
|
//
|
73 |
|
|
// 1.30 13H16 MAM Modified rEn FF to have an asynchronous reset. This
|
74 |
|
|
// drives the module output to zero before the start of
|
75 |
|
|
// the next microcycle and prevent contention on the
|
76 |
|
|
// ALU output data bus.
|
77 |
|
|
//
|
78 |
|
|
// Additional Comments:
|
79 |
|
|
//
|
80 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
81 |
|
|
|
82 |
|
|
module M65C02_BCD(
|
83 |
|
|
input Rst, // Module Reset
|
84 |
|
|
input Clk, // System Clock
|
85 |
|
|
input En, // Enable
|
86 |
|
|
|
87 |
|
|
input Op, // Adder Operation: 0 - Addition; 1 - Subtract
|
88 |
|
|
|
89 |
|
|
input [7:0] A, // Adder Input A
|
90 |
|
|
input [7:0] B, // Adder Input B
|
91 |
|
|
input Ci, // Adder Carry In
|
92 |
|
|
|
93 |
|
|
output reg [8:0] Out, // Adder Sum <= A + B + Ci
|
94 |
|
|
output reg OV, // Adder Overflow
|
95 |
|
|
output reg Valid // Adder Outputs Valid
|
96 |
|
|
);
|
97 |
|
|
|
98 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
99 |
|
|
//
|
100 |
|
|
// Declarations
|
101 |
|
|
//
|
102 |
|
|
|
103 |
|
|
reg [7:0] S; // Intermediate Binary Sum: S <= A + B + Ci
|
104 |
|
|
reg [1:0] DA; // Decimal Adjust Controls
|
105 |
|
|
reg C3, C7; // Sum Carry Out from Bitx
|
106 |
|
|
reg [7:0] Adj;
|
107 |
|
|
|
108 |
|
|
reg rEn, rOp;
|
109 |
|
|
reg [7:0] rS;
|
110 |
|
|
reg rC3, rC7;
|
111 |
|
|
|
112 |
|
|
reg MSN_GT9, MSN_GT8, LSN_GT9; // Digit value comparator signals
|
113 |
|
|
|
114 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
115 |
|
|
//
|
116 |
|
|
// Implementation
|
117 |
|
|
//
|
118 |
|
|
|
119 |
|
|
// Capture Input Control Signals
|
120 |
|
|
|
121 |
|
|
assign Rst_BCD = (Rst | ~En);
|
122 |
|
|
|
123 |
|
|
always @(posedge Clk or posedge Rst_BCD)
|
124 |
|
|
begin
|
125 |
|
|
if(Rst_BCD)
|
126 |
|
|
{rEn, rOp} <= #1 0;
|
127 |
|
|
else
|
128 |
|
|
{rEn, rOp} <= #1 {En, Op};
|
129 |
|
|
end
|
130 |
|
|
|
131 |
|
|
// Adder First Stage - Combinatorial; Binary Sums and Carries
|
132 |
|
|
|
133 |
|
|
always @(*)
|
134 |
|
|
begin
|
135 |
|
|
// Binary Addition and Generate C3 and C7 Carries
|
136 |
|
|
|
137 |
|
|
{C3, S[3:0]} <= ({1'b0, A[3:0]} + {1'b0, B[3:0]} + {4'b0, Ci});
|
138 |
|
|
{C7, S[7:4]} <= ({1'b0, A[7:4]} + {1'b0, B[7:4]} + {4'b0, C3});
|
139 |
|
|
end
|
140 |
|
|
|
141 |
|
|
// Adder First Stage - Registered; Binary Sums and Carrys
|
142 |
|
|
|
143 |
|
|
always @(posedge Clk)
|
144 |
|
|
begin
|
145 |
|
|
if(Rst)
|
146 |
|
|
{rC7, rC3, rS} <= #1 0;
|
147 |
|
|
else if(En)
|
148 |
|
|
{rC7, rC3, rS} <= #1 {C7, C3, S};
|
149 |
|
|
end
|
150 |
|
|
|
151 |
|
|
// Generate Digit/Nibble Value Comparators
|
152 |
|
|
|
153 |
|
|
//always @(*) MSN_GT9 <= (rS[7:4] > 9);
|
154 |
|
|
|
155 |
|
|
always @(*)
|
156 |
|
|
begin
|
157 |
|
|
case(rS[7:4])
|
158 |
|
|
4'b0000 : MSN_GT9 <= 0;
|
159 |
|
|
4'b0001 : MSN_GT9 <= 0;
|
160 |
|
|
4'b0010 : MSN_GT9 <= 0;
|
161 |
|
|
4'b0011 : MSN_GT9 <= 0;
|
162 |
|
|
4'b0100 : MSN_GT9 <= 0;
|
163 |
|
|
4'b0101 : MSN_GT9 <= 0;
|
164 |
|
|
4'b0110 : MSN_GT9 <= 0;
|
165 |
|
|
4'b0111 : MSN_GT9 <= 0;
|
166 |
|
|
4'b1000 : MSN_GT9 <= 0;
|
167 |
|
|
4'b1001 : MSN_GT9 <= 0;
|
168 |
|
|
4'b1010 : MSN_GT9 <= 1;
|
169 |
|
|
4'b1011 : MSN_GT9 <= 1;
|
170 |
|
|
4'b1100 : MSN_GT9 <= 1;
|
171 |
|
|
4'b1101 : MSN_GT9 <= 1;
|
172 |
|
|
4'b1110 : MSN_GT9 <= 1;
|
173 |
|
|
4'b1111 : MSN_GT9 <= 1;
|
174 |
|
|
endcase
|
175 |
|
|
end
|
176 |
|
|
|
177 |
|
|
//always @(*) MSN_GT8 <= (rS[7:4] > 8);
|
178 |
|
|
|
179 |
|
|
always @(*)
|
180 |
|
|
begin
|
181 |
|
|
case(rS[7:4])
|
182 |
|
|
4'b0000 : MSN_GT8 <= 0;
|
183 |
|
|
4'b0001 : MSN_GT8 <= 0;
|
184 |
|
|
4'b0010 : MSN_GT8 <= 0;
|
185 |
|
|
4'b0011 : MSN_GT8 <= 0;
|
186 |
|
|
4'b0100 : MSN_GT8 <= 0;
|
187 |
|
|
4'b0101 : MSN_GT8 <= 0;
|
188 |
|
|
4'b0110 : MSN_GT8 <= 0;
|
189 |
|
|
4'b0111 : MSN_GT8 <= 0;
|
190 |
|
|
4'b1000 : MSN_GT8 <= 0;
|
191 |
|
|
4'b1001 : MSN_GT8 <= 1;
|
192 |
|
|
4'b1010 : MSN_GT8 <= 1;
|
193 |
|
|
4'b1011 : MSN_GT8 <= 1;
|
194 |
|
|
4'b1100 : MSN_GT8 <= 1;
|
195 |
|
|
4'b1101 : MSN_GT8 <= 1;
|
196 |
|
|
4'b1110 : MSN_GT8 <= 1;
|
197 |
|
|
4'b1111 : MSN_GT8 <= 1;
|
198 |
|
|
endcase
|
199 |
|
|
end
|
200 |
|
|
|
201 |
|
|
//always @(*) LSN_GT9 <= (rS[3:0] > 9);
|
202 |
|
|
|
203 |
|
|
always @(*)
|
204 |
|
|
begin
|
205 |
|
|
case(rS[3:0])
|
206 |
|
|
4'b0000 : LSN_GT9 <= 0;
|
207 |
|
|
4'b0001 : LSN_GT9 <= 0;
|
208 |
|
|
4'b0010 : LSN_GT9 <= 0;
|
209 |
|
|
4'b0011 : LSN_GT9 <= 0;
|
210 |
|
|
4'b0100 : LSN_GT9 <= 0;
|
211 |
|
|
4'b0101 : LSN_GT9 <= 0;
|
212 |
|
|
4'b0110 : LSN_GT9 <= 0;
|
213 |
|
|
4'b0111 : LSN_GT9 <= 0;
|
214 |
|
|
4'b1000 : LSN_GT9 <= 0;
|
215 |
|
|
4'b1001 : LSN_GT9 <= 0;
|
216 |
|
|
4'b1010 : LSN_GT9 <= 1;
|
217 |
|
|
4'b1011 : LSN_GT9 <= 1;
|
218 |
|
|
4'b1100 : LSN_GT9 <= 1;
|
219 |
|
|
4'b1101 : LSN_GT9 <= 1;
|
220 |
|
|
4'b1110 : LSN_GT9 <= 1;
|
221 |
|
|
4'b1111 : LSN_GT9 <= 1;
|
222 |
|
|
endcase
|
223 |
|
|
end
|
224 |
|
|
|
225 |
|
|
// Adder Second Stage - Combinatorial; BCD Digit Adjustment
|
226 |
|
|
|
227 |
|
|
always @(*)
|
228 |
|
|
begin
|
229 |
|
|
// Generate Decimal Mode Digit Adjust Signals
|
230 |
|
|
|
231 |
|
|
DA[1] <= ((rOp) ? ~rC7 | (DA[0] & MSN_GT9)
|
232 |
|
|
: rC7 | MSN_GT9 | (DA[0] & ~rC3 & MSN_GT8));
|
233 |
|
|
DA[0] <= ((rOp) ? ~rC3
|
234 |
|
|
: rC3 | LSN_GT9);
|
235 |
|
|
|
236 |
|
|
case(DA)
|
237 |
|
|
2'b01 : Adj <= (rS + ((rOp) ? 8'hFA : 8'h06)); // ±06 BCD
|
238 |
|
|
2'b10 : Adj <= (rS + ((rOp) ? 8'hA0 : 8'h60)); // ±60 BCD
|
239 |
|
|
2'b11 : Adj <= (rS + ((rOp) ? 8'h9A : 8'h66)); // ±66 BCD
|
240 |
|
|
default : Adj <= (rS + 8'h00); // 0
|
241 |
|
|
endcase
|
242 |
|
|
end
|
243 |
|
|
|
244 |
|
|
// Adder Second Stage - Combinatorial; BCD Digit Adjustment
|
245 |
|
|
|
246 |
|
|
always @(*)
|
247 |
|
|
begin
|
248 |
|
|
Out <= ((rEn) ? {(rOp ^ DA[1]), Adj} : 0);
|
249 |
|
|
OV <= ((rEn) ? DA[1] : 0);
|
250 |
|
|
Valid <= rEn;
|
251 |
|
|
end
|
252 |
|
|
|
253 |
|
|
endmodule
|