1 |
2 |
ndumitrach |
//////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
//
|
3 |
|
|
// This file is part of the Next186 project
|
4 |
|
|
// http://opencores.org/project,next186
|
5 |
|
|
//
|
6 |
|
|
// Filename: Next186_ALU.v
|
7 |
|
|
// Description: Part of the Next186 CPU project, arithmetic-logic unit and effective address unit implementation
|
8 |
|
|
// Version 1.0
|
9 |
|
|
// Creation date: 11Apr2011 - 07Jun2011
|
10 |
|
|
//
|
11 |
|
|
// Author: Nicolae Dumitrache
|
12 |
|
|
// e-mail: ndumitrache@opencores.org
|
13 |
|
|
//
|
14 |
|
|
/////////////////////////////////////////////////////////////////////////////////
|
15 |
|
|
//
|
16 |
|
|
// Copyright (C) 2011 Nicolae Dumitrache
|
17 |
|
|
//
|
18 |
|
|
// This source file may be used and distributed without
|
19 |
|
|
// restriction provided that this copyright statement is not
|
20 |
|
|
// removed from the file and that any derivative work contains
|
21 |
|
|
// the original copyright notice and the associated disclaimer.
|
22 |
|
|
//
|
23 |
|
|
// This source file is free software; you can redistribute it
|
24 |
|
|
// and/or modify it under the terms of the GNU Lesser General
|
25 |
|
|
// Public License as published by the Free Software Foundation;
|
26 |
|
|
// either version 2.1 of the License, or (at your option) any
|
27 |
|
|
// later version.
|
28 |
|
|
//
|
29 |
|
|
// This source is distributed in the hope that it will be
|
30 |
|
|
// useful, but WITHOUT ANY WARRANTY; without even the implied
|
31 |
|
|
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
32 |
|
|
// PURPOSE. See the GNU Lesser General Public License for more
|
33 |
|
|
// details.
|
34 |
|
|
//
|
35 |
|
|
// You should have received a copy of the GNU Lesser General
|
36 |
|
|
// Public License along with this source; if not, download it
|
37 |
|
|
// from http://www.opencores.org/lgpl.shtml
|
38 |
|
|
//
|
39 |
|
|
///////////////////////////////////////////////////////////////////////////////////
|
40 |
|
|
// Additional Comments:
|
41 |
|
|
//
|
42 |
|
|
// ADD 00_000
|
43 |
|
|
// OR 00_001
|
44 |
|
|
// ADC 00_010
|
45 |
|
|
// SBB 00_011
|
46 |
|
|
// AND 00_100
|
47 |
|
|
// SUB 00_101
|
48 |
|
|
// XOR 00_110
|
49 |
|
|
// CMP 00_111
|
50 |
|
|
//
|
51 |
|
|
// INC 01_000
|
52 |
|
|
// DEC 01_001
|
53 |
|
|
// NOT 01_010
|
54 |
|
|
// NEG 01_011
|
55 |
|
|
// DAA 01_100 // +0066h
|
56 |
|
|
// DAS 01_101 // -0066h
|
57 |
|
|
// AAA 01_110 // +0106h
|
58 |
|
|
// AAS 01_111 // -0106h
|
59 |
|
|
//
|
60 |
|
|
// MUL 10_000
|
61 |
|
|
// IMUL 10_001
|
62 |
|
|
//
|
63 |
|
|
// FLAGOP 11_001
|
64 |
|
|
// CBW 11_010
|
65 |
|
|
// CWD 11_011
|
66 |
|
|
// SHF/ROT 1 11_100
|
67 |
|
|
// SHF/ROT n 11_101
|
68 |
|
|
// PASS FLAGS 11_110 // ALUOP <- FLAGS, clear TF, IF
|
69 |
|
|
// PASS RB 11_111 // ALUOP <- RB, FLAGS <- RA
|
70 |
|
|
//
|
71 |
|
|
//
|
72 |
|
|
// FLAGS: X X X X OF DF IF TF | SF ZF X AF X PF X CF
|
73 |
|
|
//
|
74 |
9 |
ndumitrach |
// 09Feb2013 - fixed DAA,DAS bug
|
75 |
16 |
ndumitrach |
// 07Jul2013 - fixed OV/CY flags for IMUL
|
76 |
2 |
ndumitrach |
//////////////////////////////////////////////////////////////////////////////////
|
77 |
|
|
`timescale 1ns / 1ps
|
78 |
|
|
|
79 |
|
|
module Next186_ALU(
|
80 |
|
|
input [15:0] RA,
|
81 |
|
|
input [15:0] RB,
|
82 |
|
|
input [15:0] TMP16,
|
83 |
|
|
input [15:0] FETCH23,
|
84 |
|
|
input [15:0]FIN,
|
85 |
|
|
input [4:0]ALUOP,
|
86 |
|
|
input [2:0]EXOP,
|
87 |
|
|
input [3:0]FLAGOP,
|
88 |
|
|
input WORD,
|
89 |
|
|
input INC2,
|
90 |
|
|
output reg [15:0]FOUT,
|
91 |
|
|
output reg [15:0]ALUOUT,
|
92 |
|
|
output reg ALUCONT,
|
93 |
|
|
output NULLSHIFT,
|
94 |
|
|
output COUT,
|
95 |
|
|
input [2:0]STAGE,
|
96 |
|
|
input CLK
|
97 |
|
|
);
|
98 |
|
|
|
99 |
|
|
reg CPLOP2;
|
100 |
|
|
wire [15:0]SUMOP1 = ALUOP[3:1] == 3'b101 ? 16'h0000 : RA; // NEG, NOT
|
101 |
|
|
reg [15:0]SUMOP21;
|
102 |
|
|
wire [15:0]SUMOP2 = CPLOP2 ? ~SUMOP21 : SUMOP21;
|
103 |
|
|
wire SCIN1 = ALUOP[3:1] == 3'b001 ? FIN[0] : ALUOP[3:0] == 4'b1010 ? 1'b1 : 1'b0; //ADD/ADC, NOT
|
104 |
|
|
wire SCIN = CPLOP2 ? ~SCIN1 : SCIN1; // carry in
|
105 |
|
|
wire SC16OUT;
|
106 |
|
|
wire SC8OUT;
|
107 |
|
|
wire AF;
|
108 |
|
|
wire [15:0]SUMOUT;
|
109 |
|
|
wire parity = ~^ALUOUT[7:0];
|
110 |
|
|
wire zero8 = ~|ALUOUT[7:0];
|
111 |
|
|
wire zero = ~|ALUOUT[15:0];
|
112 |
|
|
wire overflow8 = (SUMOP1[7] & SUMOP2[7] & !SUMOUT[7]) | (!SUMOP1[7] & !SUMOP2[7] & SUMOUT[7]);
|
113 |
|
|
wire overflow = (SUMOP1[15] & SUMOP2[15] & !SUMOUT[15]) | (!SUMOP1[15] & !SUMOP2[15] & SUMOUT[15]);
|
114 |
9 |
ndumitrach |
wire LONIBBLE = (RA[3:0] > 4'h9) || FIN[4];
|
115 |
|
|
wire HINIBBLE = (RA[7:0] > 8'h99) || FIN[0];
|
116 |
2 |
ndumitrach |
|
117 |
|
|
// ADDER
|
118 |
|
|
assign {AF, SUMOUT[3:0]} = SUMOP1[3:0] + SUMOP2[3:0] + SCIN;
|
119 |
|
|
assign {SC8OUT, SUMOUT[7:4]} = SUMOP1[7:4] + SUMOP2[7:4] + AF;
|
120 |
|
|
assign {SC16OUT, SUMOUT[15:8]} = SUMOP1[15:8] + SUMOP2[15:8] + SC8OUT;
|
121 |
|
|
assign COUT = (WORD ? SC16OUT : SC8OUT) ^ CPLOP2;
|
122 |
|
|
|
123 |
|
|
// SHIFTER
|
124 |
|
|
reg [4:0]SHNOPT; // optimized shift
|
125 |
|
|
wire [4:0]SHN = {STAGE[2:1] ^ SHNOPT[4:3], SHNOPT[2:0]};
|
126 |
|
|
assign NULLSHIFT = ~|SHNOPT;
|
127 |
|
|
wire [2:0]COUNT = |SHN[4:3] ? 0 : SHN[2:0];
|
128 |
|
|
wire [2:0]SCOUNT = (EXOP[0] ? COUNT[2:0] : 3'b000) - (EXOP[0] ? 3'b001 : COUNT[2:0]);
|
129 |
|
|
reg [7:0]SHEX;
|
130 |
|
|
wire [17:0]SHBAR = (EXOP[0] ? {1'bx, SHEX, WORD ? RA[15:8]: SHEX, RA[7:0]} : {RA, SHEX, 1'bx}) >> SCOUNT;
|
131 |
|
|
|
132 |
|
|
// MULTIPLIER
|
133 |
|
|
wire signed [16:0]MULOP1 = WORD ? {ALUOP[0] & TMP16[15], TMP16} : {ALUOP[0] ? {9{TMP16[7]}} : 9'b000000000, TMP16[7:0]};
|
134 |
|
|
wire signed [16:0]MULOP2 = WORD ? {ALUOP[0] & FETCH23[15], FETCH23} : {ALUOP[0] ? {9{FETCH23[7]}} : 9'b000000000, FETCH23[7:0]};
|
135 |
|
|
wire signed [31:0]MUL = MULOP1 * MULOP2;
|
136 |
|
|
|
137 |
|
|
always @* begin
|
138 |
|
|
FOUT[15:8] = {4'bxxxx, WORD ? overflow : overflow8, FIN[10:8]};
|
139 |
|
|
FOUT[7] = WORD ? ALUOUT[15] : ALUOUT[7];
|
140 |
|
|
FOUT[6] = WORD ? zero : zero8;
|
141 |
|
|
FOUT[5] = 1'bx;
|
142 |
|
|
FOUT[4] = AF ^ CPLOP2;
|
143 |
|
|
FOUT[3] = 1'bx;
|
144 |
|
|
FOUT[2] = parity;
|
145 |
|
|
FOUT[1] = 1'bx;
|
146 |
|
|
FOUT[0] = COUT;
|
147 |
|
|
ALUOUT = 16'hxxxx;
|
148 |
|
|
ALUCONT = 1'bx;
|
149 |
|
|
|
150 |
|
|
case(ALUOP[3:0]) // complement second operand
|
151 |
|
|
4'b0000, 4'b0010, 4'b1000, 4'b1100, 4'b1110: CPLOP2 = 1'b0; // ADD, ADC, INC, DEC, DAA, AAA
|
152 |
|
|
default: CPLOP2 = 1'b1;
|
153 |
|
|
endcase
|
154 |
|
|
|
155 |
|
|
case(ALUOP[3:0])
|
156 |
|
|
4'b1000, 4'b1001: SUMOP21 = INC2 && WORD ? 2 : 1; // INC/DEC
|
157 |
|
|
4'b1100, 4'b1101: SUMOP21 = {9'b000000000, HINIBBLE, HINIBBLE, 2'b00, LONIBBLE, LONIBBLE, 1'b0};
|
158 |
|
|
4'b1110, 4'b1111: SUMOP21 = {7'b0000000, LONIBBLE, 5'b00000, LONIBBLE, LONIBBLE, 1'b0};
|
159 |
|
|
default: SUMOP21 = RB;
|
160 |
|
|
endcase
|
161 |
|
|
|
162 |
|
|
if(ALUOP[0])
|
163 |
|
|
case({WORD, EXOP[2:1]})
|
164 |
|
|
3'b000: SHNOPT = {2'b00, RB[2:0]};
|
165 |
|
|
3'b001, 3'b101: SHNOPT = RB[4:0];
|
166 |
|
|
3'b010, 3'b011: SHNOPT = |RB[4:3] ? 5'b01000 : RB[4:0];
|
167 |
|
|
3'b100: SHNOPT = {1'b0, RB[3:0]};
|
168 |
|
|
3'b110, 3'b111: SHNOPT = RB[4] ? 5'b10000 : RB[4:0];
|
169 |
|
|
endcase
|
170 |
|
|
else SHNOPT = 5'b00001;
|
171 |
|
|
|
172 |
|
|
case({WORD, EXOP})
|
173 |
|
|
4'b1000: SHEX = RA[15:8]; // ROL16
|
174 |
|
|
4'b0010: SHEX = {FIN[0], RA[7:1]}; // RCL8
|
175 |
|
|
4'b0011, 4'b1011: SHEX = {RA[6:0], FIN[0]}; // RCR8, RCR16
|
176 |
|
|
4'b0111: SHEX = {8{RA[7]}}; // SAR8
|
177 |
|
|
4'b0000, 4'b0001, 4'b1001: SHEX = RA[7:0]; // ROL8, ROR8, ROR16
|
178 |
|
|
4'b1010: SHEX = {FIN[0], RA[15:9]}; // RCL16
|
179 |
|
|
4'b1111: SHEX = {8{RA[15]}}; // SAR16
|
180 |
|
|
default: SHEX = 8'h00; // SHL16, SHR16, SHL8, SHR8
|
181 |
|
|
endcase
|
182 |
|
|
|
183 |
|
|
case(ALUOP)
|
184 |
|
|
5'b00000, 5'b00010, 5'b00011, 5'b00101, 5'b00111, 5'b01010, 5'b01011: ALUOUT = SUMOUT; // ADD, ADC, SBB, SUB, CMP, NOT, NEG
|
185 |
|
|
5'b00001: begin // OR
|
186 |
|
|
ALUOUT = RA | RB;
|
187 |
|
|
FOUT[0] = 1'b0;
|
188 |
|
|
FOUT[11] = 1'b0;
|
189 |
|
|
FOUT[4] = FIN[4];
|
190 |
|
|
end
|
191 |
|
|
5'b00100: begin // AND
|
192 |
|
|
ALUOUT = RA & RB;
|
193 |
|
|
FOUT[0] = 1'b0;
|
194 |
|
|
FOUT[11] = 1'b0;
|
195 |
|
|
FOUT[4] = FIN[4];
|
196 |
|
|
end
|
197 |
|
|
5'b00110: begin // XOR
|
198 |
|
|
ALUOUT = RA ^ RB;
|
199 |
|
|
FOUT[0] = 1'b0;
|
200 |
|
|
FOUT[11] = 1'b0;
|
201 |
|
|
FOUT[4] = FIN[4];
|
202 |
|
|
end
|
203 |
|
|
5'b01000, 5'b01001: begin // INC, DEC
|
204 |
|
|
ALUOUT = SUMOUT;
|
205 |
|
|
FOUT[0] = FIN[0];
|
206 |
|
|
end
|
207 |
|
|
5'b01100, 5'b01101: begin // DAA, DAS
|
208 |
|
|
ALUOUT = SUMOUT;
|
209 |
9 |
ndumitrach |
FOUT[0] = HINIBBLE;
|
210 |
|
|
FOUT[4] = LONIBBLE;
|
211 |
2 |
ndumitrach |
end
|
212 |
|
|
5'b01110, 5'b01111: begin // AAA, AAS
|
213 |
|
|
ALUOUT = {SUMOUT[15:8], 4'b0000, SUMOUT[3:0]};
|
214 |
|
|
FOUT[0] = LONIBBLE;
|
215 |
|
|
FOUT[4] = LONIBBLE;
|
216 |
|
|
end
|
217 |
|
|
5'b10000, 5'b10001 : begin // MUL, IMUL
|
218 |
|
|
ALUOUT = STAGE[1] ? MUL[31:16] : MUL[15:0];
|
219 |
16 |
ndumitrach |
FOUT[0] = WORD ? !zero | (ALUOP[0] & MUL[15]): |MUL[15:8] | (ALUOP[0] & MUL[7]); //07Jul2013 - fixed OV/CY flags for IMUL
|
220 |
2 |
ndumitrach |
FOUT[11] = FOUT[0];
|
221 |
|
|
end
|
222 |
|
|
5'b11001: begin // flag op
|
223 |
|
|
FOUT[11:8] = FIN[11:8];
|
224 |
|
|
FOUT[7] = FIN[7];
|
225 |
|
|
FOUT[6] = FIN[6];
|
226 |
|
|
FOUT[4] = FIN[4];
|
227 |
|
|
FOUT[2] = FIN[2];
|
228 |
|
|
FOUT[0] = FIN[0];
|
229 |
|
|
case(FLAGOP)
|
230 |
|
|
4'b1000: FOUT[0] = 1'b0; // CLC
|
231 |
|
|
4'b0101: FOUT[0] = !FIN[0]; // CMC
|
232 |
|
|
4'b1001: FOUT[0] = 1'b1; // STC
|
233 |
|
|
4'b1100: FOUT[10] = 1'b0; // CLD
|
234 |
|
|
4'b1101: FOUT[10] = 1'b1; // STD
|
235 |
|
|
4'b1010: FOUT[9] = 1'b0; // CLI
|
236 |
|
|
default: FOUT[9] = 1'b1; // STI
|
237 |
|
|
endcase
|
238 |
|
|
end
|
239 |
|
|
5'b11010: ALUOUT[7:0] = {8{RB[7]}};
|
240 |
|
|
5'b11011: ALUOUT[15:0] = {16{RB[15]}};
|
241 |
|
|
5'b11100, 5'b11101: begin // ROT/SHF
|
242 |
|
|
ALUOUT = SHBAR[16:1];
|
243 |
|
|
FOUT[0] = EXOP[0] ? SHBAR[0] : WORD ? SHBAR[17] : SHBAR[9];
|
244 |
|
|
FOUT[11] = WORD ? RA[15] ^ ALUOUT[15] : RA[7] ^ ALUOUT[7]; // OF is defined only for 1bit rotate/shift
|
245 |
|
|
if(!EXOP[2]) begin // ROT
|
246 |
|
|
FOUT[7] = FIN[7];
|
247 |
|
|
FOUT[6] = FIN[6];
|
248 |
|
|
FOUT[4] = FIN[4];
|
249 |
|
|
FOUT[2] = FIN[2];
|
250 |
|
|
end
|
251 |
|
|
case({SHN[4:3], STAGE[2:1]})
|
252 |
|
|
4'b0100, 4'b1101, 4'b0110: ALUCONT = |SHN[2:0];
|
253 |
|
|
4'b1000, 4'b1100, 4'b1001: ALUCONT = 1'b1;
|
254 |
|
|
default: ALUCONT = 1'b0;
|
255 |
|
|
endcase
|
256 |
|
|
end
|
257 |
|
|
|
258 |
|
|
5'b11110: begin
|
259 |
|
|
ALUOUT = FIN;
|
260 |
|
|
FOUT[11] = FIN[11];
|
261 |
|
|
FOUT[9] = 1'b0; // IF
|
262 |
|
|
FOUT[8] = 1'b0; // TF
|
263 |
|
|
FOUT[7] = FIN[7];
|
264 |
|
|
FOUT[6] = FIN[6];
|
265 |
|
|
FOUT[4] = FIN[4];
|
266 |
|
|
FOUT[2] = FIN[2];
|
267 |
|
|
FOUT[0] = FIN[0];
|
268 |
|
|
end
|
269 |
|
|
5'b11111: begin
|
270 |
|
|
ALUOUT = RB;
|
271 |
|
|
FOUT = {WORD ? RA[15:8] : FIN[15:8], RA[7:0]};
|
272 |
|
|
end
|
273 |
|
|
endcase
|
274 |
|
|
end
|
275 |
|
|
|
276 |
|
|
endmodule
|
277 |
|
|
|
278 |
|
|
// 0000 - BX+SI+DISP
|
279 |
|
|
// 0001 - BX+DI+DISP
|
280 |
|
|
// 0010 - BP+SI+DISP
|
281 |
|
|
// 0011 - BP+DI+DISP
|
282 |
|
|
// 0100 - SI+DISP
|
283 |
|
|
// 0101 - DI+DISP
|
284 |
|
|
// 0110 - BP+DISP
|
285 |
|
|
// 0111 - BX+DISP
|
286 |
|
|
// 1000 - SP-2
|
287 |
|
|
// 1001 - SP+2
|
288 |
|
|
// 1010 - BX+AL
|
289 |
|
|
// 1011 - TMP16+2
|
290 |
|
|
// 1100 -
|
291 |
|
|
// 1101 - SP+2+DISP
|
292 |
|
|
// 1110 - DISP[7:0]<<2
|
293 |
|
|
// 1111 - PIO
|
294 |
|
|
module Next186_EA(
|
295 |
|
|
input [15:0] SP,
|
296 |
|
|
input [15:0] BX,
|
297 |
|
|
input [15:0] BP,
|
298 |
|
|
input [15:0] SI,
|
299 |
|
|
input [15:0] DI,
|
300 |
|
|
input [15:0] PIO,
|
301 |
|
|
input [15:0] TMP16,
|
302 |
|
|
input [7:0] AL,
|
303 |
|
|
input [15:0] AIMM,
|
304 |
|
|
input [3:0]EAC,
|
305 |
|
|
output [15:0] ADDR16
|
306 |
|
|
);
|
307 |
|
|
|
308 |
|
|
reg [15:0]OP1;
|
309 |
|
|
reg [15:0]OP2;
|
310 |
|
|
reg [15:0]OP3;
|
311 |
|
|
|
312 |
|
|
always @* begin
|
313 |
|
|
case(EAC)
|
314 |
|
|
4'b0000, 4'b0001, 4'b0111, 4'b1010: OP1 = BX;
|
315 |
|
|
4'b0010, 4'b0011, 4'b0110: OP1 = BP;
|
316 |
|
|
4'b1000, 4'b1001, 4'b1101: OP1 = SP;
|
317 |
|
|
4'b1011: OP1 = TMP16;
|
318 |
|
|
default: OP1 = 16'h0000;
|
319 |
|
|
endcase
|
320 |
|
|
case(EAC)
|
321 |
|
|
4'b0000, 4'b0010, 4'b0100: OP2 = SI;
|
322 |
|
|
4'b0001, 4'b0011, 4'b0101: OP2 = DI;
|
323 |
|
|
4'b1001, 4'b1011, 4'b1101: OP2 = 16'h0002; // SP/TMP16 + 2
|
324 |
|
|
default: OP2 = 16'h0000;
|
325 |
|
|
endcase
|
326 |
|
|
case(EAC)
|
327 |
|
|
4'b1000: OP3 = 16'hfffe; // SP - 2
|
328 |
|
|
4'b1010: OP3 = {8'b00000000, AL}; // XLAT
|
329 |
|
|
4'b1001, 4'b1011: OP3 = 16'h0000; // SP/TMP16 + 2
|
330 |
|
|
4'b1110: OP3 = {6'b000000, AIMM[7:0], 2'b00}; // int
|
331 |
|
|
4'b1111: OP3 = PIO; // in,out
|
332 |
|
|
default: OP3 = AIMM;
|
333 |
|
|
endcase
|
334 |
|
|
end
|
335 |
|
|
|
336 |
|
|
assign ADDR16 = OP1 + OP2 + OP3;
|
337 |
|
|
endmodule
|