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

Subversion Repositories nextz80

[/] [nextz80/] [trunk/] [NextZ80CPU.v] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ndumitrach
//////////////////////////////////////////////////////////////////////////////////
2
//
3
// This file is part of the NextZ80 project
4
// http://www.opencores.org/cores/nextz80/
5
//
6
// Filename: NextZ80CPU.v
7
// Description: Implementation of Z80 compatible CPU
8
// Version 1.0
9
// Creation date: 28Jan2011 - 18Mar2011
10 14 ndumitrach
// Updated: 04Jan2019 - single file
11 2 ndumitrach
//
12
// Author: Nicolae Dumitrache 
13
// e-mail: ndumitrache@opencores.org
14
//
15
/////////////////////////////////////////////////////////////////////////////////
16
// 
17
// Copyright (C) 2011 Nicolae Dumitrache
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
// Comments:
43
//
44
//      NextZ80 processor features:
45
//              All documented/undocumented intstructions are implemented
46
//              All documented/undocumented flags are implemented
47
//              All (doc/undoc)flags are changed accordingly by all (doc/undoc)instructions. 
48
//                      The block instructions (LDx, CPx, INx, OUTx) have only the documented effects on flags. 
49
//                      The Bit n,(IX/IY+d) and BIT n,(HL) undocumented flags XF and YF are implemented like the BIT n,r and not actually like on the real Z80 CPU.
50
//              All interrupt modes implemented: NMI, IM0, IM1, IM2
51
//              R register available
52
//              Fast conditional jump/call/ret takes only 1 T state if not executed
53
//              Fast block instructions: LDxR - 3 T states/byte, INxR/OTxR - 2 T states/byte, CPxR - 4 T states / byte
54
//              Each CPU machine cycle takes (mainly) one clock T state. This makes this processor over 4 times faster than a Z80 at the same 
55
//                      clock frequency (some instructions are up to 10 times faster). 
56 14 ndumitrach
//              Up to 70MHZ on Spartan6 speed grade -2, ~800 LUT6
57 2 ndumitrach
//              Tested with ZEXDOC (fully compliant).
58
//              Tested with ZEXALL (all OK except CPx(R), LDx(R), BIT n, (IX/IY+d), BIT n, (HL) - fail because of the un-documented XF and YF flags).
59
// 
60
///////////////////////////////////////////////////////////////////////////////////
61
`timescale 1ns / 1ps
62
 
63
module NextZ80
64
(
65 14 ndumitrach
                input   [7:0]DI,
66
                input CLK,
67
                input RESET,
68
                input INT,
69
                input NMI,
70
                input   WAIT,
71
 
72
                output [7:0]DO,
73
                output [15:0]ADDR,
74 2 ndumitrach
                output reg WR,
75
                output reg MREQ,
76
                output reg IORQ,
77
                output reg HALT,
78 14 ndumitrach
                output reg M1
79 2 ndumitrach
);
80
 
81
// connections and registers
82
        reg     [9:0] CPUStatus = 0;      // 0=AF-AF', 1=HL-HL', 2=DE-HL, 3=DE'-HL', 4=HL-X, 5=IX-IY, 6=IFF1,7=IFF2, 9:8=IMODE
83
        wire    [7:0] ALU8FLAGS;
84
        wire    [7:0]    FLAGS;
85
        wire    [7:0] ALU80;
86
        wire    [7:0] ALU81;
87
        wire    [15:0]ALU160;
88
        wire    [7:0] ALU161;
89
        wire    [15:0]ALU8OUT;
90
 
91
        reg     [9:0]    FETCH = 0;
92
        reg     [2:0]    STAGE = 0;
93
        wire    [5:0]    opd;
94
        wire    [2:0] op16;
95
        wire    op0mem = FETCH[2:0] == 6;
96
        wire    op1mem = FETCH[5:3] == 6;
97
        reg     [1:0]fetch98;
98
 
99
// stage status
100
        reg     [1:0]DO_SEL;                     // ALU80 - th - flags - ALU8OUT[7:0]
101
        reg     ALU160_SEL;                             // regs - pc
102
        reg     DINW_SEL;                               // ALU8OUT - DI
103
        reg     [5:0]WE;                                 // 5 = flags, 4 = PC, 3 = SP, 2 = tmpHI, 1 = hi, 0 = lo
104
        reg     [4:0] ALU8OP;
105
        reg     [2:0] ALU16OP;
106
        reg     next_stage;
107
        reg     [3:0]REG_WSEL;
108
        reg     [3:0]REG_RSEL;
109
        reg     [11:0]status;                    // 0=AF-AF', 1=HL-HL', 2=DE-HL, 3=DE'-HL', 4=HL-X, 5=IX-IY, 7:6=IFFVAL, 9:8=imode, 10=setIMODE, 11=set IFFVAL
110
// FETCH[5:3]: 000 NZ, 001 Z, 010 NC, 011 C, 100 PO, 101 PE, 110 P, 111 M
111
        wire    [7:0]FlagMux = {FLAGS[7], !FLAGS[7], FLAGS[2], !FLAGS[2], FLAGS[0], !FLAGS[0], FLAGS[6], !FLAGS[6]};
112
        reg     tzf;
113
        reg     FNMI = 0, SNMI = 0;
114
        reg     SRESET = 0;
115
        reg     SINT = 0;
116
        wire    [2:0]intop = FETCH[1] ? 4 : (FETCH[0] ? 5 : 6);
117
        reg     xmask;
118
 
119
        Z80Reg CPU_REGS (
120
                 .rstatus(CPUStatus[7:0]),
121
                 .M1(M1),
122
                 .WE(WE),
123
                 .CLK(CLK),
124
                 .ALU8OUT(ALU8OUT),
125
                 .DI(DI),
126
                 .ADDR(ADDR),
127
                 .CONST(FETCH[7] ? {2'b00, FETCH[5:3], 3'b000} : 8'h66),        // RST/NMI address
128
                 .ALU8FLAGS(ALU8FLAGS),
129
                 .DO_SEL(DO_SEL),
130
                 .ALU160_sel(ALU160_SEL),
131
                 .REG_WSEL(REG_WSEL),
132
                 .REG_RSEL(REG_RSEL),
133
                 .DINW_SEL(DINW_SEL),
134
                 .XMASK(xmask),
135
                 .ALU16OP(ALU16OP),                     // used for post increment for ADDR, SP mux re-direct
136 14 ndumitrach
                 .WAIT(WAIT),
137
 
138
                 .DO(DO),
139
                 .ALU80(ALU80),
140
                 .ALU81(ALU81),
141
                 .ALU160(ALU160),
142
                 .ALU161(ALU161),
143
                 .FLAGS(FLAGS)
144 2 ndumitrach
                 );
145
 
146
        ALU8 CPU_ALU8 (
147
                 .D0(ALU80),
148
                 .D1(ALU81),
149
                 .FIN(FLAGS),
150
                 .OP(ALU8OP),
151
                 .EXOP(FETCH[8:3]),
152
                 .LDIFLAGS(REG_WSEL[2]),        // inc16 HL
153 14 ndumitrach
                 .DSTHI(!REG_WSEL[0]),
154
 
155
                 .FOUT(ALU8FLAGS),
156
                 .ALU8DOUT(ALU8OUT)
157 2 ndumitrach
                 );
158
 
159
        ALU16 CPU_ALU16 (
160
                 .D0(ALU160),
161
                 .D1(ALU161),
162 14 ndumitrach
                 .OP(ALU16OP),
163
 
164
                 .DOUT(ADDR)
165 2 ndumitrach
                 );
166
 
167
        always @(posedge CLK)
168
                if(!WAIT) begin
169
                        SRESET <= RESET;
170
                        SNMI <= NMI;
171
                        SINT <= INT;
172
                        if(!SNMI) FNMI <= 0;
173
                        if(SRESET) FETCH <= 10'b1110000000;
174
                        else
175
                                if(FETCH[9:6] == 4'b1110) {FETCH[9:7]} <= 3'b000;       // exit RESET state
176
                                else begin
177 13 ndumitrach
                                        if(M1 || (fetch98 == 2'b10))    // [DD/FD CB disp op] -  M1 is inactive during <op> byte read, but FETCH is performed
178 2 ndumitrach
                                                case({MREQ, CPUStatus[9:8]})
179
                                                        3'b000, 3'b001, 3'b100, 3'b101, 3'b110, 3'b111: FETCH <= {fetch98, DI};
180
                                                        3'b010: FETCH <= {fetch98, 8'hff};      // IM1 - RST38
181
                                                        3'b011: ; // IM2 - get addrLO
182
                                                endcase
183
                                        if(~|{next_stage, fetch98[1:0], status[4]})                              // INT or NMI sample
184
                                                if(SNMI & !FNMI) begin                                          // NMI posedge
185
                                                        {FETCH[9:6], FETCH[1:0]} <= {4'b1101, HALT, M1};
186
                                                        FNMI <= 1;      // NMI acknowledged
187
                                                end else if(SINT & CPUStatus[6] & !status[11]) {FETCH[9:6], FETCH[1:0]} <= {4'b1100, HALT, M1};  // INT request
188
                                end
189
                        if(next_stage) STAGE <= STAGE + 3'b001;
190
                        else STAGE <= 0;
191
                        if(status[4]) CPUStatus[5:4] <= status[5:4];
192
                        else if(~|{next_stage, fetch98[1]} | fetch98[0]) CPUStatus[4] <= 1'b0;           // clear X
193
                        CPUStatus[3:0] <= CPUStatus[3:0] ^ status[3:0];
194
                        if(status[11]) CPUStatus[7:6] <= status[7:6];   // IFF2:1
195
                        if(status[10]) CPUStatus[9:8] <= status[9:8];   // IMM
196
                        tzf <= ALU8FLAGS[6];
197
                end
198
 
199
        assign opd[0] = FETCH[0] ^ &FETCH[2:1];
200
        assign opd[2:1] = FETCH[2:1];
201
        assign opd[3] = FETCH[3] ^ &FETCH[5:4];
202
        assign opd[5:4] = FETCH[5:4];
203
        assign op16[2:0] = &FETCH[5:4] ? 3'b101 : {1'b0, FETCH[5:4]};
204
 
205
        always @* begin
206
                DO_SEL  = 2'bxx;                                                // ALU80 - th - flags - ALU8OUT[7:0]
207
                ALU160_SEL = 1'bx;                                      // regs - pc
208
                DINW_SEL = 1'bx;                                                // ALU8OUT - DI
209
                WE              = 6'bxxxxxx;                            // 5 = flags, 4 = PC, 3 = SP, 2 = tmpHI, 1 = hi, 0 = lo
210
                ALU8OP  = 5'bxxxxx;
211
                ALU16OP = 3'b000;                                       // NOP, post inc
212
                next_stage = 0;
213
                REG_WSEL        = 4'bxxxx;
214 14 ndumitrach
                REG_RSEL        = 4'bx0xx;                                      // prevents default 4'b0100 which leads to incorrect P flag value in some cases (like RLA)
215 2 ndumitrach
                M1              = 1;
216
                MREQ            = 1;
217
                WR                      = 0;
218
 
219
                HALT = 0;
220
                IORQ = 0;
221
                status  = 12'b00xxxxx00000;
222
                fetch98 = 2'b00;
223
 
224
                case({FETCH[7:6], op1mem, op0mem})
225
                        4'b0000, 4'b0001, 4'b0010, 4'b0011, 4'b0100, 4'b1000, 4'b1100: xmask = 1;
226
                        default: xmask = 0;
227
                endcase
228
 
229
                case(FETCH[9:6])
230
//------------------------------------------- block 00 ----------------------------------------------------
231
                        4'b0000:
232
                                case(FETCH[3:0])
233
//                              -----------------------         NOP, EX AF, AF', DJNZ, JR, JR c --------------------
234
                                        4'b0000, 4'b1000:
235
                                                case(FETCH[5:4])
236
                                                        2'b00: begin                                    // NOP, EX AF, AF'
237
                                                                DO_SEL  = 2'bxx;
238
                                                                ALU160_SEL = 1;                 // PC
239
                                                                WE              = 6'b010x00;    // PC
240
                                                                status[0] = FETCH[3];
241
                                                        end
242
                                                        2'b01:
243
                                                                if(!STAGE[0]) begin              // DJNZ, JR - stage1
244
                                                                        ALU160_SEL = 1;                         // pc
245
                                                                        WE              = 6'b010100;            // PC, tmpHI
246
                                                                        if(!FETCH[3]) begin
247 14 ndumitrach
                                                                                ALU8OP  = 5'b01010;             // DEC, for tzf only
248
                                                                                REG_WSEL        = 4'b0000;              // B
249 2 ndumitrach
                                                                        end
250
                                                                        next_stage = 1;
251
                                                                        M1              = 0;
252
                                                                end else if(FETCH[3]) begin     // JR - stage2
253
                                                                        ALU160_SEL = 1;                         // pc
254
                                                                        WE              = 6'b010x00;            // PC
255
                                                                        ALU16OP = 3;                                    // ADD
256 14 ndumitrach
                                                                end else begin                                          // DJNZ - stage2
257 2 ndumitrach
                                                                        ALU160_SEL = 1;                         // pc
258
                                                                        DINW_SEL = 0;                                    // ALU8OUT
259
                                                                        WE              = 6'b010x10;            // PC, hi
260
                                                                        ALU8OP  = 5'b01010;                     // DEC
261
                                                                        ALU16OP = tzf ? 3'd0 : 3'd3;            // NOP/ADD
262
                                                                        REG_WSEL        = 4'b0000;                      // B
263
                                                                end
264
                                                        2'b10, 2'b11:                                                   // JR cc, stage1, stage2
265
                                                                case({STAGE[0], FlagMux[{1'b0, FETCH[4:3]}]})
266
                                                                        2'b00, 2'b11: begin
267 14 ndumitrach
                                                                                ALU160_SEL = 1;                 // pc
268
                                                                                WE              = 6'b010x00;    // PC
269 2 ndumitrach
                                                                                ALU16OP = STAGE[0] ? 3'd3 : 3'd1;                // ADD/ INC, post inc
270
                                                                        end
271
                                                                        2'b01: begin
272 14 ndumitrach
                                                                                ALU160_SEL = 1;                 // pc
273
                                                                                WE              = 6'b010100;    // PC, tmpHI
274 2 ndumitrach
                                                                                next_stage = 1;
275
                                                                                M1              = 0;
276
                                                                        end
277
                                                                endcase
278
                                                endcase
279
//                              -----------------------         LD rr,nn  --------------------
280
                                        4'b0001:                        // LD rr,nn, stage1
281
                                                case({STAGE[1:0], op16[2]})
282
                                                        3'b00_0, 3'b00_1, 3'b01_0, 3'b01_1: begin                       // LD rr,nn, stage1,2
283
                                                                ALU160_SEL = 1;                 // pc
284
                                                                DINW_SEL = 1;                           // DI
285
                                                                WE              = {4'b010x, STAGE[0] ? 1'b1 : 1'bx, !STAGE[0]};   // PC, lo/HI
286
                                                                next_stage = 1;
287
                                                                REG_WSEL        = {op16, 1'bx};
288
                                                                M1              = 0;
289
                                                        end
290
                                                        3'b10_0, 3'b11_1: begin         // BC, DE, HL, stage3, SP stage4
291
                                                                ALU160_SEL = 1;                 // pc
292
                                                                WE              = 6'b010x00;    // PC
293
                                                        end
294 14 ndumitrach
                                                        3'b10_1: begin                                  // SP stage3
295 2 ndumitrach
                                                                ALU160_SEL = 0;                  // regs
296
                                                                WE              = 6'b001x00;    // SP
297
                                                                ALU16OP = 4;                            // NOP
298
                                                                next_stage = 1;
299
                                                                REG_RSEL        = 4'b101x;              // tmpSP
300
                                                                M1              = 0;
301
                                                                MREQ            = 0;
302
                                                        end
303
                                                endcase
304
//                              -----------------------         LD (BC) A -  LD (DE) A - LD (nn) HL, LD (nn),A   --------------------
305
//                              -----------------------         LD A (BC) -  LD A (DE) - LD HL (nn), LD A (nn)   --------------------
306
                                        4'b0010,        4'b1010:
307
                                                case(STAGE[2:0])
308
                                                        3'b000:
309
                                                                if(FETCH[5] == 0) begin                  // LD (BC) A, LD (DE) A - stage1
310 14 ndumitrach
                                                                        if(FETCH[3]) DINW_SEL = 1;      // DI
311
                                                                        else DO_SEL     = 2'b00;                        // ALU80
312 2 ndumitrach
                                                                        ALU160_SEL = 0;                          // regs
313
                                                                        WE              = {4'b000x, FETCH[3], 1'bx};            // hi
314
                                                                        next_stage = 1;
315
                                                                        REG_WSEL        = FETCH[3] ? 4'b011x : 4'b0110; // A
316
                                                                        REG_RSEL        = {op16, 1'bx};
317
                                                                        M1              = 0;
318
                                                                        WR = !FETCH[3];
319
                                                                end else begin                                          // LD (nn) A - LD (nn) HL - stage 1
320
                                                                        ALU160_SEL = 1;                         // PC
321
                                                                        DINW_SEL = 1;                                   // DI
322
                                                                        WE              = 6'b010xx1;            // PC, lo
323
                                                                        next_stage = 1;
324
                                                                        REG_WSEL        = 4'b111x;
325
                                                                        M1              = 0;
326
                                                                end
327
                                                        3'b001:
328
                                                                if(FETCH[5] == 0) begin                  // LD (BC), A, LD (DE), A - stage2
329
                                                                        ALU160_SEL = 1;                         // pc
330
                                                                        WE              = 6'b010x00;            // PC
331
                                                                end else begin                                          // LD (nn),A  - LH (nn),HL - stage 2
332
                                                                        ALU160_SEL = 1;                         // pc
333
                                                                        DINW_SEL = 1;                                   // DI
334
                                                                        WE              = 6'b010x10;            // PC, hi
335
                                                                        next_stage = 1;
336
                                                                        REG_WSEL        = 4'b111x;
337
                                                                        M1              = 0;
338
                                                                end
339
                                                        3'b010: begin
340
                                                                ALU160_SEL = 1'b0;              // regs
341
                                                                REG_RSEL        = 4'b111x;
342
                                                                M1              = 0;
343
                                                                WR                      = !FETCH[3];
344
                                                                next_stage = 1;
345
                                                                if(FETCH[3]) begin              // LD A (nn)  - LD HL (nn) - stage 3
346 14 ndumitrach
                                                                        DINW_SEL = 1;                   // DI
347 2 ndumitrach
                                                                        WE              = {4'b000x, FETCH[4] ? 1'b1 : 1'bx, FETCH[4] ? 1'bx : 1'b1};    // lo/hi
348
                                                                        REG_WSEL = FETCH[4] ? 4'b011x : 4'b010x;        // A or L
349
                                                                end else begin                          // LD (nn),A  - LD (nn),HL - stage 3
350 14 ndumitrach
                                                                        DO_SEL  = 2'b00;                // ALU80
351
                                                                        WE              = 6'b000x00;// nothing
352 2 ndumitrach
                                                                        REG_WSEL = FETCH[4] ? 4'b0110 : 4'b0101;        // A or L
353
                                                                end
354
                                                        end
355
                                                        3'b011:
356
                                                                if(FETCH[4]) begin                      // LD (nn),A - stage 4
357
                                                                        ALU160_SEL = 1;                 // pc
358
                                                                        WE              = 6'b010x00;    // PC
359
                                                                end else begin
360
                                                                        REG_RSEL        = 4'b111x;
361
                                                                        M1              = 0;
362
                                                                        WR                      = !FETCH[3];
363
                                                                        ALU160_SEL = 1'b0;              // regs
364
                                                                        ALU16OP = 1;                            // INC
365
                                                                        next_stage = 1;
366 14 ndumitrach
                                                                        if(FETCH[3]) begin              // LD HL (nn) - stage 4
367
                                                                                DINW_SEL = 1;                   // DI
368
                                                                                WE              = 6'b000x10;// hi
369
                                                                                REG_WSEL = 4'b010x;     // H
370
                                                                        end else begin                          // LD (nn),HL - stage 4
371
                                                                                DO_SEL  = 2'b00;                // ALU80
372
                                                                                WE              = 6'b000x00;// nothing
373
                                                                                REG_WSEL = 4'b0100;     // H
374 2 ndumitrach
                                                                        end
375
                                                                end
376
                                                        3'b100: begin                           // LD (nn),HL - stage 5
377 14 ndumitrach
                                                                ALU160_SEL = 1;         // pc
378
                                                                WE              = 6'b010x00;// PC
379 2 ndumitrach
                                                        end
380
                                                endcase
381
//                              -----------------------         inc/dec rr   --------------------
382
                                        4'b0011, 4'b1011:
383
                                                if(!STAGE[0])
384
                                                        if(op16[2]) begin                               // SP - stage1
385
                                                                ALU160_SEL = 0;                  // regs
386
                                                                WE              = 6'b001x00;    // SP
387
                                                                ALU16OP = {FETCH[3], 1'b0, FETCH[3]};           // post inc, dec
388
                                                                next_stage = 1;
389
                                                                REG_RSEL        = 4'b101x;              // sp
390
                                                                M1              = 0;
391
                                                                MREQ            = 0;
392
                                                        end else begin                                  // BC, DE, HL - stage 1
393
                                                                ALU160_SEL = 1;                 // pc
394
                                                                DINW_SEL = 0;                            // ALU8OUT
395
                                                                WE              = 6'b010x11;    // PC, hi, lo
396
                                                                ALU8OP  = {4'b0111, FETCH[3]};                  // INC16 / DEC16
397
                                                                REG_WSEL        = {op16, 1'b0}; // hi
398
                                                                REG_RSEL        = {op16, 1'b1}; // lo
399
                                                        end
400 14 ndumitrach
                                                else    begin                                           // SP, stage2
401 2 ndumitrach
                                                        ALU160_SEL = 1;                 // pc
402
                                                        WE              = 6'b010x00;    // PC
403
                                                end
404
//                              -----------------------         inc/dec 8  --------------------
405
                                        4'b0100, 4'b0101, 4'b1100, 4'b1101:
406
                                                if(!op1mem) begin                                               //regs
407
                                                        DINW_SEL = 0;                                            // ALU8OUT
408
                                                        ALU160_SEL = 1;                                 // pc
409 14 ndumitrach
                                                        WE              = opd[3] ? 6'b110x01 : 6'b110x10;// flags, PC, hi/lo
410 2 ndumitrach
                                                        ALU8OP  = {3'b010, FETCH[0], 1'b0};              // inc / dec
411
                                                        REG_WSEL        = {1'b0, opd[5:3]};
412
                                                end else case({STAGE[1:0], CPUStatus[4]})
413
                                                        3'b00_0, 3'b01_1: begin                         // (HL) - stage1, (X) - stage2
414
                                                                ALU160_SEL = 0;                                  // regs
415
                                                                DINW_SEL = 1;                                           // DI
416
                                                                WE              = 6'b000001;                    // lo
417
                                                                ALU16OP = CPUStatus[4] ? 3'd3 : 3'd0;
418
                                                                next_stage = 1;
419
                                                                REG_WSEL        = 4'b011x;                              // tmpLO
420
                                                                REG_RSEL        = 4'b010x;                              // HL
421
                                                                M1              = 0;
422
                                                        end
423
                                                        3'b00_1:        begin                                                   // (X) - stage1
424
                                                                ALU160_SEL = 1;                                 // pc
425
                                                                WE              = 6'b010100;                    // PC, tmpHI
426
                                                                next_stage = 1;
427
                                                                M1              = 0;
428
                                                        end
429 14 ndumitrach
                                                        3'b01_0, 3'b10_1: begin                         // (HL) stage2, (X) - stage3
430
                                                                DO_SEL  = 2'b11;                                        // ALU80OUT
431
                                                                ALU160_SEL = 0;                                  // regs
432
                                                                WE              = 6'b100x0x;                    // flags
433 2 ndumitrach
                                                                ALU8OP  = {3'b010, FETCH[0], 1'b0};              // inc / dec
434
                                                                ALU16OP = CPUStatus[4] ? 3'd3 : 3'd0;
435
                                                                next_stage = 1;
436 14 ndumitrach
                                                                REG_WSEL        = 4'b0111;                              // tmpLO
437
                                                                REG_RSEL        = 4'b010x;                              // HL
438 2 ndumitrach
                                                                M1              = 0;
439
                                                                WR                      = 1;
440
                                                        end
441 14 ndumitrach
                                                        3'b10_0, 3'b11_1: begin                         // (HL) - stage3, (X) - stage 4
442
                                                                ALU160_SEL = 1;                                 // pc
443
                                                                WE              = 6'b010x00;                    // PC
444 2 ndumitrach
                                                        end
445
                                                endcase
446
//                              -----------------------         ld r/(HL-X), n  --------------------                                            
447
                                        4'b0110, 4'b1110:
448
                                                case({STAGE[1:0], CPUStatus[4], op1mem})
449
                                                        4'b00_0_0, 4'b00_0_1, 4'b00_1_0, 4'b01_1_1: begin               // r, (HL) - stage1, (X) - stage2 (read n)
450
                                                                ALU160_SEL = 1;                                 // pc
451
                                                                DINW_SEL = 1;                                           // DI
452
                                                                WE              = opd[3] ? 6'b010001 : 6'b010010;                       // PC, hi/lo
453
                                                                next_stage = 1;
454
                                                                REG_WSEL        = {1'b0, opd[5:4], 1'bx};
455
                                                                M1              = 0;
456
                                                        end
457
                                                        4'b01_0_0, 4'b01_1_0, 4'b10_0_1, 4'b11_1_1: begin               // r - stage2, (HL) - stage3, (X) - stage4
458
                                                                ALU160_SEL = 1;                                         // pc
459
                                                                WE              = 6'b010x00;                            // PC
460
                                                        end
461
                                                        4'b01_0_1, 4'b10_1_1: begin                     // (HL) - stage2, (X) - stage3
462
                                                                DO_SEL  = 2'b00;                                                // ALU80
463
                                                                ALU160_SEL = 0;                                          // regs
464
                                                                WE              = 6'b000x0x;                            // nothing
465
                                                                ALU16OP = CPUStatus[4] ? 3'd3 : 3'd0;
466
                                                                next_stage = 1;
467
                                                                REG_WSEL        = 4'b0111;                                      // tmpLO
468
                                                                REG_RSEL        = 4'b010x;                                      // HL
469
                                                                M1              = 0;
470
                                                                WR                      = 1;
471
                                                        end
472
                                                        4'b00_1_1: begin                                                        // (X) - stage1
473
                                                                ALU160_SEL = 1;                                         // pc
474
                                                                WE              = 6'b010100;                            // PC, tmpHI
475
                                                                next_stage = 1;
476
                                                                M1              = 0;
477
                                                        end
478
                                                endcase
479
//                              -----------------------         rlca, rrca, rla, rra, daa, cpl, scf, ccf  --------------------                                          
480
                                        4'b0111, 4'b1111:
481
                                                case(FETCH[5:3])
482
                                                        3'b000, 3'b001, 3'b010, 3'b011, 3'b100, 3'b101: begin           // rlca, rrca, rla, rra, daa, cpl
483
                                                                ALU160_SEL = 1;                                 // pc
484
                                                                DINW_SEL = 0;                                            // ALU8OUT
485
                                                                WE              = 6'b110x1x;                    // flags, PC, hi
486
                                                                ALU8OP  = FETCH[5] ? {2'b01, !FETCH[3], 2'b01} : {3'b110, FETCH[4:3]};
487
                                                                REG_WSEL        = 4'b0110;                              // A
488
                                                        end
489
                                                        3'b110, 3'b111: begin                           // scf, ccf
490
                                                                ALU160_SEL = 1;                                 // pc
491
                                                                DINW_SEL = 0;                                            // ALU8OUT
492
                                                                WE              = 6'b110x0x;                    // flags, PC
493
                                                                ALU8OP  = {4'b1010, !FETCH[3]};
494
                                                        end
495
                                                endcase
496
//                              -----------------------         add 16  --------------------                                            
497
                                        4'b1001:
498
                                                if(!STAGE[0]) begin
499
                                                        DINW_SEL = 0;                                            // ALU8OUT
500
                                                        WE              = 6'b100x01;                    // flags, lo
501
                                                        ALU8OP  = 5'b10000;                             // ADD16LO
502
                                                        next_stage = 1;
503
                                                        REG_WSEL        = 4'b0101;                              // L
504
                                                        REG_RSEL        = {op16, 1'b1};
505
                                                        M1              = 0;
506
                                                        MREQ            = 0;
507
                                                end else begin
508
                                                        ALU160_SEL = 1;                                 // pc
509
                                                        DINW_SEL = 0;                                            // ALU8OUT
510
                                                        WE              = 6'b110x10;                    // flags, PC, hi
511
                                                        ALU8OP  = 5'b10001;                             // ADD16HI
512
                                                        REG_WSEL        = 4'b0100;                              // H
513
                                                        REG_RSEL        = {op16, 1'b0};
514
                                                end
515
                                endcase
516
 
517
// ---------------------------------------------- block 01 LD8 ---------------------------------------------------
518
                        4'b0001:
519
                                case({STAGE[1:0], CPUStatus[4], op1mem, op0mem})
520
                                        5'b00_0_00, 5'b00_1_00,         // LD r, r 1st stage
521
                                        5'b01_0_01,                                             // LD r, (HL) 2nd stage
522
                                        5'b10_1_01:                                             // LD r, (X) 3rd stage
523
                                        begin
524
                                                ALU160_SEL = 1;                 // PC
525
                                                DINW_SEL          = 0;                   // ALU8
526
                                                WE = opd[3] ? 6'b010x01 : 6'b010x10;    // PC and LO or HI
527
                                                ALU8OP = 29;                            // PASS D1
528
                                                REG_WSEL = {1'b0, opd[5:4], 1'bx};
529
                                                REG_RSEL = {1'b0, opd[2:0]};
530
                                        end
531
                                        5'b00_0_01,                                             // LD r, (HL) 1st stage
532
                                        5'b01_1_01:                                             // LD r, (X) 2nd stage
533
                                        begin
534
                                                ALU160_SEL = 0;                  // regs
535
                                                DINW_SEL = 1;                           // DI           
536
                                                WE              = 6'b000x01;    // LO
537
                                                ALU16OP = CPUStatus[4] ? 3'd3 : 3'd0;           // ADD - NOP
538
                                                next_stage = 1;
539
                                                REG_WSEL        = 4'b011x;              // A - tmpLO
540
                                                REG_RSEL = 4'b010x;             // HL
541
                                                M1 = 0;
542
                                        end
543
                                        5'b00_1_01,                                             // LD r, (X) 1st stage
544
                                        5'b00_1_10:                                             // LD (X), r 1st stage
545
                                        begin
546
                                                ALU160_SEL = 1;                 // pc
547
                                                WE              = 6'b010100;    // PC, tmpHI
548
                                                next_stage = 1;
549
                                                M1              = 0;
550
                                        end
551
                                        5'b00_0_10,                                     // LD (HL), r 1st stage
552
                                        5'b01_1_10:                                             // LD (X), r 2nd stage
553
                                        begin
554
                                                DO_SEL  = 0;                             // ALU80
555
                                                ALU160_SEL = 0;                  // regs
556
                                                WE              = 6'b000x00;    // no write
557
                                                ALU16OP = CPUStatus[4] ? 3'd3 : 3'd0;                   // ADD - NOP
558
                                                next_stage = 1;
559
                                                REG_WSEL        = {1'b0, opd[2:0]};
560
                                                REG_RSEL        = 4'b010x;              // HL
561
                                                M1              = 0;
562
                                                WR                      = 1;
563
                                        end
564
                                        5'b01_0_10,                                     // LD (HL), r 2nd stage
565
                                        5'b10_1_10:                                             // LD (X), r 3rd stage
566
                                        begin
567
                                                ALU160_SEL = 1;                 // pc
568
                                                WE              = 6'b010x00;    // PC
569
                                        end
570
                                        5'b00_0_11, 5'b00_1_11: begin   // HALT
571
                                                WE              = 6'b000x00;    // no write
572
                                                M1              = 0;
573
                                                MREQ            = 0;
574
                                                HALT            = 1;
575
                                        end
576
                                endcase
577
// ---------------------------------------------- block 10 arith8 ---------------------------------------------------
578
                        4'b0010:
579
                                case({STAGE[1:0], CPUStatus[4], op0mem})
580
                                        4'b00_0_0, 4'b00_1_0,           // OP r,r 1st stage
581
                                        4'b01_0_1,                                              // OP r, (HL) 2nd stage
582
                                        4'b10_1_1:                                              // OP r, (X) 3rd stage
583
                                        begin
584
                                                ALU160_SEL = 1;                 // pc
585
                                                DINW_SEL = 0;                            // ALU8OUT
586
                                                WE              = {4'b110x, ~&FETCH[5:3], 1'bx};        // flags, PC, hi
587
                                                ALU8OP  = {2'b00, FETCH[5:3]};
588
                                                REG_WSEL        = 4'b0110;              // A
589
                                                REG_RSEL        = {1'b0, opd[2:0]};
590
                                        end
591
                                        4'b00_0_1,                                              // OP r, (HL) 1st stage
592
                                        4'b01_1_1:                                              // OP r, (X) 2nd stage
593
                                        begin
594
                                                ALU160_SEL = 0;                  // HL
595
                                                DINW_SEL = 1;                           // DI
596
                                                WE              = 6'b000x01;    // lo
597
                                                ALU16OP = CPUStatus[4] ? 3'd3 : 3'd0;                   // ADD - NOP
598
                                                next_stage = 1;
599
                                                REG_WSEL        = 4'b011x;              // A-tmpLO
600
                                                REG_RSEL        = 4'b010x;              // HL
601
                                                M1              = 0;
602
                                        end
603
                                        4'b00_1_1:                                              // OP r, (X) 1st stage
604
                                        begin
605
                                                ALU160_SEL = 1;                 // pc
606
                                                WE              = 6'b010100;    // PC, tmpHI
607
                                                next_stage = 1;
608
                                                M1              = 0;
609
                                        end
610
                                endcase
611
//------------------------------------------- block 11 ----------------------------------------------------
612
                        4'b0011:
613
                                case(FETCH[3:0])
614
//                              -----------------------         RET cc --------------------
615
                                        4'b0000, 4'b1000:
616
                                                case(STAGE[1:0])
617
                                                        2'b00, 2'b01:                   // stage1, stage2
618
                                                                if(FlagMux[FETCH[5:3]]) begin   // POP addr
619
                                                                        ALU160_SEL = 0;                          // regs
620
                                                                        DINW_SEL = 1;                                   // DI
621
                                                                        WE              = {4'b001x, STAGE[0] ? 1'b1 : 1'bx, !STAGE[0]};           // SP, lo/hi
622
                                                                        next_stage = 1;
623
                                                                        REG_WSEL        = 4'b111x;                      // tmp16
624
                                                                        REG_RSEL        = 4'b101x;                      // SP
625
                                                                        M1              = 0;
626
                                                                end else begin
627
                                                                        ALU160_SEL = 1;                         // pc
628
                                                                        WE              = 6'b010x00;            // PC
629
                                                                end
630
                                                        2'b10: begin                    // stage3
631
                                                                ALU160_SEL = 0;                                  // regs
632
                                                                WE              = 6'b010x00;                    // PC
633
                                                                REG_RSEL        = 4'b111x;                              // tmp16
634
                                                        end
635
                                                endcase
636
//                              -----------------------         POP --------------------
637
                                        4'b0001:
638
                                                case(STAGE[1:0])
639
                                                        2'b00, 2'b01: begin
640
                                                                if(op16[2]) begin       // AF
641
                                                                        WE              = STAGE[0] ? 6'b101x1x : 6'b001xx1;              // flags, SP, lo/hi
642
                                                                        REG_WSEL        = {3'b011, STAGE[0] ? 1'b1 : 1'bx};
643 14 ndumitrach
                                                                        if(STAGE[0]) ALU8OP      = 30;                                                           // FLAGS <- D0
644 2 ndumitrach
                                                                end else begin          // r16
645
                                                                        WE              = STAGE[0] ? 6'b001x10 : 6'b001xx1;              // SP, lo/hi
646
                                                                        REG_WSEL        = {1'b0, FETCH[5:4], 1'bx};
647
                                                                end
648
                                                                ALU160_SEL = 0;                  // regs
649
                                                                DINW_SEL = 1;                           // DI
650
                                                                next_stage = 1;
651
                                                                REG_RSEL        = 4'b101x;              // SP
652
                                                                M1              = 0;
653
                                                        end
654
                                                        2'b10: begin                                    // stage3
655
                                                                ALU160_SEL = 1;                 // PC
656
                                                                WE              = 6'b010x00;    // PC
657
                                                        end
658
                                                endcase
659
//                              -----------------------         JP cc --------------------
660
                                        4'b0010, 4'b1010:
661
                                                case(STAGE[1:0])
662
                                                        2'b00, 2'b01:   begin                           // stage1,2
663
                                                                if(FlagMux[FETCH[5:3]]) begin
664
                                                                        ALU160_SEL = 1;                                 // pc
665
                                                                        DINW_SEL = 1;                                           // DI
666
                                                                        WE              = {4'b010x, STAGE[0] ? 1'b1 : 1'bx, !STAGE[0]};           // PC, hi/lo
667
                                                                        next_stage = 1;
668
                                                                        REG_WSEL        = 4'b111x;                              // tmp7
669
                                                                        M1              = 0;
670
                                                                end else begin
671
                                                                        ALU160_SEL = 1;                                 // pc
672
                                                                        WE              = 6'b010x00;                    // PC
673
                                                                        ALU16OP = 2;                                            // add2
674
                                                                end
675
                                                        end
676
                                                        2'b10: begin                                            // stage3
677 14 ndumitrach
                                                                ALU160_SEL = 0;                          // regs
678
                                                                WE              = 6'b010x00;            // PC
679
                                                                REG_RSEL        = 4'b111x;                      // tmp7
680 2 ndumitrach
                                                        end
681
                                                endcase
682
//                              -----------------------         JP, OUT (n) A, EX (SP) HL, DI --------------------
683
                                        4'b0011:
684
                                                case(FETCH[5:4])
685
                                                        2'b00:                                  // JP
686
                                                                case(STAGE[1:0])
687
                                                                        2'b00, 2'b01:   begin                           // stage1,2 - read addr
688 14 ndumitrach
                                                                                ALU160_SEL = 1;                         // pc
689
                                                                                DINW_SEL = 1;                                   // DI
690 2 ndumitrach
                                                                                WE              = {4'b010x, STAGE[0] ? 1'b1 : 1'bx, !STAGE[0]};           // PC, hi/lo
691
                                                                                next_stage = 1;
692 14 ndumitrach
                                                                                REG_WSEL        = 4'b111x;                      // tmp7
693 2 ndumitrach
                                                                                M1              = 0;
694
                                                                        end
695
                                                                        2'b10: begin                                            // stage3
696 14 ndumitrach
                                                                                ALU160_SEL = 0;                          // regs
697
                                                                                WE              = 6'b010x00;            // PC
698
                                                                                REG_RSEL        = 4'b111x;                      // tmp7
699 2 ndumitrach
                                                                        end
700
                                                                endcase
701
                                                        2'b01:                                  // OUT (n), a - stage1 - read n
702
                                                                case(STAGE[1:0])
703
                                                                        2'b00: begin
704 14 ndumitrach
                                                                                ALU160_SEL = 1;                         // pc
705
                                                                                DINW_SEL = 1;                                   // DI
706
                                                                                WE              = 6'b010x01;            // PC, lo
707 2 ndumitrach
                                                                                next_stage = 1;
708 14 ndumitrach
                                                                                REG_WSEL        = 4'b011x;                      // tmpLO
709 2 ndumitrach
                                                                                M1              = 0;
710
                                                                        end
711
                                                                        2'b01: begin            // stage2 - OUT
712 14 ndumitrach
                                                                                DO_SEL  = 2'b00;                                // ALU80
713
                                                                                ALU160_SEL = 0;                          // regs
714
                                                                                WE              = 6'b000x00;            // nothing
715 2 ndumitrach
                                                                                next_stage = 1;
716 14 ndumitrach
                                                                                REG_WSEL        = 4'b0110;                      // A
717
                                                                                REG_RSEL        = 4'b011x;                      // A-tmpLO
718 2 ndumitrach
                                                                                M1              = 0;
719
                                                                                MREQ            = 0;
720
                                                                                WR              = 1;
721
                                                                                IORQ            = 1;
722
                                                                        end
723
                                                                        2'b10: begin            // stage3 - fetch
724 14 ndumitrach
                                                                                ALU160_SEL = 1;                         // PC
725
                                                                                WE              = 6'b010x00;            // PC
726 2 ndumitrach
                                                                        end
727
                                                                endcase
728
                                                        2'b10:                          // EX (SP), HL
729
                                                                case(STAGE[2:0])
730
                                                                        3'b000, 3'b001: begin                   // stage1,2 - pop tmp16
731 14 ndumitrach
                                                                                ALU160_SEL = 0;                          // regs
732
                                                                                DINW_SEL = 1;                                   // DI
733 2 ndumitrach
                                                                                WE              = {4'b001x, STAGE[0] ? 1'b1 : 1'bx, !STAGE[0]};                   // SP, lo/hi
734
                                                                                next_stage = 1;
735 14 ndumitrach
                                                                                REG_WSEL        = 4'b111x;                      // tmp16
736
                                                                                REG_RSEL        = 4'b101x;                      // SP
737 2 ndumitrach
                                                                                M1              = 0;
738
                                                                        end
739
                                                                        3'b010, 3'b011: begin                   // stage3,4 - push hl
740 14 ndumitrach
                                                                                DO_SEL  = 2'b00;                                // ALU80
741
                                                                                ALU160_SEL = 0;                          // regs
742
                                                                                WE              = 6'b001x00;            // SP
743
                                                                                ALU16OP = 5;                                    // dec
744 2 ndumitrach
                                                                                next_stage = 1;
745
                                                                                REG_WSEL        = {3'b010, STAGE[0]};// H/L      
746 14 ndumitrach
                                                                                REG_RSEL        = 4'b101x;                      // SP
747 2 ndumitrach
                                                                                M1              = 0;
748
                                                                                WR                      = 1;
749
                                                                        end
750
                                                                        3'b100, 3'b101: begin           // stage5,6
751 14 ndumitrach
                                                                                ALU160_SEL = 1;                         // pc
752
                                                                                DINW_SEL = 0;                                    // ALU8OUT
753 2 ndumitrach
                                                                                WE              = {1'b0, STAGE[0], 2'b0x, STAGE[0] ? 1'b1 : 1'bx, !STAGE[0]};      // PC, lo/hi
754
                                                                                ALU8OP  = 29;           // pass D1
755
                                                                                next_stage = !STAGE[0];
756
                                                                                REG_WSEL        = 4'b010x;              // HL
757
                                                                                REG_RSEL        = {3'b111, !STAGE[0]};           // tmp16
758
                                                                                M1              = STAGE[0];
759
                                                                                MREQ            = STAGE[0];
760
                                                                        end
761
                                                                endcase
762
                                                        2'b11:  begin                   // DI
763
                                                                ALU160_SEL = 1;                 // PC
764
                                                                WE              = 6'b010x00;    // PC
765
                                                                status[11] = 1'b1;              // set IFF flags
766
                                                                status[7:6] = 2'b00;
767
                                                        end
768
                                                endcase
769
//                              -----------------------         CALL cc --------------------
770
                                        4'b0100, 4'b1100:
771
                                                case(STAGE[2:0])
772
                                                        3'b000, 3'b001:         // stage 1,2 - load addr
773
                                                                if(FlagMux[FETCH[5:3]]) begin
774
                                                                        ALU160_SEL = 1;                                 // pc
775
                                                                        DINW_SEL = 1;                                           // DI
776
                                                                        WE              = {4'b010x, STAGE[0] ? 1'b1 : 1'bx, !STAGE[0]};           // PC, hi/lo
777
                                                                        next_stage = 1;
778
                                                                        REG_WSEL        = 4'b111x;                              // tmp7
779
                                                                        M1              = 0;
780
                                                                end else begin
781
                                                                        ALU160_SEL = 1;                                 // pc
782
                                                                        WE              = 6'b010x00;                    // PC
783
                                                                        ALU16OP = 2;                                            // add2
784
                                                                end
785
                                                        3'b010, 3'b011: begin           // stage 3,4 - push pc
786
                                                                DO_SEL  = {1'b0, STAGE[0]};      // pc hi/lo
787
                                                                ALU160_SEL = 0;                                  // regs
788
                                                                WE              = 6'b001x00;                    // SP
789
                                                                ALU16OP = 5;                                            // DEC
790
                                                                next_stage = 1;
791
                                                                REG_WSEL        = 4'b1xxx;                              // pc
792
                                                                REG_RSEL        = 4'b101x;                              // sp
793
                                                                M1              = 0;
794
                                                                WR                      = 1;
795
                                                        end
796
                                                        3'b100: begin   // stage5
797
                                                                ALU160_SEL = 0;                                  // regs
798
                                                                WE              = 6'b010x00;                    // PC
799
                                                                REG_RSEL        = 4'b111x;                              // tmp7
800
                                                        end
801
                                                endcase
802
//                              -----------------------         PUSH --------------------
803
                                        4'b0101:
804
                                                case(STAGE[1:0])
805
                                                        2'b00, 2'b01: begin                     // stage1,2
806
                                                                DO_SEL  = {STAGE[0] & op16[2], 1'b0};            // FLAGS/ALU80
807
                                                                ALU160_SEL = 0;                          // regs
808
                                                                WE              = 6'b001x00;            // SP
809
                                                                ALU16OP = 5;                            // dec
810
                                                                next_stage = 1;
811
                                                                REG_WSEL        = {1'b0, FETCH[5:4], STAGE[0]};
812 14 ndumitrach
                                                                REG_RSEL        = 4'b101x;                      // SP
813 2 ndumitrach
                                                                M1              = 0;
814
                                                                WR                      = 1;
815
                                                        end
816
                                                        2'b10: begin                                    //stage3
817
                                                                ALU160_SEL = 1;                         // PC
818
                                                                WE              = 6'b010x00;            // PC
819
                                                        end
820
                                                endcase
821
//                              -----------------------         op A, n  --------------------
822
                                        4'b0110, 4'b1110:
823
                                                if(!STAGE[0]) begin                      // stage1, read n
824
                                                        ALU160_SEL = 1;                                 // pc
825
                                                        DINW_SEL = 1;                                           // DI
826
                                                        WE              = 6'b010x01;                    // PC, lo
827
                                                        next_stage = 1;
828
                                                        REG_WSEL        = 4'b011x;                              // tmpLO
829
                                                        M1              = 0;
830
                                                end else begin                                  // stage 2
831
                                                        DINW_SEL = 0;                                            // ALU8OUT[7:0]
832
                                                        ALU160_SEL = 1;                                 // pc
833
                                                        WE              = {4'b110x, ~&FETCH[5:3], 1'bx};                        // flags, PC, hi
834
                                                        ALU8OP  = {2'b00, FETCH[5:3]};
835
                                                        REG_WSEL        = 4'b0110;                              // A
836
                                                        REG_RSEL        = 4'b0111;                              // tmpLO
837
                                                end
838
//                              -----------------------         RST  --------------------
839
                                        4'b0111, 4'b1111:
840
                                                case(STAGE[1:0])
841
                                                        2'b00, 2'b01: begin             // stage 1,2 - push pc
842
                                                                DO_SEL  = {1'b0, STAGE[0]};      // pc hi/lo
843
                                                                ALU160_SEL = 0;                                  // regs
844
                                                                WE              = 6'b001x00;                    // SP
845
                                                                ALU16OP = 5;                                            // DEC
846
                                                                next_stage = 1;
847
                                                                REG_WSEL        = 4'b1xxx;                              // pc
848
                                                                REG_RSEL        = 4'b101x;                              // sp
849
                                                                M1              = 0;
850
                                                                WR                      = 1;
851
                                                        end
852
                                                        2'b10:  begin                           // stage3
853
                                                                ALU160_SEL = 0;                                  // regs
854
                                                                WE              = 6'b010x00;                    // PC
855
                                                                REG_RSEL        = 4'b110x;                              // const
856
                                                        end
857
                                                endcase
858
//                              -----------------------         RET, EXX, JP (HL), LD SP HL --------------------
859
                                        4'b1001:
860
                                                case(FETCH[5:4])
861
                                                        2'b00:                          // RET
862
                                                                case(STAGE[1:0])
863
                                                                        2'b00, 2'b01:   begin           // stage1, stage2 - pop addr
864 14 ndumitrach
                                                                                ALU160_SEL = 0;                  // regs
865
                                                                                DINW_SEL = 1;                           // DI
866 2 ndumitrach
                                                                                WE              = {4'b001x, STAGE[0] ? 1'b1 : 1'bx, !STAGE[0]};           // SP, lo/hi
867
                                                                                next_stage = 1;
868 14 ndumitrach
                                                                                REG_WSEL        = 4'b111x;              // tmp16
869
                                                                                REG_RSEL        = 4'b101x;              // SP
870 2 ndumitrach
                                                                                M1              = 0;
871
                                                                        end
872
                                                                        2'b10: begin                    // stage3 - jump
873 14 ndumitrach
                                                                                ALU160_SEL = 0;                  // regs
874
                                                                                WE              = 6'b010x00;    // PC
875
                                                                                REG_RSEL        = 4'b111x;              // tmp16
876 2 ndumitrach
                                                                        end
877
                                                                endcase
878
                                                        2'b01: begin                    // EXX
879
                                                                ALU160_SEL = 1;                 // PC
880
                                                                WE              = 6'b010x00;    // PC
881
                                                                status[1] = 1;
882
                                                        end
883
                                                        2'b10:  begin           // JP (HL)
884
                                                                ALU160_SEL = 0;                                  // regs
885
                                                                WE              = 6'b010x00;                    // PC
886
                                                                REG_RSEL        = 4'b010x;                              // HL
887
                                                        end
888
                                                        2'b11: begin    // LD SP,HL     
889
                                                                if(!STAGE[0]) begin                      // stage1
890
                                                                        ALU160_SEL = 0;                          // regs
891
                                                                        WE              = 6'b001x00;            // SP
892
                                                                        ALU16OP = 4;                                    // NOP, no post inc
893
                                                                        next_stage = 1;
894
                                                                        REG_RSEL        = 4'b010x;                      // HL
895
                                                                        M1              = 0;
896
                                                                        MREQ            = 0;
897
                                                                end else begin                                          // stage2
898
                                                                        ALU160_SEL = 1;                         // pc
899
                                                                        WE              = 6'b010x00;            // PC
900
                                                                end
901
                                                        end
902
                                                endcase
903
//                              -----------------------         CB, IN A (n), EX DE HL, EI --------------------
904
                                        4'b1011:
905
                                                case(FETCH[5:4])
906
                                                        2'b00:                                          // CB prefix
907
                                                                case({STAGE[0], CPUStatus[4]})
908
                                                                        2'b00, 2'b11: begin
909
                                                                                ALU160_SEL = 1;                 // PC
910
                                                                                WE              = 6'b010000;    // PC
911
                                                                                fetch98 = 2'b10;
912 13 ndumitrach
                                                                                M1 = !CPUStatus[4];     // [DD/FD CB disp op] -  M1 is inactive during <op> byte read
913 2 ndumitrach
                                                                        end
914
                                                                        2'b01: begin
915
                                                                                ALU160_SEL = 1;                 // PC
916
                                                                                WE              = 6'b010100;    // PC, tmpHI
917
                                                                                next_stage = 1;
918
                                                                                M1              = 0;
919
                                                                        end
920
                                                                endcase
921
                                                        2'b01:                                  // IN A, (n)
922
                                                                case(STAGE[1:0])
923
                                                                        2'b00: begin            //stage1 - read n
924
                                                                                ALU160_SEL = 1;                         // pc
925
                                                                                DINW_SEL = 1;                                   // DI
926
                                                                                WE              = 6'b010x01;            // PC, lo
927
                                                                                next_stage = 1;
928
                                                                                REG_WSEL        = 4'b011x;                      // tmpLO
929
                                                                                M1              = 0;
930
                                                                        end
931
                                                                        2'b01: begin            // stage2 - IN
932
                                                                                ALU160_SEL = 0;                          // regs
933
                                                                                DINW_SEL = 1;                                   // DI
934
                                                                                WE              = 6'b000x1x;            // hi
935
                                                                                next_stage = 1;
936
                                                                                REG_WSEL        = 4'b011x;                      // A
937
                                                                                REG_RSEL        = 4'b011x;                      // A - tmpLO
938
                                                                                M1              = 0;
939
                                                                                MREQ            = 0;
940
                                                                                IORQ            = 1;
941
                                                                        end
942
                                                                        2'b10: begin            // stage3 - fetch
943
                                                                                ALU160_SEL = 1;                 // PC
944
                                                                                WE              = 6'b010x00;    // PC
945
                                                                        end
946
                                                                endcase
947
                                                        2'b10: begin                    // EX DE, HL
948
                                                                ALU160_SEL = 1;                 // PC
949
                                                                WE              = 6'b010x00;    // PC
950
                                                                if(CPUStatus[1]) status[3] = 1;
951
                                                                else status[2] = 1;
952
                                                        end
953
                                                        2'b11: begin                    // EI
954
                                                                ALU160_SEL = 1;                 // PC
955
                                                                WE              = 6'b010x00;    // PC
956
                                                                status[11] = 1'b1;
957
                                                                status[7:6] = 2'b11;
958
                                                        end
959
                                                endcase
960
//                              -----------------------         CALL , IX, ED, IY --------------------
961
                                        4'b1101:
962
                                                case(FETCH[5:4])
963
                                                        2'b00:                                  // CALL
964
                                                                case(STAGE[2:0])
965
                                                                        3'b000, 3'b001: begin           // stage 1,2 - load addr
966
                                                                                ALU160_SEL = 1;                                 // pc
967
                                                                                DINW_SEL = 1;                                           // DI
968
                                                                                WE              = {4'b010x, STAGE[0] ? 1'b1 : 1'bx, !STAGE[0]};           // PC, hi/lo
969
                                                                                next_stage = 1;
970
                                                                                REG_WSEL        = 4'b111x;                              // tmp7
971
                                                                                M1              = 0;
972
                                                                        end
973
                                                                        3'b010, 3'b011: begin           // stage 3,4 - push pc
974
                                                                                DO_SEL  = {1'b0, STAGE[0]};      // pc hi/lo
975
                                                                                ALU160_SEL = 0;                                  // regs
976
                                                                                WE              = 6'b001x00;                    // SP
977
                                                                                ALU16OP = 5;                                            // DEC
978
                                                                                next_stage = 1;
979
                                                                                REG_WSEL        = 4'b1xxx;                              // pc
980
                                                                                REG_RSEL        = 4'b101x;                              // sp
981
                                                                                M1              = 0;
982
                                                                                WR                      = 1;
983
                                                                        end
984
                                                                        3'b100: begin   // stage5 - jump
985
                                                                                ALU160_SEL = 0;                                  // regs
986
                                                                                WE              = 6'b010x00;                    // PC
987
                                                                                REG_RSEL        = 4'b111x;                              // tmp7
988
                                                                        end
989
                                                                endcase
990
                                                        2'b01: begin                    // DD - IX
991
                                                                ALU160_SEL = 1;                 // PC
992
                                                                WE              = 6'b010x00;    // PC
993
                                                                status[5:4] = 2'b01;
994
                                                        end
995
                                                        2'b10: begin                    // ED prefix
996
                                                                ALU160_SEL = 1;                 // PC
997
                                                                WE              = 6'b010x00;    // PC
998
                                                                fetch98 = 2'b01;
999
                                                        end
1000
                                                        2'b11:  begin                   // FD - IY
1001
                                                                ALU160_SEL = 1;                 // PC
1002
                                                                WE              = 6'b010x00;    // PC
1003
                                                                status[5:4]     = 2'b11;
1004
                                                        end
1005
                                                endcase
1006
                                endcase
1007
 
1008
//      ------------------------------------------- ED + opcode ----------------------------------------------------
1009
                        4'b0100, 4'b0111: begin         // ED + 2'b00, ED + 2'b11               = NOP
1010
                                ALU160_SEL = 1;                 // PC
1011
                                WE              = 6'b010x00;    // PC
1012
                        end
1013
                        4'b0101:
1014
                                case(FETCH[2:0])
1015
//                              -----------------------         in r (C)  --------------------
1016
                                        3'b000:
1017
                                                if(!STAGE[0]) begin
1018
                                                        ALU160_SEL = 0;                                  // regs
1019
                                                        DINW_SEL = 1;                                           // DI
1020
                                                        WE              = {4'b000x, !opd[3], opd[3]} ;  // hi/lo
1021
                                                        next_stage = 1;
1022
                                                        REG_WSEL        = {1'b0, opd[5:4], 1'bx};
1023
                                                        REG_RSEL        = 4'b000x;                              // BC
1024
                                                        M1              = 0;
1025
                                                        MREQ            = 0;
1026
                                                        IORQ            = 1;
1027
                                                end else begin
1028
                                                        ALU160_SEL = 1;                                 // pc
1029
                                                        WE              = 6'b110x00;                    // flags, PC
1030
                                                        ALU8OP  = 29;                                           // IN
1031
                                                        REG_RSEL        = {1'b0, opd[5:3]};     // reg
1032
                                                end
1033
//                              -----------------------         out (C) r  --------------------
1034
                                        3'b001:
1035
                                                if(!STAGE[0]) begin
1036
                                                        DO_SEL  = 2'b00;                                        // ALU80
1037
                                                        ALU160_SEL = 0;                                  // regs
1038
                                                        WE              = 6'b000x00;                    // nothing
1039
                                                        next_stage = 1;
1040
                                                        REG_WSEL        = &opd[5:3] ? 4'b110x : {1'b0, opd[5:3]}; // zero/reg
1041
                                                        REG_RSEL        = 4'b000x;                              // BC
1042
                                                        M1              = 0;
1043
                                                        MREQ            = 0;
1044
                                                        WR                      = 1;
1045
                                                        IORQ            = 1;
1046
                                                end else begin
1047
                                                        ALU160_SEL = 1;                                 // pc
1048
                                                        WE              = 6'b010x00;                    // PC
1049
                                                end
1050
//                              -----------------------         SBC16, ADC16  --------------------
1051
                                        3'b010:
1052
                                                if(!STAGE[0]) begin                      // stage1
1053
                                                        DINW_SEL = 0;                                            // ALU8OUT
1054
                                                        WE              = 6'b100x01;                    // flags, lo
1055
                                                        ALU8OP  = {3'b000, !FETCH[3], 1'b1};    // SBC/ADC
1056
                                                        next_stage = 1;
1057
                                                        REG_WSEL        = 4'b0101;                              // L                    
1058
                                                        REG_RSEL        = {op16, 1'b1};
1059
                                                        M1              = 0;
1060
                                                        MREQ            = 0;
1061
                                                end else begin
1062
                                                        ALU160_SEL = 1;                                 // pc
1063
                                                        DINW_SEL = 0;                                            // ALU8OUT
1064
                                                        WE              = 6'b110x10;                    // flags, PC, hi
1065
                                                        ALU8OP  = {3'b000, !FETCH[3], 1'b1};
1066
                                                        REG_WSEL        = 4'b0100;                              // H
1067
                                                        REG_RSEL        = {op16, 1'b0};
1068
                                                end
1069
//                              -----------------------         LD (nn) r16, ld r16 (nn)  --------------------
1070
                                        3'b011:
1071
                                                case(STAGE[2:1])
1072
                                                        2'b00:  begin // stage 1,2 - read address
1073
                                                                ALU160_SEL = 1;                         // pc
1074
                                                                DINW_SEL = 1;                                   // DI
1075
                                                                WE              = {4'b010x, STAGE[0] ? 1'b1 : 1'bx, !STAGE[0]};   // PC, hi/lo
1076
                                                                next_stage = 1;
1077
                                                                REG_WSEL        = 4'b111x;                      // tmp16
1078
                                                                M1              = 0;
1079
                                                        end
1080
                                                        2'b01: begin
1081
                                                                ALU160_SEL = 0;                  // regs
1082
                                                                next_stage = 1;
1083
                                                                ALU16OP = {2'b00, STAGE[0]};
1084
                                                                REG_RSEL        = 4'b111x;              // tmp16
1085
                                                                REG_WSEL        = {op16, !STAGE[0]};
1086
                                                                M1              = 0;
1087
                                                                if(FETCH[3]) begin      // LD rr, (nn) - stage3,4
1088
                                                                        DINW_SEL = 1;                           // DI
1089
                                                                        WE              = {4'b000x, STAGE[0] ? 1'b1 : 1'bx, !STAGE[0]};   // lo
1090
                                                                end else begin                  // LD (nn), rr - stage3,4
1091
                                                                        DO_SEL  = op16[2] ? {1'b1, !STAGE[0]} : 2'b00;                           // ALU80/sp
1092
                                                                        WE              = 6'b000x00;            // nothing
1093
                                                                        WR                      = 1;
1094
                                                                end
1095
                                                        end
1096
                                                        2'b10:          // stage5 
1097
                                                                if(FETCH[3] & op16[2] & !STAGE[0]) begin // LD sp, (nn) - stage5
1098
                                                                        ALU160_SEL = 0;                                  // regs
1099
                                                                        WE              = 6'b001x00;                    // SP
1100
                                                                        ALU16OP = 4;                                            // NOP
1101
                                                                        next_stage = 1;
1102
                                                                        REG_RSEL        = 4'b101x;                              // tmp SP
1103
                                                                        M1              = 0;
1104
                                                                        MREQ            = 0;
1105
                                                                end else begin
1106
                                                                        ALU160_SEL = 1;                                 // pc
1107
                                                                        WE              = 6'b010x00;                    // PC
1108
                                                                end
1109
                                                        endcase
1110
//                              -----------------------         NEG  --------------------
1111
                                        3'b100: begin
1112
                                                ALU160_SEL = 1;                                 // pc
1113
                                                DINW_SEL = 0;                                            // ALU8OUT
1114
                                                WE              = 6'b110x10;                    // flags, PC, hi
1115
                                                ALU8OP  = 5'b11111;                             // NEG
1116
                                                REG_WSEL        = 4'b011x;                              // A
1117
                                                REG_RSEL        = 4'b0110;                              // A
1118
                                        end
1119
//                              -----------------------         RETN, RETI  --------------------
1120
                                        3'b101:
1121
                                                case(STAGE[1:0])
1122
                                                        2'b00, 2'b01:   begin           // stage1, stage2 - pop addr
1123
                                                                ALU160_SEL = 0;                          // regs
1124
                                                                DINW_SEL = 1;                                   // DI
1125
                                                                WE              = {4'b001x, STAGE[0] ? 1'b1 : 1'bx, !STAGE[0]};           // SP, lo/hi
1126
                                                                next_stage = 1;
1127
                                                                REG_WSEL        = 4'b111x;                      // tmp16
1128
                                                                REG_RSEL        = 4'b101x;                      // SP
1129
                                                                M1              = 0;
1130
                                                        end
1131
                                                        2'b10: begin                    // stage3 - jump
1132
                                                                ALU160_SEL = 0;                                  // regs
1133
                                                                WE              = 6'b010x00;                    // PC
1134
                                                                REG_RSEL        = 4'b111x;                              // tmp16
1135
                                                                status[11] = 1'b1;
1136
                                                                status[7:6] = {CPUStatus[7], CPUStatus[7]};
1137
                                                        end
1138
                                                endcase
1139
//                              -----------------------         IM  --------------------
1140
                                        3'b110: begin
1141
                                                ALU160_SEL = 1;                                 // PC
1142
                                                WE              = 6'b010x00;                    // PC
1143
                                                status[10:8] = {1'b1, FETCH[4:3]};
1144
                                        end
1145
//                              -----------------------         LD I A, LD R A, LD A I, LD A R, RRD, RLD  --------------------
1146
                                        3'b111:
1147
                                                case(FETCH[5:4])
1148
                                                        2'b00: begin    // LD I/R A
1149
                                                                ALU160_SEL = 1;                                 // pc
1150
                                                                DINW_SEL = 1'b0;                                        // ALU8OUT
1151
                                                                WE              = {4'b010x, !FETCH[3], FETCH[3]};       // PC, hi/lo
1152
                                                                ALU8OP  = 29;                                           // pass D1
1153 11 ndumitrach
                                                                REG_WSEL        = 4'b1001;                              // IR, write r
1154 2 ndumitrach
                                                                REG_RSEL        = 4'b0110;                              // A
1155
                                                        end
1156
                                                        2'b01: begin    // LD A I/R
1157
                                                                ALU160_SEL = 1;                                 // pc
1158
                                                                DINW_SEL = 1'b0;                                        // ALU8OUT
1159
                                                                WE              = 6'b110x1x;                    // flags, PC, hi
1160
                                                                ALU8OP  = 29;                                           // PASS D1
1161
                                                                REG_WSEL        = 4'b011x;                              // A
1162
                                                                REG_RSEL        = {3'b100, FETCH[3]};// I/R
1163
                                                        end
1164
                                                        2'b10:                  // RRD, RLD
1165
                                                                case(STAGE[1:0])
1166
                                                                        2'b00:begin             // stage1, read data
1167
                                                                                ALU160_SEL = 0;                                  // regs
1168
                                                                                DINW_SEL = 1;                                           // DI
1169
                                                                                WE              = 6'b000x01;                    // lo
1170
                                                                                next_stage = 1;
1171
                                                                                REG_WSEL        = 4'b011x;                              // tmpLO
1172
                                                                                REG_RSEL        = 4'b010x;                              // HL
1173
                                                                                M1              = 0;
1174
                                                                        end
1175
                                                                        2'b01: begin    // stage2, shift data
1176
                                                                                DINW_SEL = 0;                                            // ALU8OUT
1177
                                                                                WE              = 6'b100x11;                    // flags, hi, lo
1178
                                                                                ALU8OP  = FETCH[3] ? 5'b01100 : 5'b01011;       // RRD/RLD
1179
                                                                                next_stage = 1;
1180
                                                                                REG_WSEL        = 4'b0110;                              // A
1181
                                                                                REG_RSEL        = 4'b0111;                              // tmpLO
1182
                                                                                M1              = 0;
1183
                                                                                MREQ            = 0;
1184
                                                                        end
1185
                                                                        2'b10: begin // stage3 - write
1186
                                                                                DO_SEL  = 2'b00;                                        // ALU80
1187
                                                                                ALU160_SEL = 0;                                  // regs
1188
                                                                                WE              = 6'b000x0x;                    // nothing
1189
                                                                                next_stage = 1;
1190
                                                                                REG_WSEL        = 4'b0111;                              // tmpLO
1191
                                                                                REG_RSEL        = 4'b010x;                              // HL
1192
                                                                                M1              = 0;
1193
                                                                                WR                      = 1;
1194
                                                                        end
1195
                                                                        2'b11: begin
1196
                                                                                ALU160_SEL = 1;                                 // PC
1197
                                                                                WE              = 6'b010x00;                    // PC
1198
                                                                        end
1199
                                                                endcase
1200
                                                        2'b11: begin    // NOP
1201
                                                                ALU160_SEL = 1;                                 // PC
1202
                                                                WE              = 6'b010x00;                    // PC
1203
                                                        end
1204
                                                endcase
1205
                                endcase
1206
//                              -----------------------         block instructions  --------------------
1207
                        4'b0110:
1208
                                if({FETCH[5], FETCH[2]} == 4'b10)
1209
                                        case(FETCH[1:0])
1210
                                                2'b00:  // LDI, LDD, LDIR, LDDR
1211
                                                        case(STAGE[1:0])
1212
                                                                2'b00:  begin                   // stage1, read data, inc/dec HL
1213
                                                                        ALU160_SEL = 0;                                  // regs
1214
                                                                        DINW_SEL = 0;                                            // ALU8OUT
1215
                                                                        WE              = 6'b100111;                    // flags, tmpHI, hi, lo
1216
                                                                        ALU8OP  = {4'b0111, FETCH[3]};  // INC/DEC16
1217
                                                                        next_stage = 1;
1218
                                                                        REG_WSEL        = 4'b0100;                              // H
1219
                                                                        REG_RSEL        = 4'b0101;                              // L
1220
                                                                        M1              = 0;
1221
                                                                end
1222
                                                                2'b01:  begin                   // stage2, dec BC
1223
                                                                        DINW_SEL = 0;                                            // ALU8OUT
1224
                                                                        WE              = 6'b100011;                    // flags, hi, lo (affects PF only)
1225
                                                                        ALU8OP  = 5'b01111;                             // DEC
1226
                                                                        next_stage = 1;
1227
                                                                        REG_WSEL        = 4'b0000;                              // B
1228
                                                                        REG_RSEL        = 4'b0001;                              // C
1229
                                                                        M1              = 0;
1230
                                                                        MREQ            = 0;
1231
                                                                end
1232
                                                                2'b10:  begin                   // stage2, write data, inc/dec DE
1233
                                                                        DO_SEL  = 2'b01;                                        // th
1234
                                                                        ALU160_SEL = 0;                                  // regs
1235
                                                                        DINW_SEL = 0;                                            // ALU8OUT
1236
                                                                        WE              = 6'b000x11;                    // hi, lo
1237
                                                                        ALU8OP  = {4'b0111, FETCH[3]};  // INC / DEC
1238
                                                                        next_stage = FETCH[4] ? !FLAGS[2] : 1'b1;
1239
                                                                        REG_WSEL        = 4'b0010;                              // D
1240
                                                                        REG_RSEL        = 4'b0011;                              // E
1241
                                                                        M1              = 0;
1242
                                                                        WR                      = 1;
1243
                                                                end
1244
                                                                2'b11: begin
1245
                                                                        ALU160_SEL = 1;                                 // PC
1246
                                                                        WE              = 6'b010x00;                    // PC
1247
                                                                end
1248
                                                        endcase
1249
                                                2'b01:  // CPI, CPD, CPIR, CPDR
1250
                                                        case(STAGE[1:0])
1251
                                                                2'b00: begin                    // stage1, load data
1252
                                                                        ALU160_SEL = 0;                                  // regs
1253
                                                                        DINW_SEL = 1;                                           // DI
1254
                                                                        WE              = 6'b000x01;                    // lo
1255
                                                                        next_stage = 1;
1256
                                                                        REG_WSEL        = 4'b011x;                              // tmpLO
1257
                                                                        REG_RSEL        = 4'b010x;                              // HL
1258
                                                                        M1              = 0;
1259
                                                                end
1260
                                                                2'b01: begin                    // stage2, CP
1261
                                                                        WE              = 6'b100x0x;                    // flags
1262
                                                                        ALU8OP  = 7;                                            // CP
1263
                                                                        next_stage = 1;
1264
                                                                        REG_WSEL        = 4'b0110;                              // A
1265
                                                                        REG_RSEL        = 4'b0111;                              // tmpLO
1266
                                                                        M1              = 0;
1267
                                                                        MREQ            = 0;
1268
                                                                end
1269
                                                                2'b10: begin                    // stage3, dec BC
1270
                                                                        DINW_SEL = 0;                                            // ALU8OUT
1271
                                                                        WE              = 6'b100x11;                    // flags, hi, lo
1272
                                                                        ALU8OP  = 5'b01111;                             // DEC16
1273
                                                                        next_stage = 1;
1274
                                                                        REG_WSEL        = 4'b0000;                              // B
1275
                                                                        REG_RSEL        = 4'b0001;                              // C
1276
                                                                        M1              = 0;
1277
                                                                        MREQ            = 0;
1278
                                                                end
1279
                                                                2'b11: begin                    // stage4, inc/dec HL
1280
                                                                        ALU160_SEL = 1;                                 // pc
1281
                                                                        DINW_SEL = 0;                                            // ALU8OUT
1282
                                                                        M1              = FETCH[4] ? (!FLAGS[2] || FLAGS[6]) : 1'b1;
1283
                                                                        WE              = {1'b0, M1, 4'b0x11};  // PC, hi, lo
1284
                                                                        ALU8OP  = {4'b0111, FETCH[3]};  // INC / DEC
1285
                                                                        REG_WSEL        = 4'b0100;                              // H
1286
                                                                        REG_RSEL        = 4'b0101;                              // L
1287
                                                                        MREQ            = M1;
1288
                                                                end
1289
                                                        endcase
1290
                                                2'b10:  // INI, IND, INIR, INDR
1291
                                                        case(STAGE[1:0])
1292
                                                                2'b00:  begin                   // stage1, in data, dec B
1293
                                                                        ALU160_SEL = 0;                                  // regs
1294
                                                                        DINW_SEL = 0;                                            // ALU8OUT
1295
                                                                        WE              = 6'b100110;                    // flags, tmpHI, hi
1296
                                                                        ALU8OP  = 10;                                           // DEC
1297
                                                                        next_stage = 1;
1298
                                                                        REG_WSEL        = 4'b0000;                              // B
1299
                                                                        REG_RSEL        = 4'b000x;                              // BC
1300
                                                                        M1              = 0;
1301
                                                                        MREQ            = 0;
1302
                                                                        IORQ            = 1;
1303
                                                                end
1304
                                                                2'b01:  begin                   // stage2, write data, inc/dec HL
1305
                                                                        DO_SEL  = 2'b01;                                        // th
1306
                                                                        ALU160_SEL = 0;                                  // regs
1307
                                                                        DINW_SEL = 0;                                            // ALU8OUT
1308
                                                                        WE              = 6'b000x11;                    // hi, lo
1309
                                                                        ALU8OP  = {4'b0111, FETCH[3]};  // INC / DEC
1310
                                                                        next_stage = FETCH[4] ? FLAGS[6] : 1'b1;
1311
                                                                        REG_WSEL        = 4'b0100;                              // H
1312
                                                                        REG_RSEL        = 4'b0101;                              // L
1313
                                                                        M1              = 0;
1314
                                                                        WR                      = 1;
1315
                                                                end
1316
                                                                2'b10:  begin                   // stage3
1317
                                                                        ALU160_SEL = 1;                                 // pc
1318
                                                                        WE              = 6'b010x00;                    // PC
1319
                                                                end
1320
                                                        endcase
1321
                                                2'b11:  // OUTI/OUTD/OTIR/OTDR
1322
                                                        case(STAGE[1:0])
1323
                                                                2'b00:  begin                   // stage1, load data, inc/dec HL
1324
                                                                        ALU160_SEL = 0;                                  // regs
1325
                                                                        DINW_SEL = 0;                                            // ALU8OUT
1326
                                                                        WE              = 6'b000111;                    // tmpHI, hi, lo
1327
                                                                        ALU8OP  = {4'b0111, FETCH[3]};  // INC / DEC
1328
                                                                        next_stage = 1;
1329
                                                                        REG_WSEL        = 4'b0100;                              // H
1330
                                                                        REG_RSEL        = 4'b0101;                              // L
1331
                                                                        M1              = 0;
1332
                                                                end
1333
                                                                2'b01:  begin                   // stage2, out data, dec B
1334
                                                                        DO_SEL  = 2'b01;                                        // th
1335
                                                                        ALU160_SEL = 0;                                  // regs
1336
                                                                        DINW_SEL = 0;                                            // ALU8OUT
1337
                                                                        WE              = 6'b100x10;                    // flags, hi
1338
                                                                        ALU8OP  = 10;                                           // DEC
1339
                                                                        next_stage = FETCH[4] ? (ALU80 == 8'b00000001) : 1'b1;
1340
                                                                        REG_WSEL        = 4'b0000;                              // B
1341
                                                                        REG_RSEL        = 4'b000x;                              // BC
1342
                                                                        M1              = 0;
1343
                                                                        MREQ            = 0;
1344
                                                                        IORQ            = 1;
1345
                                                                        WR                      = 1;
1346
                                                                end
1347
                                                                2'b10:  begin                   // stage3
1348
                                                                        ALU160_SEL = 1;                                 // pc
1349
                                                                        WE              = 6'b010x00;                    // PC
1350
                                                                end
1351
                                                        endcase
1352
                                        endcase
1353
                                else begin                      // NOP
1354
                                        ALU160_SEL = 1;                                 // PC
1355
                                        WE              = 6'b010x00;                    // PC
1356
                                end
1357
//------------------------------------------- CB + opcode ----------------------------------------------------
1358
                        4'b1000, 4'b1001, 4'b1010, 4'b1011:                                                                             // CB class (rot/shift, bit/res/set)
1359
                                case({STAGE[1:0], CPUStatus[4], op0mem})
1360
                                        4'b00_0_0: begin                                                // execute reg-reg
1361
                                                DINW_SEL = 0;                                            // ALU8OUT
1362
                                                ALU160_SEL = 1;                                 // pc
1363
                                                WE              = {!FETCH[7], 3'b10x, FETCH[7:6] == 2'b01 ? 2'b00 : {!opd[0], opd[0]}};   // flags, hi/lo
1364 14 ndumitrach
                                                ALU8OP  = 28;                                           // BIT
1365 2 ndumitrach
                                                REG_WSEL        = {1'b0, opd[2:0]};
1366
                                        end
1367
                                        4'b00_0_1, 4'b00_1_0, 4'b00_1_1: begin                          // stage1, (HL-X) - read data
1368
                                                ALU160_SEL = 0;                          // regs
1369
                                                DINW_SEL = 1;                                   // DI
1370
                                                WE              = opd[0] ? 6'b000001 : 6'b000010;        // lo/hi
1371 14 ndumitrach
                                                ALU16OP = CPUStatus[4] ? 3'd3 : 3'd0;                   // ADD - NOP
1372 2 ndumitrach
                                                next_stage = 1;
1373
                                                REG_WSEL = FETCH[7:6] == 2'b01 ? 4'b111x : {1'b0, opd[2:0]};     // dest, tmp16 for BIT
1374
                                                REG_RSEL        = 4'b010x;                      // HL
1375
                                                M1              = 0;
1376
                                        end
1377
                                        4'b01_0_1, 4'b01_1_0, 4'b01_1_1:                // stage2 (HL-X) - execute, write
1378
                                                case(FETCH[7:6])
1379
                                                        2'b00, 2'b10, 2'b11: begin              // exec + write
1380
                                                                DINW_SEL = 0;                                    // ALU8OUT
1381
                                                                DO_SEL  = 2'b11;                                // ALU8OUT[7:0]
1382
                                                                ALU160_SEL = 0;                          // regs
1383
                                                                WE              = {!FETCH[7], 3'b00x, !opd[0], opd[0]};   // flags, hi/lo
1384
                                                                ALU8OP  = 28;
1385
                                                                ALU16OP = CPUStatus[4] ? 3'd3 : 3'd0;
1386
                                                                next_stage = 1;
1387
                                                                REG_WSEL        = {1'b0, opd[2:0]};
1388
                                                                REG_RSEL        = 4'b010x;                              // HL
1389
                                                                M1              = 0;
1390
                                                                WR                      = 1;
1391
                                                        end
1392
                                                        2'b01: begin                                                    // BIT, no write
1393
                                                                ALU160_SEL = 1;                                 // pc
1394
                                                                WE              = 6'b110xxx;                    // flags, PC
1395
                                                                ALU8OP  = 28;                                           // BIT
1396
                                                                REG_WSEL        = {3'b111, opd[0]};      // tmp
1397
                                                        end
1398
                                                endcase
1399
                                        4'b10_0_1, 4'b10_1_0, 4'b10_1_1: begin  // (HL-X) - load next op
1400
                                                ALU160_SEL = 1;                                                 // pc
1401
                                                WE              = 6'b010x00;                                    // PC
1402
                                        end
1403
                                endcase
1404
//------------------------------------------- // RST, NMI, INT ----------------------------------------------------
1405
                        4'b1110: begin                  // RESET: IR <- 0, IM <- 0, IFF1,IFF2 <- 0, pC <- 0
1406
                                        ALU160_SEL = 0;                                  // regs
1407
                                        DINW_SEL = 0;                                            // ALU8OUT
1408
                                        WE              = 6'bx1xx11;                    // PC, hi, lo
1409
                                        ALU8OP  = 29;                                           // pass D1
1410
                                        ALU16OP = 4;                                            // NOP
1411 11 ndumitrach
                                        REG_WSEL        = 4'b1001;                              // IR, write r
1412 2 ndumitrach
                                        REG_RSEL        = 4'b110x;                              // const
1413
                                        M1              = 0;
1414
                                        MREQ            = 0;
1415
                                        status[11:6] = 6'b110000;               // IM0, DI
1416
                                end
1417
                        4'b1101:                                                // NMI
1418
                                case(STAGE[1:0])
1419
                                        2'b00: begin
1420
                                                ALU160_SEL = 1;                         // pc
1421
                                                WE              = 6'b010x00;            // PC
1422
                                                ALU16OP = intop;                                // DEC/DEC2 (if block instruction interrupted)
1423
                                                next_stage = 1;
1424
                                                M1              = 0;
1425
                                                MREQ            = 0;
1426
                                        end
1427
                                        2'b01, 2'b10: begin
1428
                                                DO_SEL  = {1'b0, !STAGE[0]};     // pc hi/lo
1429
                                                ALU160_SEL = 0;                                  // regs
1430
                                                WE              = 6'b001x00;                    // SP
1431
                                                ALU16OP = 5;                                            // DEC
1432
                                                next_stage = 1;
1433
                                                REG_WSEL        = 4'b1xxx;                              // pc
1434
                                                REG_RSEL        = 4'b101x;                              // sp
1435
                                                M1              = 0;
1436
                                                WR                      = 1;
1437
                                                status[11]      = 1'b1;
1438
                                                status[7:6] = {CPUStatus[7], 1'b0};     // reset IFF1
1439
                                        end
1440
                                        2'b11: begin
1441
                                                ALU160_SEL = 0;                                  // regs
1442
                                                WE              = 6'b010x00;                    // PC
1443
                                                REG_RSEL        = 4'b110x;                              // const
1444
                                        end
1445
                                endcase
1446
                        4'b1100:                                // INT
1447
                                case(CPUStatus[9:8])
1448
                                        2'b00, 2'b01, 2'b10: begin              // IM0, IM1     
1449
                                                ALU160_SEL = 1;                                 // pc
1450
                                                WE              = 6'b010x00;                    // PC
1451
                                                ALU16OP = intop;                                        // DEC/DEC2 (if block instruction interrupted)
1452
                                                MREQ            = 0;
1453
                                                IORQ            = 1;
1454
                                                status[11]      = 1'b1;
1455
                                                status[7:6] = 2'b0;                             // reset IFF1, IFF2
1456
                                        end
1457
                                        2'b11:                                                          // IM2
1458
                                                case(STAGE[2:0])
1459
                                                        3'b000: begin
1460
                                                                ALU160_SEL = 1;                         // pc
1461
                                                                DINW_SEL = 1;                                   // DI
1462
                                                                WE              = 6'b010x01;            // PC, lo
1463
                                                                ALU16OP = intop;                                // DEC/DEC2 (if block instruction interrupted)
1464
                                                                next_stage = 1;
1465 11 ndumitrach
                                                                REG_WSEL        = 4'b1000;                      // Itmp, no write r
1466 2 ndumitrach
                                                                MREQ            = 0;
1467
                                                                IORQ            = 1;
1468
                                                                status[11]      = 1'b1;
1469
                                                                status[7:6] = 2'b0;                     // reset IFF1, IFF2
1470
                                                        end
1471
                                                        3'b001, 3'b010: begin                   // push pc
1472
                                                                DO_SEL  = {1'b0, !STAGE[0]};     // pc hi/lo
1473
                                                                ALU160_SEL = 0;                                  // regs
1474
                                                                WE              = 6'b001x00;                    // SP
1475
                                                                ALU16OP = 5;                                            // DEC
1476
                                                                next_stage = 1;
1477
                                                                REG_WSEL        = 4'b1xxx;                              // pc
1478
                                                                REG_RSEL        = 4'b101x;                              // sp
1479
                                                                M1              = 0;
1480
                                                                WR                      = 1;
1481
                                                        end
1482
                                                        3'b011, 3'b100: begin                   // read address
1483
                                                                ALU160_SEL = 0;                                  // regs
1484
                                                                DINW_SEL = 1;                                           // DI
1485
                                                                WE              = {4'b0x0x, STAGE[0] ? 1'bx : 1'b1, STAGE[0]};                            // hi/lo
1486
                                                                ALU16OP = {2'b00, !STAGE[0]};// NOP/INC
1487
                                                                next_stage = 1;
1488
                                                                REG_WSEL        = 4'b111x;                              // tmp16
1489
                                                                REG_RSEL        = 4'b1000;                              // I-Itmp
1490
                                                                M1              = 0;
1491
                                                        end
1492
                                                        3'b101: begin                                           // jump
1493
                                                                ALU160_SEL = 0;                                  // regs
1494
                                                                WE              = 6'b010x00;                    // PC
1495
                                                                REG_RSEL        = 4'b111x;                              // tmp16
1496
                                                        end
1497
                                                endcase
1498
                                endcase
1499
                endcase
1500
        end
1501
 
1502
endmodule
1503 14 ndumitrach
 
1504
module Z80Reg(
1505
        input [7:0]rstatus,      // 0=af-af', 1=exx, 2=hl-de, 3=hl'-de',4=hl-ixy, 5=ix-iy, 6=IFF1, 7=IFF2
1506
        input M1,
1507
        input [5:0]WE,                   // 5 = flags, 4 = PC, 3 = SP, 2 = tmpHI, 1 = hi, 0 = lo
1508
        input CLK,
1509
        input [15:0]ALU8OUT,     // CPU data out bus (output of alu8)
1510
        input [7:0]DI,                   // CPU data in bus
1511
        input [15:0]ADDR,                // CPU addr bus
1512
        input [7:0]CONST,
1513
        input [7:0]ALU8FLAGS,
1514
        input [1:0]DO_SEL,       // select DO betwen ALU8OUT lo and th register
1515
        input ALU160_sel,               // 0=REG_RSEL, 1=PC
1516
        input [3:0]REG_WSEL,     // rdow:        [3:1] 0=BC, 1=DE, 2=HL, 3=A-TL, 4=I-x  ----- [0] = 0HI,1LO
1517
        input [3:0]REG_RSEL,     // mux_rdor:   [3:1] 0=BC, 1=DE, 2=HL, 3=A-TL, 4=I-R, 5=SP, 7=tmpSP   ----- [0] = 0HI, 1LO
1518
        input DINW_SEL,         // select RAM write data between (0)ALU8OUT, and 1(DI)
1519
        input XMASK,                    // 0 if REG_WSEL should not use IX, IY, even if rstatus[4] == 1
1520
        input [2:0]ALU16OP,      // ALU16OP
1521
        input WAIT,                             // wait
1522
 
1523
        output reg [7:0]DO,                      // CPU data out bus
1524
        output reg [7:0]ALU80,
1525
        output reg [7:0]ALU81,
1526
        output reg [15:0]ALU160,
1527
        output [7:0]ALU161,
1528
        output [7:0]FLAGS
1529
        );
1530
 
1531
// latch registers
1532
        reg [15:0]pc=0;                           // program counter
1533
        reg [15:0]sp;                                    // stack pointer
1534
        reg [7:0]r;                                              // refresh
1535
        reg [15:0]flg = 0;
1536
        reg [7:0]th;                                     // temp high
1537
 
1538
// internal wires       
1539
        wire [15:0]rdor;         // R out from RAM
1540
        wire [15:0]rdow;         // W out from RAM
1541
        wire [3:0]SELW;          // RAM W port sel
1542
        wire [3:0]SELR;          // RAM R port sel
1543
        reg  [15:0]DIN;          // RAM W in data
1544
        reg [15:0]mux_rdor;      // (3)A reversed mixed with TL, (4)I mixed with R (5)SP
1545
 
1546
//------------------------------------ RAM block registers ----------------------------------
1547
// 0:BC, 1:DE, 2:HL, 3:A-x, 4:I-x, 5:IX, 6:IY, 7:x-x, 8:BC', 9:DE', 10:HL', 11:A'-x, 12: tmpSP, 13:zero
1548
   RAM16X8D_regs regs_lo
1549
        (
1550
      .A(SELW),          // R/W address
1551
      .D(DIN[7:0]),      // Write data input
1552
      .DPRA(SELR),               // Read-only address
1553
      .WCLK(CLK),                // Write clock input
1554
      .WE(WE[0] & !WAIT),// Write enable input
1555
 
1556
      .DPO(rdor[7:0]),   // Read-only data output
1557
      .SPO(rdow[7:0])    // R/W data output
1558
   );
1559
 
1560
   RAM16X8D_regs regs_hi
1561
        (
1562
      .A(SELW),          // R/W address
1563
      .D(DIN[15:8]),     // Write data input
1564
      .DPRA(SELR),               // Read-only address
1565
      .WCLK(CLK),                // Write clock input
1566
      .WE(WE[1] & !WAIT),// Write enable input
1567
 
1568
      .DPO(rdor[15:8]),  // Read-only data output
1569
      .SPO(rdow[15:8])   // R/W data output
1570
   );
1571
 
1572
        wire [15:0]ADDR1 = ADDR + !ALU16OP[2]; // address post increment
1573
        wire [7:0]flgmux = {ALU8FLAGS[7:3], SELR[3:0] == 4'b0100 ? rstatus[7] : ALU8FLAGS[2], ALU8FLAGS[1:0]}; // LD A, I/R IFF2 flag on parity
1574
        always @(posedge CLK)
1575
                if(!WAIT) begin
1576
                        if(WE[2]) th <= DI;
1577
                        if(WE[3]) sp <= ADDR1;
1578
                        if(WE[4]) pc <= ADDR1;
1579
                        if({REG_WSEL, WE[0]} == 5'b10011) r <= ALU8OUT[7:0];
1580
                        else if(M1) r[6:0] <= r[6:0] + 1;
1581
                        if(WE[5])
1582
                                if(rstatus[0]) flg[15:8] <= flgmux;
1583
                                else flg[7:0] <= flgmux;
1584
                end
1585
 
1586
        assign ALU161 = th;
1587
        assign FLAGS = rstatus[0] ? flg[15:8] : flg[7:0];
1588
 
1589
        always @* begin
1590
                DIN = DINW_SEL ? {DI, DI} : ALU8OUT;
1591
                ALU80 = REG_WSEL[0] ? rdow[7:0] : rdow[15:8];
1592
                ALU81 = REG_RSEL[0] ? mux_rdor[7:0] : mux_rdor[15:8];
1593
                ALU160 = ALU160_sel ? pc : mux_rdor;
1594
 
1595
                case({REG_WSEL[3], DO_SEL})
1596
                        0:       DO = ALU80;
1597
                        1:      DO = th;
1598
                        2: DO = FLAGS;
1599
                        3: DO = ALU8OUT[7:0];
1600
                        4: DO = pc[15:8];
1601
                        5: DO = pc[7:0];
1602
                        6:      DO = sp[15:8];
1603
                        7: DO = sp[7:0];
1604
                endcase
1605
                case({ALU16OP == 4, REG_RSEL[3:0]})
1606
                        5'b01001, 5'b11001:                                                             mux_rdor = {rdor[15:8], r};
1607
                        5'b01010, 5'b01011:                                                             mux_rdor = sp;
1608
                        5'b01100, 5'b01101, 5'b11100, 5'b11101: mux_rdor = {8'b0, CONST};
1609
                        default:                mux_rdor = rdor;
1610
                endcase
1611
        end
1612
 
1613
        RegSelect WSelectW
1614
        (
1615
                .SEL(REG_WSEL[3:1]),
1616
                .rstatus({rstatus[5], rstatus[4] & XMASK, rstatus[3:0]}),
1617
 
1618
                .RAMSEL(SELW)
1619
        );
1620
 
1621
        RegSelect WSelectR
1622
        (
1623
                .SEL(REG_RSEL[3:1]),
1624
                .rstatus(rstatus[5:0]),
1625
 
1626
                .RAMSEL(SELR)
1627
        );
1628
 
1629
endmodule
1630
 
1631
 
1632
module RegSelect(
1633
        input [2:0]SEL,
1634
        input [5:0]rstatus,                      // 0=af-af', 1=exx, 2=hl-de, 3=hl'-de',4=hl-ixy, 5=ix-iy
1635
 
1636
        output reg [3:0]RAMSEL
1637
        );
1638
 
1639
        always @* begin
1640
                RAMSEL = 4'bxxxx;
1641
                case(SEL)
1642
                        0: RAMSEL = {rstatus[1], 3'b000};        // BC
1643
                        1:  //DE
1644
                                if(rstatus[{1'b1, rstatus[1]}]) RAMSEL = {rstatus[1], 3'b010};          //      HL
1645
                                else RAMSEL = {rstatus[1], 3'b001};                             // DE
1646
                        2:      // HL
1647
                                case({rstatus[5:4], rstatus[{1'b1, rstatus[1]}]})
1648
                                        0,4:     RAMSEL = {rstatus[1], 3'b010};          // HL
1649
                                        1,5:    RAMSEL = {rstatus[1], 3'b001};          // DE
1650
                                        2,3:    RAMSEL = 4'b0101;       //      IX
1651
                                        6,7:    RAMSEL = 4'b0110;               // IY
1652
                                endcase
1653
                        3: RAMSEL = {rstatus[0], 3'b011}; // A-TL
1654
                        4:      RAMSEL = 4; // I-R
1655
                        5: RAMSEL = 12; // tmp SP
1656
                        6: RAMSEL = 13; // zero
1657
                        7: RAMSEL = 7;  // temp reg for BIT/SET/RES
1658
                endcase
1659
        end
1660
endmodule
1661
 
1662
module RAM16X8D_regs(
1663
      input [3:0]A,              // R/W address 
1664
      input [7:0]D,        // Write data input
1665
      input [3:0]DPRA,           // Read-only address
1666
      input WCLK,                       // Write clock
1667
      input WE,                 // Write enable
1668
 
1669
      output [7:0]DPO,     // Read-only data output
1670
      output [7:0]SPO      // R/W data output
1671
   );
1672
 
1673
        reg [7:0]data[15:0];
1674
        assign DPO = data[DPRA];
1675
        assign SPO = data[A];
1676
 
1677
        always @(posedge WCLK)
1678
                if(WE) data[A] <= D;
1679
 
1680
endmodule
1681
 
1682
//FLAGS: S Z X1 N X2 PV N C
1683
//      OP[4:0]
1684
//      00000   -       ADD     D0,D1
1685
//      00001   -       ADC     D0,D1
1686
//      00010   -       SUB     D0,D1
1687
//      00011   -       SBC     D0,D1
1688
//      00100   -       AND     D0,D1
1689
//      00101   -       XOR     D0,D1
1690
//      00110   -       OR              D0,D1
1691
//      00111   -       CP              D0,D1
1692
//      01000   -       INC     D0
1693
//      01001   -       CPL     D0
1694
// 01010        -       DEC     D0
1695
//      01011   -       RRD
1696
// 01100        -       RLD
1697
//      01101   -       DAA
1698
//      01110   -       INC16
1699
//      01111   -  DEC16
1700
// 10000        -       ADD16LO
1701
//      10001   -       ADD16HI
1702
//      10010   -       
1703
//      10011   -       
1704
//      10100   -       CCF, pass D0
1705
// 10101        -       SCF, pass D0
1706
// 10110        -       
1707
//      10111   -       
1708
//      11000   -       RLCA    D0
1709
//      11001   -       RRCA    D0
1710
//      11010   -       RLA     D0
1711
//      11011   -       RRA     D0
1712
//      11100   -       {ROT, BIT, SET, RES} D0,EXOP 
1713
//                                RLC           D0                      C <-- D0 <-- D0[7]
1714
//            RRC               D0                      D0[0] --> D0 --> C
1715
//            RL                D0                      C <-- D0 <-- C
1716
//            RR                D0                      C --> D0 --> C
1717
//            SLA               D0                      C <-- D0 <-- 0
1718
//            SRA               D0                      D0[7] --> D0 --> C
1719
//            SLL               D0                      C <-- D0 <-- 1
1720
//            SRL               D0                      0 --> D0 --> C
1721
//      11101   -       IN, pass D1
1722
//      11110   -       FLAGS <- D0
1723
//      11111   -       NEG     D1      
1724
///////////////////////////////////////////////////////////////////////////////////
1725
module ALU8(
1726
    input [7:0] D0,
1727
    input [7:0] D1,
1728
         input [7:0] FIN,
1729
    input [4:0] OP,
1730
         input [5:0] EXOP, // EXOP[5:4] = 2'b11 for CPI/D/R
1731
         input LDIFLAGS,         // zero HF and NF on inc/dec16
1732
         input DSTHI,            // destination lo
1733
 
1734
    output reg[7:0] FOUT,
1735
    output reg [15:0] ALU8DOUT
1736
    );
1737
 
1738
        wire [7:0] daaadjust;
1739
        wire cdaa, hdaa;
1740
 
1741
        daa daa_adjust
1742
        (
1743
                .flags(FIN),
1744
                .val(D0),
1745
 
1746
                .adjust(daaadjust),
1747
                .cdaa(cdaa),
1748
                .hdaa(hdaa)
1749
        );
1750
 
1751
        wire parity = ~^ALU8DOUT[15:8];
1752
        wire zero = ALU8DOUT[15:8] == 0;
1753
        reg csin, cin;
1754
        wire [7:0]d0mux = OP[4:1] == 4'b1111 ? 0 : D0;
1755
        reg [7:0]_d1mux;
1756
        wire [7:0]d1mux = OP[1] ? ~_d1mux : _d1mux;
1757
        wire [8:0]sum;
1758
        wire hf;
1759
        assign {hf, sum[3:0]} = d0mux[3:0] + d1mux[3:0] + cin;
1760
        assign sum[8:4] = d0mux[7:4] + d1mux[7:4] + hf;
1761
        wire overflow = (d0mux[7] & d1mux[7] & !sum[7]) | (!d0mux[7] & !d1mux[7] & sum[7]);
1762
        reg [7:0]dbit;
1763
 
1764
        always @* begin
1765
                ALU8DOUT = 16'hxxxx;
1766
                FOUT = 8'hxx;
1767
                case({OP[4:2]})
1768
                        0,1,4,7: _d1mux = D1;
1769
                        2: _d1mux = 1;
1770
                        3: _d1mux = daaadjust;          // DAA
1771
                        6,5: _d1mux = 8'hxx;
1772
                endcase
1773
                case({OP[2:0], FIN[0]})
1774
                        0,1,2,7,8,9,10,11,12,13: cin = 0;
1775
                        3,4,5,6,14,15: cin = 1;
1776
                endcase
1777
                case(EXOP[3:0])
1778
                        0: dbit =  8'b11111110;
1779
                        1: dbit =  8'b11111101;
1780
                        2: dbit =  8'b11111011;
1781
                        3: dbit =  8'b11110111;
1782
                        4: dbit =  8'b11101111;
1783
                        5: dbit =  8'b11011111;
1784
                        6: dbit =  8'b10111111;
1785
                        7: dbit =  8'b01111111;
1786
                        8: dbit =  8'b00000001;
1787
                        9: dbit =  8'b00000010;
1788
                        10: dbit = 8'b00000100;
1789
                        11: dbit = 8'b00001000;
1790
                        12: dbit = 8'b00010000;
1791
                        13: dbit = 8'b00100000;
1792
                        14: dbit = 8'b01000000;
1793
                        15: dbit = 8'b10000000;
1794
                endcase
1795
                case(OP[3] ? EXOP[2:0] : OP[2:0])
1796
                        0,5:     csin = D0[7];
1797
                        1:      csin = D0[0];
1798
                        2,3:    csin = FIN[0];
1799
                        4,7:    csin = 0;
1800
                        6:              csin = 1;
1801
                endcase
1802
                case(OP[4:0])
1803
                        0,1,2,3,8,10:    begin           // ADD, ADC, SUB, SBC, INC, DEC
1804
                                ALU8DOUT[15:8] = sum[7:0];
1805
                                ALU8DOUT[7:0] = sum[7:0];
1806
                                FOUT[0] = OP[3] ? FIN[0] : (sum[8] ^ OP[1]); // inc/dec
1807
                                FOUT[1] = OP[1];
1808
                                FOUT[2] = overflow;
1809
                                FOUT[3] = ALU8DOUT[11];
1810
                                FOUT[4] = hf ^ OP[1];
1811
                                FOUT[5] = ALU8DOUT[13];
1812
                                FOUT[6] = zero & (FIN[6] | ~EXOP[5] | ~DSTHI | OP[3]); //(EXOP[5] & DSTHI) ? (zero & FIN[6]) : zero;                            // adc16/sbc16
1813
                                FOUT[7] = ALU8DOUT[15];
1814
                        end
1815
                        16,17:  begin           // ADD16LO, ADD16HI
1816
                                ALU8DOUT[15:8] = sum[7:0];
1817
                                ALU8DOUT[7:0] = sum[7:0];
1818
                                FOUT[0] = sum[8];
1819
                                FOUT[1] = OP[1];
1820
                                FOUT[2] = FIN[2];
1821
                                FOUT[3] = ALU8DOUT[11];
1822
                                FOUT[4] = hf ^ OP[1];
1823
                                FOUT[5] = ALU8DOUT[13];
1824
                                FOUT[6] = FIN[6];
1825
                                FOUT[7] = FIN[7];
1826
                        end
1827
                        7: begin                // CP
1828
                                ALU8DOUT[15:8] = sum[7:0];
1829
                                FOUT[0] = EXOP[5] ? FIN[0] : !sum[8]; // CPI/D/R
1830
                                FOUT[1] = OP[1];
1831
                                FOUT[2] = overflow;
1832
                                FOUT[3] = D1[3];
1833
                                FOUT[4] = !hf;
1834
                                FOUT[5] = D1[5];
1835
                                FOUT[6] = zero;
1836
                                FOUT[7] = ALU8DOUT[15];
1837
                        end
1838
                        31:     begin           // NEG
1839
                                ALU8DOUT[15:8] = sum[7:0];
1840
                                FOUT[0] = !sum[8];
1841
                                FOUT[1] = OP[1];
1842
                                FOUT[2] = overflow;
1843
                                FOUT[3] = ALU8DOUT[11];
1844
                                FOUT[4] = !hf;
1845
                                FOUT[5] = ALU8DOUT[13];
1846
                                FOUT[6] = zero;
1847
                                FOUT[7] = ALU8DOUT[15];
1848
                        end
1849
                        4: begin                        // AND
1850
                                ALU8DOUT[15:8] = D0 & D1;
1851
                                FOUT[0] = 0;
1852
                                FOUT[1] = 0;
1853
                                FOUT[2] = parity;
1854
                                FOUT[3] = ALU8DOUT[11];
1855
                                FOUT[4] = 1;
1856
                                FOUT[5] = ALU8DOUT[13];
1857
                                FOUT[6] = zero;
1858
                                FOUT[7] = ALU8DOUT[15];
1859
                        end
1860
                        5,6: begin              //XOR, OR
1861
                                ALU8DOUT[15:8] = OP[0] ? (D0 ^ D1) : (D0 | D1);
1862
                                FOUT[0] = 0;
1863
                                FOUT[1] = 0;
1864
                                FOUT[2] = parity;
1865
                                FOUT[3] = ALU8DOUT[11];
1866
                                FOUT[4] = 0;
1867
                                FOUT[5] = ALU8DOUT[13];
1868
                                FOUT[6] = zero;
1869
                                FOUT[7] = ALU8DOUT[15];
1870
                        end
1871
                        9: begin                        // CPL
1872
                                ALU8DOUT[15:8] = ~D0;
1873
                                FOUT[0] = FIN[0];
1874
                                FOUT[1] = 1;
1875
                                FOUT[2] = FIN[2];
1876
                                FOUT[3] = ALU8DOUT[11];
1877
                                FOUT[4] = 1;
1878
                                FOUT[5] = ALU8DOUT[13];
1879
                                FOUT[7:6] = FIN[7:6];
1880
                        end
1881
                        11,12: begin                                    // RLD, RRD
1882
                                if(OP[0]) ALU8DOUT = {D0[7:4], D1[3:0], D0[3:0], D1[7:4]};
1883
                                else ALU8DOUT = {D0[7:4], D1[7:0], D0[3:0]};
1884
                                FOUT[0] = FIN[0];
1885
                                FOUT[1] = 0;
1886
                                FOUT[2] = parity;
1887
                                FOUT[3] = ALU8DOUT[11];
1888
                                FOUT[4] = 0;
1889
                                FOUT[5] = ALU8DOUT[13];
1890
                                FOUT[6] = zero;
1891
                                FOUT[7] = ALU8DOUT[15];
1892
                        end
1893
                        13: begin       // DAA
1894
                                ALU8DOUT[15:8] = sum[7:0];
1895
                                FOUT[0] = cdaa;
1896
                                FOUT[1] = FIN[1];
1897
                                FOUT[2] = parity;
1898
                                FOUT[3] = ALU8DOUT[11];
1899
                                FOUT[4] = hdaa;
1900
                                FOUT[5] = ALU8DOUT[13];
1901
                                FOUT[6] = zero;
1902
                                FOUT[7] = ALU8DOUT[15];
1903
                        end
1904
                        14,15: begin    // inc/dec 16
1905
                                ALU8DOUT = {D0, D1} + (OP[0] ? 16'hffff : 16'h0001);
1906
                                FOUT[0] = FIN[0];
1907
                                FOUT[1] = LDIFLAGS ? 1'b0 : FIN[1];
1908
                                FOUT[2] = ALU8DOUT != 0;
1909
                                FOUT[3] = FIN[3];
1910
                                FOUT[4] = LDIFLAGS ? 1'b0 : FIN[4];
1911
                                FOUT[5] = FIN[5];
1912
                                FOUT[6] = FIN[6];
1913
                                FOUT[7] = FIN[7];
1914
                        end
1915
                        20,21: begin            // CCF, SCF
1916
                                ALU8DOUT[15:8] = D0;
1917
                                FOUT[0] = OP[0] ? 1'b1 : !FIN[0];
1918
                                FOUT[1] = 1'b0;
1919
                                FOUT[2] = FIN[2];
1920
                                FOUT[3] = ALU8DOUT[11];
1921
                                FOUT[4] = OP[0] ? 1'b0 : FIN[0];
1922
                                FOUT[5] = ALU8DOUT[13];
1923
                                FOUT[6] = FIN[6];
1924
                                FOUT[7] = FIN[7];
1925
                        end
1926
                        24,25,26,27, 28: begin                                                  // ROT, BIT, RES, SET
1927
                                case({OP[2], EXOP[4:3]})
1928
                                        0,1,2,3,4:       // rot - shift
1929
                                                if(OP[2] ? EXOP[0] : OP[0]){ALU8DOUT[15:8], FOUT[0]} = {csin, D0};         // right
1930
                                                else                                                            {FOUT[0], ALU8DOUT[15:8]} = {D0, csin};          // left
1931
                                        5,6: begin      // BIT, RES 
1932
                                                FOUT[0] = FIN[0];
1933
                                                ALU8DOUT[15:8] = D0 & dbit;
1934
                                        end
1935
                                        7: begin        // SET
1936
                                                FOUT[0] = FIN[0];
1937
                                                ALU8DOUT[15:8] = D0 | dbit;
1938
                                        end
1939
                                endcase
1940
                                ALU8DOUT[7:0] = ALU8DOUT[15:8];
1941
                                FOUT[1] = 0;
1942
                                FOUT[2] = OP[2] ? (EXOP[3] ? zero : parity) : FIN[2];
1943
                                FOUT[3] = ALU8DOUT[11];
1944
                                FOUT[4] = OP[2] & EXOP[3];
1945
                                FOUT[5] = ALU8DOUT[13];
1946
                                FOUT[6] = OP[2] ? zero : FIN[6];
1947
                                FOUT[7] = OP[2] ? ALU8DOUT[15] : FIN[7];
1948
                        end
1949
                        29:     begin           // IN, pass D1
1950
                                ALU8DOUT = {D1, D1};
1951
                                FOUT[0] = FIN[0];
1952
                                FOUT[1] = 0;
1953
                                FOUT[2] = parity;
1954
                                FOUT[3] = ALU8DOUT[11];
1955
                                FOUT[4] = 0;
1956
                                FOUT[5] = ALU8DOUT[13];
1957
                                FOUT[6] = zero;
1958
                                FOUT[7] = ALU8DOUT[15];
1959
                        end
1960
                        30: FOUT = D0;          // FLAGS <- D0
1961
                        default:;
1962
                endcase
1963
        end
1964
endmodule
1965
 
1966
module daa (
1967
        input [7:0]flags,
1968
        input [7:0]val,
1969
 
1970
        output [7:0]adjust,
1971
        output reg cdaa,
1972
        output reg hdaa
1973
        );
1974
 
1975
        wire h08 = val[7:4] < 9;
1976
        wire h09 = val[7:4] < 10;
1977
        wire l05 = val[3:0] < 6;
1978
        wire l09 = val[3:0] < 10;
1979
        reg [1:0]aa;
1980
        assign adjust = ({1'b0, aa[1], aa[1], 2'b0, aa[0], aa[0], 1'b0} ^ {8{flags[1]}}) + flags[1];
1981
 
1982
        always @* begin
1983
                case({flags[0], h08, h09, flags[4], l09})
1984
                        5'b00101, 5'b01101:     aa = 0;
1985
                        5'b00111, 5'b01111, 5'b01000, 5'b01010, 5'b01100, 5'b01110:     aa = 1;
1986
                        5'b00001, 5'b01001, 5'b10001, 5'b10101, 5'b11001, 5'b11101:     aa = 2;
1987
                        default: aa = 3;
1988
                endcase
1989
                case({flags[0], h08, h09, l09})
1990
                        4'b0011, 4'b0111, 4'b0100, 4'b0110:     cdaa = 0;
1991
                        default: cdaa = 1;
1992
                endcase
1993
                case({flags[1], flags[4], l05, l09})
1994
                        4'b0000, 4'b0010, 4'b0100, 4'b0110, 4'b1110, 4'b1111:   hdaa = 1;
1995
                        default:        hdaa = 0;
1996
                endcase
1997
        end
1998
endmodule
1999
 
2000
 
2001
module ALU16(
2002
    input [15:0]D0,
2003
    input [7:0]D1,
2004
    input [2:0]OP,       // 0-NOP, 1-INC, 2-INC2, 3-ADD, 4-NOP, 5-DEC, 6-DEC2
2005
 
2006
    output [15:0]DOUT
2007
    );
2008
 
2009
        reg [7:0] mux;
2010
        always @*
2011
                case(OP)
2012
                        0: mux = 8'h00;  // post inc
2013
                        1: mux = 8'h01; // post inc
2014
                        2: mux = 8'h02; // post inc
2015
                        3: mux = D1;            // post inc
2016
                        4: mux = 8'h00; // no post inc                  
2017
                        5: mux = 8'hff; // no post inc
2018
                        6: mux = 8'hfe; // no post inc
2019
                        default: mux = 8'hxx;
2020
                endcase
2021
 
2022
        assign DOUT = D0 + {{8{mux[7]}}, mux};
2023
endmodule

powered by: WebSVN 2.1.0

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