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

Subversion Repositories vg_z80_sbc

[/] [vg_z80_sbc/] [trunk/] [rtl/] [z80_inst_exec.v] - Blame information for rev 36

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 hharte
///////////////////////////////////////////////////////////////////////////////////////////////////
2
//                                                                                               //
3
//  file name:   inst_exec.v                                                                     //
4
//  description: main execution engine for wishbone z80                                          //
5
//  project:     wb_z80                                                                          //
6
//                                                                                               //
7
//  Author: B.J. Porcella                                                                        //
8
//  e-mail: bporcella@sbcglobal.net                                                              //
9
//                                                                                               //
10
//                                                                                               //
11
//                                                                                               //
12
///////////////////////////////////////////////////////////////////////////////////////////////////
13
//                                                                                               //
14
// Copyright (C) 2000-2002 B.J. Porcella                                                         //
15
//                         Real Time Solutions                                                   //
16
//                                                                                               //
17
//                                                                                               //
18
// This source file may be used and distributed without                                          //
19
// restriction provided that this copyright statement is not                                     //
20
// removed from the file and that any derivative work contains                                   //
21
// the original copyright notice and the associated disclaimer.                                  //
22
//                                                                                               //
23
//     THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY                                       //
24
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED                                     //
25
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS                                     //
26
// FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR                                        //
27
// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,                                           //
28
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES                                      //
29
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE                                     //
30
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR                                          //
31
// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF                                    //
32
// LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT                                    //
33
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT                                    //
34
// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE                                           //
35
// POSSIBILITY OF SUCH DAMAGE.                                                                   //
36
//                                                                                               //
37
//-------1---------2---------3--------Comments on file  -------------7---------8---------9--------0
38
//
39
// This file contains the data related registers of the z80 and the
40
// logic required to update them.  Included registers are:
41
//  ar   fr
42
//  br   cr
43
//  dr   er
44
//  hr   lr
45
//    ixr
46
//    iyr
47
//  intr
48
//
49
//  and the "prime" registers
50
//  ap  fp
51
//  bp  cp
52
//  dp  ep
53
//  hp  lp
54
//
55
// This logic can be considered a "slave" to the memstate sequencer (in memstate2.v).  
56
// as memstate sequencer executes any instruction from ir1 (the of - os pipe) the instruction
57
// gets transferred to ir2 - which now becomes active.  
58
//
59
// In the case of any memory type instruction (HL) , the pipeline must stall 1 tick to get the
60
// operand into the nn register.  This file logic needs not understand any of that --  just 
61
// execute when told to (ir2_val).
62
//
63
// From a block diagram standpoint this file is somewhat messy.  There are multiple ALU's and 
64
// multiple source multiplexors.  Part of the reason for this is hardware speed  -  the 
65
// various additions start pretty early in the cycle ( as not much decode logic is needed to 
66
// get them started.   In parallel with that - the destination selectors ( which require more
67
// complex decoding logic ) are "doing thier thing"  No claim that this is absolute optimum - any 
68
// good synthesizer should be able to make the basic structure faster when flattened.   However, 
69
// the intention is that even if the synthesizer is pretty primitive -- reasonably fast hardware 
70
// will be produced.
71
// 
72
//-------1---------2---------3--------CVS Log -----------------------7---------8---------9--------0
73
//
74
//  $Id: z80_inst_exec.v,v 1.1 2008-12-01 02:00:10 hharte Exp $
75
//
76
//  $Date: 2008-12-01 02:00:10 $
77
//  $Revision: 1.1 $
78
//  $Author: hharte $
79
//  $Locker:  $
80
//  $State: Exp $
81
//
82
// Change History:
83
//      $Log: not supported by cvs2svn $
84
//      Revision 1.4  2004/05/21 02:51:25  bporcella
85
//      inst test  got to the worked macro
86
//
87
//      Revision 1.3  2004/05/18 22:31:21  bporcella
88
//      instruction test getting to final stages
89
//
90
//      Revision 1.2  2004/05/13 14:58:53  bporcella
91
//      testbed built and verification in progress
92
//
93
//      Revision 1.1  2004/04/27 21:27:13  bporcella
94
//      first core build
95
//
96
//      Revision 1.4  2004/04/19 19:13:27  bporcella
97
//      real lint problems pretty much fixed   --  need another look - but need to get on to other things first
98
//
99
//      Revision 1.3  2004/04/19 05:09:11  bporcella
100
//      fixed some lint problems  --
101
//
102
//      Revision 1.2  2004/04/18 18:50:08  bporcella
103
//      fixed some lint problems  --
104
//
105
//      Revision 1.1.1.1  2004/04/13 23:49:54  bporcella
106
//      import first files
107
//
108
//
109
//
110
//-------1---------2---------3--------Module Name and Port List------7---------8---------9--------0
111
module z80_inst_exec( br_eq0,
112
                  cr_eq0,
113
                  upd_ar, upd_br, upd_cr, upd_dr, upd_er, upd_hr, upd_lr,upd_fr,
114
                  ar, fr, br, cr, dr, er, hr, lr, intr,
115
                  ixr, iyr, add16, alu8_out,  sh_alu, bit_alu,
116
                   exec_ir2,
117
                   exec_decbc, exec_decb,
118
                   adr_alu,
119
                   blk_mv_upd_hl,
120
                   blk_mv_upd_de,
121
                   ir2,
122
                   clk,
123
                   rst,
124
                   nn, sp,
125
                   ir2dd,
126
                   ir2fd
127
                   );
128
 
129
//-------1---------2---------3--------Output Ports---------6---------7---------8---------9--------0
130
output          br_eq0;
131
output          cr_eq0;
132
output          upd_ar, upd_br, upd_cr, upd_dr, upd_er, upd_hr, upd_lr,upd_fr;
133
output  [7:0]   ar, fr, br, cr, dr, er, hr, lr, intr;
134
output  [15:0]  ixr, iyr;
135
output  [15:0]  add16;
136
output  [7:0]   alu8_out;   // used for INCs6HL7 and DECs6HL7 types ---   flags need updating
137
                            // also so need to do with alu8. 
138
output  [7:0]   sh_alu;
139
output  [7:0]   bit_alu;
140
//-------1---------2---------3--------Input Ports----------6---------7---------8---------9--------0
141
input        exec_ir2;
142
input        exec_decbc;  // in general this needs to happen at different time from exec
143
input        exec_decb;   // in general - we don't have the EB instruction (yet) when this hits
144
input [9:0]  ir2;
145
input        clk;
146
input        rst;
147
input [15:0] nn, sp;
148
input        ir2dd;       // this must be ir2
149
input        ir2fd;
150
input [15:0] adr_alu;
151
input        blk_mv_upd_hl;
152
input        blk_mv_upd_de;
153
//-------1---------2---------3--------Parameters-----------6---------7---------8---------9--------0
154
`include "opcodes.v"
155
 
156
//-------1---------2---------3--------Wires----------------6---------7---------8---------9--------0
157
 
158
//wire [7:0]   src_pqr;    //  arithmetic sources gven by ir2[2:0]
159
wire [7:0]   src_hr ;
160
wire [7:0]   src_lr ;
161
//wire [7:0]   alu_out;  // {CF. 8bit_result}
162
//wire         alu_cry;
163
 
164
//wire            c_in0, c_out7, c_in8, c_out11, cout15;
165
wire   [15:0]   src_a, src_b;
166
wire   [15:0]   add16;
167
wire            sf, zf, f5f, hf, f3f, pvf, nf, cf;
168
wire   [7:0]    daa_alu; // {cry, number}   hf goes to 0 always.
169
wire            daa_cry;
170
wire            upd_ar, upd_br, upd_cr, upd_dr, upd_er, upd_fr, upd_hr, upd_lr;
171
wire        c_8out3;
172
wire [7:0]  add_8bit;
173
 
174
wire  [15:0]  src_dblhr       ;
175
//wire          src_cb_r20      ;
176
wire  [7:0]  src_pqr20       ;
177
wire  [7:0]   src_pqr53       ;
178
wire  [15:0]  src_dbl         ;
179
wire  [7:0]   alu8_fr         ;
180
wire          alu8_nf         ;
181
wire          c_8out7         ;
182
wire          alu8_cry        ;
183
wire          alu8_pvf        ;
184
wire          alu8_hcry       ;
185
wire  [7:0]   alu8_out        ;
186
wire          add16_ofl       ;
187
wire          c_16out7        ;
188
wire          c_16out11       ;
189
wire          c_16out15       ;
190
wire          c_16in0         ;
191
wire          sh_cry          ;
192
wire  [7:0]   sh_alu          ;
193
wire          sh_alu_act      ;
194
wire          bit_alu_act     ;
195
wire  [7:0]   bit_alu         ;
196
wire  [7:0]   decc_alu        ;
197
wire  [7:0]   decb_alu        ;
198
wire          upd_a_alu8      ;
199
wire          up_a_sh_alu     ;
200
wire          up_a_src_pqr    ;
201
wire          up_a_n          ;
202
wire          upd_b_alu8      ;
203
wire          up_b_src_pqr    ;
204
wire          up_b_add16      ;
205
wire [7:0]    sh_src          ;
206
 
207
wire          up_c_add16       ;
208
wire          upd_c_alu8       ;
209
wire          up_c_src_pqr     ;
210
wire          up_d_add16       ;
211
wire          upd_d_alu8       ;
212
wire          up_d_src_pqr     ;
213
wire          up_e_add16       ;
214
wire          upd_e_alu8       ;
215
wire          up_e_src_pqr     ;
216
wire          up_h_add16       ;
217
wire          upd_h_alu8       ;
218
wire          upd_h_src_pqr    ;
219
wire          up_l_add16       ;
220
wire          upd_l_alu8       ;
221
wire          upd_l_src_pqr    ;
222
wire          upd_bc_cpi       ;
223
wire          upd_fr_alu8      ;
224
wire          upd_fr_add16     ;
225
wire          upd_fr_edadd16   ;
226
wire          upd_fr_sh        ;
227
wire          upd_fr_cbsh      ;
228
//wire          eb_blk_mv        ;
229
wire          ed_blk_cp        ;
230
wire          c_8in0           ;
231
 
232
//-------1---------2---------3--------Registers------------6---------7---------8---------9--------0
233
 
234
reg [7:0] ar, fr, br, cr, dr, er, hr, lr, intr;
235
reg [7:0] ap, fp, bp, cp, dp, ep, hp, lp;
236
reg [15:0] ixr, iyr;
237
//-------1---------2---------3--------Assignments----------6---------7---------8---------9--------0
238
 
239
//  it appears that dd and fd as a prefix to cb has a significantly modfied fuction......
240
//  specifically, it is assumed that a memory operation is to be implemented (ix + d)
241
// ,  In fact the 
242
// pipeline is such that we can make a fetch for free  - so we will do that.....   the 
243
// prefix flags should not be set here   -- all we will know on execution is that it is a 
244
// cb instruction.   ----   src is always nn 
245
assign  src_hr = ir2dd ? ixr[15:8] :
246
                 ir2fd ? iyr[15:8] :
247
                 hr                   ;
248
 
249
assign  src_lr = ir2dd ? ixr[7:0] :
250
                 ir2fd ? iyr[7:0] :
251
                 lr                 ;
252
 
253
assign src_dblhr = ir2dd ? ixr :    // ed grp instructions (ADC HL ; SBC HL are not affected -
254
                   ir2fd ? iyr :    // instruction assembler assures this - ed_grp has no prefix
255
                   {hr, lr}      ;
256
//  ddcb_grp not defined  - src_cb_r20  not used.  Why these lines?  4/17/2004
257
//assign  src_cb_r20 = (ddcb_grp | fdcb_grp) ? nn[7:0]   :
258
//                             cb_grp        ? src_pqr20 :
259
//                             ar                        ;
260
assign  br_eq0 = ~|br; // for first cut do this quick and dirty.   
261
assign  cr_eq0 = ~|cr; // if this becomes a critical path - make these registers.
262
assign  src_pqr20 = {8{ir2[2:0]==REG8_B   }} & br     |
263
                    {8{ir2[2:0]==REG8_C   }} & cr     |
264
                    {8{ir2[2:0]==REG8_D   }} & dr     |
265
                    {8{ir2[2:0]==REG8_E   }} & er     |
266
                    {8{ir2[2:0]==REG8_H   }} & src_hr |
267
                    {8{ir2[2:0]==REG8_L   }} & src_lr |
268
                    {8{ir2[2:0]==REG8_MEM}} & nn[15:8] |
269
                    {8{ir2[2:0]==REG8_A   }} & ar      ;
270
 
271
assign src_pqr53 =  {8{ir2[5:3]==REG8_B   }} & br     |
272
                    {8{ir2[5:3]==REG8_C   }} & cr     |
273
                    {8{ir2[5:3]==REG8_D   }} & dr     |
274
                    {8{ir2[5:3]==REG8_E   }} & er     |
275
                    {8{ir2[5:3]==REG8_H   }} & src_hr |
276
                    {8{ir2[5:3]==REG8_L   }} & src_lr |
277
                    {8{ir2[5:3]==REG8_MEM}} & nn[15:8] |
278
                    {8{ir2[5:3]==REG8_A   }} & ar      ;
279
 
280
 
281
assign src_dbl =   {16{ir2[5:4]==2'b00}} & {br, cr}  |
282
                   {16{ir2[5:4]==2'b01}} & {dr, er}  |
283
                   {16{ir2[5:4]==2'b10}} & src_dblhr |   // HL, ixr, iyr
284
                   {16{ir2[5:4]==2'b11}} & sp         ;
285
 
286
assign sh_src =   ir2[8] & ir2dd ?  nn[15:8]   :
287
                  ir2[8] & ir2fd ?  nn[15:8]   :
288
                  ir2[8]          ?  src_pqr20  :
289
                                     ar          ;
290
// I wonder how well the synthesizer can reduce this??? - It is probably worth spending
291
// some time during physical design to see if a more low level description would help --
292
// there is somebody out there who knows  -  and there is probably a good low level description.
293
//
294
// guess its kind of important to understand precisely what the synthesizer does 
295
// with some of the status things we need also. 
296
//
297
//
298
//  The nastiest status to get is HF.  Really need 4 bit adders to do that  ( or reproduce a lot
299
//  of logic.)  I don't have a lot of confdence in the synthesier's ability to minimize arithmetic
300
//  operations   --  Its a moving target of course, but I've seen some really silly stuff come out
301
//  of synthesis when you use a "+" operator.   guess I will be pretty explicit here.  
302
//  Documentation of the HF is srange.  IN and OUT operators are defined as X  -- but 16 bit operations
303
//  get set by CRY from bit 11.  (Do I care??? ) well probably not but it is documented  - so should 
304
//  be tested i guess.   
305
//
306
//  
307
//  may want to re-define as a module with carry look-ahead ?  
308
//
309
//  Had a notion to define a single adder - subtractor for both 8 and 16 bit operations, but 
310
//  getting into source mux issues that solution scared me.....   Worry the cry flag might
311
//  become a worst case path.   As defined, a good chunk of the decode process can go on in 
312
//  parallel with the cry computation ---   with final decisions made using a small mux at 
313
//  the flag register.
314
//  ------------ 8 bit adder for accumulator ops  plus the INC DEC ops ---------------------
315
//  It is documented that the hf is modified by the INC and DEC ops even if ar is not the 
316
//  destination of result   ---   clearly hf and nf are pretty usless on a INC B but ours is 
317
//  not to reason why :-)  ----   well its fun to bitch about silly stuff like this.  
318
//  ( not as much fun to deal with instruction tests testing "features" -- or worse programmers
319
//   who figure out ways to use theses "features". )
320
//
321
//  8 bit adder with cry out of bit 3   used for most operations on A as well as the 
322
//  inc/dec instructions.   also need to get ED44 (ar <= -ar) working here
323
wire [7:0]  src_pqri;  // use just here and below
324
wire [7:0]  src_aor_cnst = ed_blk_cp ?  ar    :  // CPI CPIR CPD CPDR
325
                              ir2[9] ?  8'h0  :  // for ed44 -a  //ed_grp == ir2[9]
326
                              ir2[7] ?  ar    :
327
                              ir2[0] ?  8'hff :
328
                                        8'h00  ;
329
 
330
//---------------  the "standard" flag logic -----------------------------
331
//                 sf           zf            f5f          hf        
332
assign alu8_fr ={alu8_out[7], ~|alu8_out, alu8_out[5], alu8_hcry,
333
//                 f3f          fpv           fn         fc
334
                 alu8_out[3], alu8_pvf, alu8_nf,  alu8_cry };
335
 //   excludeINC_r DEC_r        AND               XOR                    OR
336
assign alu8_pvf = ir2[7] & (ir2[5:3]==7'b100 | ir2[5:3]==7'b101 | ir2[5:3]==7'b110) ?
337
                                                                    ~^alu8_out   : // even parity
338
                (src_aor_cnst[7]==src_pqri[7]) & (src_aor_cnst[7]!=alu8_out[7])  ; // ofl 
339
 
340
assign alu8_nf = (ir2[7:3]==5'b10010)       |
341
                 (ir2[7:3]==5'b10011)       |
342
                 (ir2[7:6]==2'b00) & ir2[0] |
343
                 ir2[9]                      ;
344
 
345
assign {c_8out3, add_8bit[3:0]} = {1'b0, src_aor_cnst[3:0]} + {1'b0, src_pqri[3:0]}   + {4'b0, c_8in0};
346
//wire [4:0] ha_temp = {1'b0, src_aor_cnst[3:0]} + {1'b0, src_pqri[3:0]}   + {4'b0, c_8in0};
347
//assign c_8out3 
348
 
349
assign {c_8out7, add_8bit[7:4]} = {1'b0, src_aor_cnst[7:4]} + {1'b0, src_pqri[7:4]}   + {4'b0, c_8out3};
350
 
351
//  notice that both inputs and outputs of the adder are being selected below.
352
//  making ed_blk_cp high priority kind of negates the origional idea of making the
353
//  decodes fast here  ---   course when all is included this can't be too fast.
354
//  Just note for syntheses that this is a slow path that could be improved with some thought.
355
//         1         1          8          8        1
356
assign {alu8_cry, alu8_hcry, alu8_out,  src_pqri, c_8in0 }=
357
 
358
   ed_blk_cp ?                         {c_8out7,c_8out3,  add_8bit,   ~nn[15:8], 1'h1} :   //CPI CPIR CPD CPDR
359
 
360
   {19{ir2[7] & ir2[5:3]==3'b000}} & ({c_8out7,c_8out3,  add_8bit,    src_pqr20, 1'b0} )  |// a+src
361
   {19{ir2[7] & ir2[5:3]==5'b001}} & ({c_8out7,c_8out3,  add_8bit,    src_pqr20,   cf} )  |// a+src+cf
362
   {19{ir2[7] & ir2[5:3]==5'b010}} & ({~c_8out7,c_8out3,  add_8bit,   ~src_pqr20, 1'h1} )  |// a-src
363
   {19{ir2[7] & ir2[5:3]==5'b011}} & ({~c_8out7,c_8out3,  add_8bit,   ~src_pqr20, ~cf } )  |// a-src-cf
364
   {19{ir2[7] & ir2[5:3]==5'b100}} & ({1'b0   ,1'b1   , ar & src_pqr20, src_pqr20, 1'b0} )|// a&src
365
   {19{ir2[7] & ir2[5:3]==5'b101}} & ({1'b0   ,1'b0   , ar ^ src_pqr20, src_pqr20, 1'b0} )|// a^src
366
   {19{ir2[7] & ir2[5:3]==5'b110}} & ({1'b0   ,1'b0   , ar | src_pqr20, src_pqr20, 1'b0} )|// a|src
367
   {19{ir2[7] & ir2[5:3]==5'b111}} & ({~c_8out7,c_8out3,  add_8bit,   ~src_pqr20,  1'h1})  |// a-src
368
   {19{(ir2[7:6]==2'b00)& ~ir2[0] }}& ({     cf,c_8out3,  add_8bit,    src_pqr53,  1'h1}) |// inc_r main
369
   {19{(ir2[7:6]==2'b00)&  ir2[0] }}& ({     cf,c_8out3,  add_8bit,    src_pqr53,  1'h0}) |// dec_r
370
   {19{(ir2[7:6]==2'b01)          }}& ({~c_8out7,c_8out3,  add_8bit,          ~ar,  1'h1})  ;// ed44 -a
371
 
372
 
373
// do some hand  decoding here                                        
374
//  ADDsHL_BC    = 'h09,  DECsBC       = 'h0B, INCsBC       = 'h03    compair with {ir2[7:6],ir2[3:0]}
375
//  ADDsHL_DE    = 'h19,  DECsDE       = 'h1B  INCsDE       = 'h13    ED_SBCsHL_REG  = 6'b01__0010
376
//  ADDsHL_HL    = 'h29,  DECsHL       = 'h2B  INCsHL       = 'h23    ED_ADCsHL_REG  = 6'b01__1010
377
//  ADDsHL_SP    = 'h39,  DECsSP       = 'h3B  INCsSP       = 'h33
378
//  by inspection just use ir2[3:0]  -  i guess in a pinch we do't need ir2[2]  =  but let the 
379
//  synthesizer figure that out. - it should be able to.
380
//
381
 
382
 
383
// ---------------- 16 bit adder with bit 11 carrry out and bit 8 carry in ------------------
384
//
385
assign add16_ofl = (src_a[15] == src_b[15]) & (src_a[15] != add16[15]);
386
 ///tmp/lint/wb_z80/rtl/inst_exec.v(363): Warning 22014: synchronous loop without set/reset detected on signal "src_b[11:8]" (OC)
387
assign {c_16out7,  add16[7:0]}  = {1'b0, src_a[7:0]}   + {1'b0, src_b[7:0]   } + {8'b0, c_16in0};
388
assign {c_16out11, add16[11:8]} = {1'b0, src_a[11:8]}  + {1'b0, src_b[11:8]  } + {4'b0, c_16out7};
389
assign {c_16out15, add16[15:12]} = {1'b0, src_a[15:12]} + {1'b0, src_b[15:12]} + {4'b0, c_16out11};
390
 
391
assign  { src_a,     src_b, c_16in0} =         // assigning 33 bits
392
   {33{ir2[3:0] == 4'h9}} & {src_dblhr, src_dbl  ,1'b0 }   |  //ADD
393
   {33{ir2[3:0] == 4'hb}} & {16'hffff , src_dbl  ,1'b0 }   |  //DEC 
394
   {33{ir2[3:0] == 4'h3}} & {16'h0001 , src_dbl  ,1'b0 }   |  //INC
395
   {33{ir2[3:0] == 4'h2}} & {src_dblhr, ~src_dbl , ~cf }   |  //SBC
396
   {33{ir2[3:0] == 4'ha}} & {src_dblhr, src_dbl  , cf  }    ; //ADC
397
 
398
//-------------------------- sh alu --------------------------------------------------
399
//  shift insructions.  Think of these as 8 shift types:
400
//  RLC RL RRC RR SLA SLL SRA SRL  The SLL types appear to be undocumented  -- but possibly used 
401
//   in assembly code as they appear to have some utility  -  and by all accounts operate reliably. 
402
//   The first four are implemented in a single byte inaruction . (A <= sh_op A )
403
//   All 8  are implemented in the CB group with all registers as potential sources (and dests).
404
//   if ir2dd or ir2fd is prefix.....   source is always the memory. This is undocumented - but
405
//   may be a useful hint for simplyfing the total machine.  Destination registers
406
//   (if any) get a copy of the updated memory location  (This is also true of the bit set and 
407
//   clear instructions in the cb_grp.
408
 
409
assign {sh_cry, sh_alu} =  {9{ir2[5:3]==3'b000}} & {sh_src, sh_src[7] }                 | //RLC
410
                           {9{ir2[5:3]==3'b001}} & {sh_src[0], sh_src[0], sh_src[7:1]}  | // RRC
411
                           {9{ir2[5:3]==3'b010}} & {sh_src, cf  }                       | //RL 
412
                           {9{ir2[5:3]==3'b011}} & {sh_src[0], cf, sh_src[7:1] }        | // RR 
413
                           {9{ir2[5:3]==3'b100}} & {sh_src, 1'b0}                       |  //SLA
414
                           {9{ir2[5:3]==3'b101}} & {sh_src[0], sh_src[7], sh_src[7:1]}  |  //SRA
415
                           {9{ir2[5:3]==3'b110}} & {sh_src, 1'b1}                       |  //SLL
416
                           {9{ir2[5:3]==3'b111}} & {sh_src[0], 1'b0, sh_src[7:1]}      ;   //SRL
417
 
418
 
419
 // shift insts
420
 assign sh_alu_act = ir2[9:6] == 4'b0100;
421
 //CB_RLC   = 7'b01_00_000,  // these must be compaired with ir2[9:3]
422
 //CB_RRC   = 7'b01_00_001,  // these must be compaired with ir2[9:3]
423
 //CB_RL    = 7'b01_00_010,  // these must be compaired with ir2[9:3]
424
 //CB_RR    = 7'b01_00_011,  // these must be compaired with ir2[9:3]
425
 //CB_SLA   = 7'b01_00_100,  // these must be compaired with ir2[9:3]
426
 //CB_SRA   = 7'b01_00_101,  // these must be compaired with ir2[9:3]
427
 //CB_SLL   = 7'b01_00_110,  // these must be compaired with ir2[9:3]
428
 //CB_SRL   = 7'b01_00_111,  // these must be compaired with ir2[9:3]
429
 
430
//---------------------------- bit test alu ---------------------------------------
431
//  bit test insts
432
//CB_BIT   = 4'b01_01,    // these must be compaired with ir2[9:6]
433
//CB_RES   = 4'b01_10,    // these must be compaired with ir2[9:6]assign 
434
//CB_SET   = 4'b01_11,    // these must be compaired with ir2[9:6] 
435
assign bit_alu_act = ir2[9:6] == CB_RES |
436
                     ir2[9:6] == CB_SET ;
437
 
438
wire [7:0] bit_decode = {8{ir2[5:3] == 3'h0}} & 8'h01 |
439
                        {8{ir2[5:3] == 3'h1}} & 8'h02 |
440
                        {8{ir2[5:3] == 3'h2}} & 8'h04 |
441
                        {8{ir2[5:3] == 3'h3}} & 8'h08 |
442
                        {8{ir2[5:3] == 3'h4}} & 8'h10 |
443
                        {8{ir2[5:3] == 3'h5}} & 8'h20 |
444
                        {8{ir2[5:3] == 3'h6}} & 8'h40 |
445
                        {8{ir2[5:3] == 3'h7}} & 8'h80 ;
446
 
447
assign bit_alu = {8{ir2[9:6] == CB_BIT}} & ( sh_src & bit_decode)  |
448
                 {8{ir2[9:6] == CB_RES}} & ( sh_src & ~bit_decode) |
449
                 {8{ir2[9:6] == CB_SET}} & ( sh_src | bit_decode)   ;
450
 
451
 
452
//------------ dec bc alu ---------------------------------------------
453
//exec_decbc;  these are all we know (in general)
454
//exec_decb;
455
assign decc_alu  =  cr + 8'hff ;
456
assign decb_alu  =  br + ( exec_decb ? 8'hff :    // just dec b if io blk move
457
                              cr_eq0 ? 8'hff :    // cry out if c in this case
458
                                      8'h00 );   // only dec c reg this tick
459
// ------------------ daa alu -------------------------------------------------------
460
// the documentation does not cover all cases here  -- only those that matter (i suppose).
461
// ( documentation assumes you are operating with 2 daa'd numbers  --  but of course the
462
// ar can contain many values that don't fit that assumption when this instruction is executed.
463
// Any arbitrary instruction test may test un-documented cases.
464
//
465
// this leaves me to guess what the actual logic is  - and how to match it.   
466
// So I am doing that -- see what happens. If an instruction test breaks this...  I should be 
467
// able to fix it easily.
468
//
469
wire [3:0] ls_nbl   =  (!nf & hf)                 ?  4'h6:
470
                       (!nf & (ar[3:0] > 4'h9))   ?  4'h6:
471
                       (nf  & hf )                ?  4'ha:
472
                                                     4'h0;
473
 
474
wire [4:0] ms_nbl   =  (!nf & cf)                 ?  5'h16:    //  includes new cry
475
                       (!nf & (ar[3:0]  > 4'h9))  ?  5'h16:
476
                       (!nf & (ar[3:0] == 4'h9) &
477
                               (ar[3:0] > 4'h9))  ?  5'h16:
478
                       (nf  & !cf  &  hf )        ?  5'h0f:
479
                       (nf  &  cf  & !hf )        ?  5'h1a:
480
                       (nf  &  cf  &  hf )        ?  5'h19:
481
                                                     5'h00;
482
 
483
 
484
assign {daa_cry, daa_alu} = { ms_nbl[4], {ar + { ms_nbl[3:0], ls_nbl}}  } ;
485
 
486
 
487
//-------1---------2---------3--------State Machines-------6---------7---------8---------9--------0
488
 
489
//  update ar
490
 
491
assign upd_a_alu8 =
492
    ADDsA_B      == ir2 | SUBsB      == ir2 |  ANDsB      == ir2 | ORsB         == ir2   |
493
    ADDsA_C      == ir2 | SUBsC      == ir2 |  ANDsC      == ir2 | ORsC         == ir2   |
494
    ADDsA_D      == ir2 | SUBsD      == ir2 |  ANDsD      == ir2 | ORsD         == ir2   |
495
    ADDsA_E      == ir2 | SUBsE      == ir2 |  ANDsE      == ir2 | ORsE         == ir2   |
496
    ADDsA_H      == ir2 | SUBsH      == ir2 |  ANDsH      == ir2 | ORsH         == ir2   |
497
    ADDsA_L      == ir2 | SUBsL      == ir2 |  ANDsL      == ir2 | ORsL         == ir2   |
498
    ADDsA_6HL7   == ir2 | SUBs6HL7   == ir2 |  ANDs6HL7   == ir2 | ORs6HL7      == ir2   |
499
    ADDsA_A      == ir2 | SUBsA      == ir2 |  ANDsA      == ir2 | ORsA         == ir2   |
500
    ADCsA_B      == ir2 | SBCsB      == ir2 |  XORsB      == ir2 |
501
    ADCsA_C      == ir2 | SBCsC      == ir2 |  XORsC      == ir2 | INCsA        == ir2   |
502
    ADCsA_D      == ir2 | SBCsD      == ir2 |  XORsD      == ir2 | DECsA        == ir2   |
503
    ADCsA_E      == ir2 | SBCsE      == ir2 |  XORsE      == ir2 |
504
    ADCsA_H      == ir2 | SBCsH      == ir2 |  XORsH      == ir2 |
505
    ADCsA_L      == ir2 | SBCsL      == ir2 |  XORsL      == ir2 |
506
    ADCsA_6HL7   == ir2 | SBCs6HL7   == ir2 |  XORs6HL7   == ir2 |
507
    ADCsA_A      == ir2 | SBCsA      == ir2 |  XORsA      == ir2 |
508
    ADDsA_N      == ir2 | //      ADD A,N      ; C6 XX   ADDsA_6HL7   = 'h86
509
    ADCsA_N      == ir2 | //      ADC A,N      ; CE XX   ADCsA_6HL7   = 'h8E
510
    SUBsN        == ir2 | //      SUB N        ; D6 XX   SUBs6HL7     = 'h96
511
    SBCsA_N      == ir2 | //      SBC A,N      ; DE XX
512
    ANDsN        == ir2 | //      AND N        ; E6 XX
513
    XORsN        == ir2 | //      XOR N        ; EE XX
514
    ORsN         == ir2 ; //      OR N         ; F6 XX
515
assign up_a_sh_alu =
516
    RLCA         == ir2   | //      RLCA        ; 07
517
    RRCA         == ir2   | //      RRCA        ; 0F
518
    RRA          == ir2   | //      RRA          ; 1F
519
    RLA          == ir2   ; //      RLA          ; 17
520
assign up_a_src_pqr =
521
    LDsA_B       == ir2 |    //      LD A,B       ; 78
522
    LDsA_C       == ir2 |    //      LD A,C       ; 79
523
    LDsA_D       == ir2 |    //      LD A,D       ; 7A
524
    LDsA_E       == ir2 |    //      LD A,E       ; 7B
525
    LDsA_H       == ir2 |    //      LD A,H       ; 7C
526
    LDsA_L       == ir2 |    //      LD A,L       ; 7D
527
    LDsA_6HL7    == ir2 |    //      LD A,(HL)    ; 7E
528
    LDsA_A       == ir2 ;    //      LD A,A       ; 7F
529
assign up_a_n =
530
    LDsA_N       == ir2 | //      LD A,N       ; 3E XX
531
    LDsA_6BC7    == ir2 | //      LD A,(BC)    ; 0A
532
    LDsA_6DE7    == ir2 | //      LD A,(DE)    ; 1A
533
    LDsA_6NN7    == ir2 | //      LD A,(NN)    ; 3A XX XX
534
    INsA_6N7     == ir2 | //      IN A,(N)     ; DB XX        
535
    (ED_INsREG_6C7 == {ir2[9:6],ir2[2:0]}) & (ir2[5:3] == REG8_A) ;
536
 
537
 
538
//EXsAF_AFp    = 10'h08,//      EX AF,AF'    ; 08
539
//EXX          = 10'hD9,//      EXX          ; D9
540
//DAA          = 10'h27,//      DAA          ; 27
541
//CPL          = 10'h2F,//      CPL          ; 2F   a <= ~a
542
//POPsAF       = 10'hF1,//      POP AF       ; F1
543
//  don't forget these beauties  not affected by prefixes
544
//ED_RRD       =  'h67//      RRD        ;  
545
//ED_RLD       =  'h6F//      RLD        ; ED 6F   nibble roates A (HL)
546
//ED_NEG         =    5'b01___100, // A<= -A   compair with {ir2[9:6],ir2[2:0]}                
547
 
548
//------------------------------- ar ------------------------------------------
549
 
550
assign upd_ar = upd_a_alu8 | up_a_sh_alu | up_a_src_pqr | up_a_n |  ir2 == EXsAF_AFp |
551
                ir2 == EXX | ir2 == DAA  | ir2 == CPL   | ir2 == POPsAF |
552
                ir2[2:0] == REG8_A & bit_alu_act | ir2[2:0] == REG8_A & sh_alu_act |
553
                ir2== ED_RRD | {ir2[9:6], ir2[2:0]} == ED_NEG |
554
                ir2 == ED_LDsA_I ;
555
 
556
always @(posedge clk)
557
begin
558
    if (upd_a_alu8 & exec_ir2)       ar <= alu8_out;
559
    if (up_a_sh_alu & exec_ir2)      ar <= sh_alu;
560
    if (up_a_src_pqr & exec_ir2)     ar <= src_pqr20;
561
    if (up_a_n  & exec_ir2)          ar <= nn[15:8];    // changed for LD A N
562
    if (ir2 == EXsAF_AFp & exec_ir2) ar <= ap;
563
    if (ir2 == EXX & exec_ir2)       ar <= ap;
564
    if (ir2 == DAA & exec_ir2)       ar <= daa_alu;
565
    if (ir2 == CPL & exec_ir2)       ar <= ~ar;
566
    if (ir2 == POPsAF & exec_ir2)    ar <= nn[15:8];
567
    if (ir2[2:0] == REG8_A &
568
             bit_alu_act & exec_ir2) ar <= bit_alu;
569
    if (ir2[2:0] == REG8_A &
570
             sh_alu_act & exec_ir2)  ar <= sh_alu;
571
    if (ir2 == ED_RRD & exec_ir2) ar[3:0] <= nn[11:8];
572
    if (ir2 == ED_RLD & exec_ir2) ar[3:0] <= nn[15:12];
573
    if ({ir2[9:6], ir2[2:0]} == ED_NEG & exec_ir2) ar <= alu8_out;  // ED44 this done by alu8 for flags
574
    if (ir2 == ED_LDsA_I & exec_ir2) ar <= intr ;
575
end
576
 
577
 
578
 
579
 
580
// update br
581
//assign upd_b_decbc = 
582
//    ED_LDI       == ir2 | //      LDI        ; ED A0
583
//    ED_CPI       == ir2 | //      CPI        ; ED A1
584
//    ED_LDD       == ir2 | //      LDD        ; ED A8
585
//    ED_CPD       == ir2 | //      CPD        ; ED A9
586
//    ED_LDIR      == ir2 | //      LDIR       ; ED B0
587
//    ED_CPIR      == ir2 | //      CPIR       ; ED B1
588
//    ED_LDDR      == ir2 | //      LDDR       ; ED B8
589
//    ED_CPDR      == ir2  ;//      CPDR       ; ED B9
590
 
591
//assign eb_io = 
592
 
593
//    ED_INI       == ir2 | //      INI        ; ED A2
594
//    ED_IND       == ir2 | //      IND        ; ED AA
595
//    ED_OUTD      == ir2 | //      OUTD       ; ED AB
596
//    ED_OUTI      == ir2 | //      OUTI       ; ED A3
597
//    ED_INIR      == ir2 | //      INIR       ; ED B2
598
//    ED_OTIR      == ir2 | //      OTIR       ; ED B3
599
//    ED_INDR      == ir2 | //      INDR       ; ED BA
600
//    ED_OTDR      == ir2  ; //      OTDR       ; ED BB
601
 
602
assign upd_b_alu8 =
603
    INCsB         == ir2 |//      INC B       ; 04
604
    DECsB         == ir2 ;//      DEC B       ; 05
605
 
606
 
607
assign up_b_src_pqr =
608
    LDsB_B       == ir2 |//      LD B,B       ; 40
609
    LDsB_C       == ir2 |//      LD B,C       ; 41
610
    LDsB_D       == ir2 |//      LD B,D       ; 42
611
    LDsB_E       == ir2 |//      LD B,E       ; 43
612
    LDsB_H       == ir2 |//      LD B,H       ; 44
613
    LDsB_L       == ir2 |//      LD B,L       ; 45
614
    LDsB_6HL7    == ir2 |//      LD B,(HL)    ; 46
615
    LDsB_A       == ir2 ;//      LD B,A       ; 47
616
assign up_b_add16 =
617
    INCsBC       == ir2 |//      INC BC      ; 03
618
    DECsBC       == ir2 ;//      DEC BC      ; 0B
619
//LDsBC_nn     = 10'h01,//      LD BC,NN    ; 01 XX XX
620
//POPsBC       = 10'hC1,//      POP BC       ; C1
621
//EXX          = 10'hD9,//      EXX          ; D9
622
//LDsB_N       = 10'h06,//      LD B,N      ; 06 XX
623
//DJNZs$t2     = 10'h10,//      DJNZ $+2     ; 10 XX   //pre dec br
624
//ED_RRD       =  'h67//      RRD        ; ED 67   nibble roates A HL
625
//ED_RLD       =  'h6F//      RLD        ; ED 6F   nibble roates A HL
626
//ED_INsREG_6C7  =    5'b01___000,// compair with {ir2[7:6],ir2[2:0]} really (BCio)
627
 
628
//------------------------------- br -----------------------------------------
629
 
630
assign upd_bc_cpi = ed_blk_cp & exec_ir2;
631
 
632
assign upd_br = upd_b_alu8 | up_b_src_pqr | up_b_add16 | LDsBC_NN  == ir2 |
633
                POPsBC    == ir2 | EXX       == ir2 | LDsB_N    == ir2    |
634
                ir2[2:0] == REG8_B & bit_alu_act | ir2[2:0] == REG8_B & sh_alu_act |
635
                DJNZs$t2  == ir2 | (ED_INsREG_6C7 == {ir2[9:6],ir2[2:0]}) & (ir2[5:3] == REG8_B) |
636
                (ED_LDsREG_6NN7 == {ir2[9:6],ir2[3:0]}) & (ir2[5:4] == DBL_REG_BC);
637
 
638
 
639
always @(posedge clk)
640
begin
641
    if ( upd_b_alu8 & exec_ir2)        br <= alu8_out;
642
    if ( up_b_src_pqr & exec_ir2)      br <= src_pqr20;
643
    if ( up_b_add16   & exec_ir2)      br <= add16[15:8];
644
    if ( LDsBC_NN  == ir2 & exec_ir2)  br <= nn[15:8];
645
    if ( POPsBC    == ir2 & exec_ir2)  br <= nn[15:8];
646
    if ( EXX       == ir2  & exec_ir2) br <= bp;
647
    if ( LDsB_N    == ir2  & exec_ir2) br <= nn[15:8];
648
    if (ir2[2:0] == REG8_B &
649
             bit_alu_act & exec_ir2)   br <= bit_alu;
650
    if (ir2[2:0] == REG8_B &
651
             sh_alu_act & exec_ir2)    br <= sh_alu;
652
    if ( DJNZs$t2  == ir2  & exec_ir2) br <= br + 8'hff; // use seperate adder here as no flags  
653
                                                        // change  -- we need br==0.  for now 
654
                                                        // use |br.   If we need more speed add
655
                                                        // a ff.
656
    if (exec_decb | exec_decbc |upd_bc_cpi)        br <= decb_alu;
657
    if ( (ED_INsREG_6C7 == {ir2[9:6],ir2[2:0]}) & (ir2[5:3] == REG8_B) & exec_ir2 ) br <= nn[15:8];
658
    if ( (ED_LDsREG_6NN7 == {ir2[9:6],ir2[3:0]}) & (ir2[5:4] == DBL_REG_BC) & exec_ir2 ) br <= nn[15:8];
659
end
660
 
661
 
662
//  update cr
663
assign up_c_add16 =
664
    INCsBC       == ir2 |//      INC BC      ; 03
665
    DECsBC       == ir2 ;//      DEC BC      ; 0B,
666
assign upd_c_alu8 =
667
    INCsC        == ir2 |//      INC C       ; 0C
668
    DECsC        == ir2 ;//      DEC C       ; 0D
669
assign up_c_src_pqr =
670
    LDsC_B       == ir2 |//      LD C,B       ; 48
671
    LDsC_C       == ir2 |//      LD C,C       ; 49
672
    LDsC_D       == ir2 |//      LD C,D       ; 4A
673
    LDsC_E       == ir2 |//      LD C,E       ; 4B
674
    LDsC_H       == ir2 |//      LD C,H       ; 4C
675
    LDsC_L       == ir2 |//      LD C,L       ; 4D
676
    LDsC_6HL7    == ir2 |//      LD C,(HL)    ; 4E
677
    LDsC_A       == ir2 ;//      LD C,A       ; 4F
678
 
679
 
680
//LDsC_N       == ir2 |//      LD C,N      ; 0E XX
681
//LDsBC_NN     = 10'h01,//      LD BC,NN    ; 01 XX XX
682
//POPsBC       = 10'hC1,//      POP BC       ; C1
683
//EXX          = 10'hD9,//      EXX          ; D9
684
//ED_INsREG_6C7  =    5'b01___000,// compair with {ir2[9:6],ir2[2:0]} really (BCio)
685
 
686
//------------------------------- cr -----------------------------------------
687
assign upd_cr = upd_c_alu8 | up_c_src_pqr | up_c_add16 | LDsBC_NN  == ir2 |
688
                POPsBC    == ir2 | EXX       == ir2 | LDsC_N    == ir2    |
689
                ir2[2:0] == REG8_C & bit_alu_act | ir2[2:0] == REG8_C & sh_alu_act |
690
                (ED_INsREG_6C7 == {ir2[9:6],ir2[2:0]}) & (ir2[5:3] == REG8_C)      |
691
                (ED_LDsREG_6NN7 == {ir2[9:6],ir2[3:0]}) & (ir2[5:4] == DBL_REG_BC);
692
 
693
 
694
 
695
always @(posedge clk)
696
begin
697
    if ( upd_c_alu8 & exec_ir2)        cr <= alu8_out;
698
    if ( up_c_src_pqr & exec_ir2)      cr <= src_pqr20;
699
    if ( up_c_add16   & exec_ir2)      cr <= add16[7:0];
700
    if ( LDsBC_NN  == ir2 & exec_ir2)  cr <= nn[7:0];
701
    if ( POPsBC    == ir2 & exec_ir2)  cr <= nn[7:0];
702
    if ( EXX       == ir2  & exec_ir2) cr <= cp;
703
    if ( LDsC_N    == ir2  & exec_ir2) cr <= nn[15:8];
704
    if (ir2[2:0] == REG8_C &
705
             bit_alu_act & exec_ir2)   cr <= bit_alu;
706
    if (ir2[2:0] == REG8_C &
707
             sh_alu_act & exec_ir2)    cr <= sh_alu;
708
    if ( exec_decbc |upd_bc_cpi)       cr <= decc_alu;
709
    if ((ED_INsREG_6C7 == {ir2[9:6],ir2[2:0]}) & (ir2[5:3] == REG8_C) & exec_ir2)
710
                                           cr <= nn[15:8];
711
    if ( (ED_LDsREG_6NN7 == {ir2[9:6],ir2[3:0]}) & (ir2[5:4] == DBL_REG_BC) & exec_ir2 ) cr <= nn[7:0];
712
 
713
end
714
 
715
 
716
//  update dr
717
assign up_d_add16 =
718
    INCsDE       == ir2 |  //      INC DE       ; 13
719
    DECsDE       == ir2  ; //      DEC DE       ; 1B
720
 
721
assign upd_d_alu8 =
722
    INCsD        == ir2 |  //      INC D        ; 14
723
    DECsD        == ir2  ; //      DEC D        ; 15
724
assign up_d_src_pqr =
725
    LDsD_B       == ir2 |      //LD D,B       ; 50
726
    LDsD_C       == ir2 |      //LD D,C       ; 51
727
    LDsD_D       == ir2 |      //LD D,D       ; 52
728
    LDsD_E       == ir2 |      //LD D,E       ; 53
729
    LDsD_H       == ir2 |      //LD D,H       ; 54
730
    LDsD_L       == ir2 |      //LD D,L       ; 55             
731
    LDsD_6HL7    == ir2 |      //LD D,(HL)    ; 56endmodule
732
    LDsD_A       == ir2 ;      //LD D,A       ; 57
733
 
734
 
735
//LDsD_N       = 10'h16,//      LD D,N       ; 16 XX
736
//LDsDE_NN     = 10'h11,//      LD DE,NN     ; 11 XX XX
737
//POPsDE       = 10'hD1,//      POP DE       ; D1
738
//EXX          = 10'hD9,//      EXX          ; D9
739
//EXsDE_HL     = 10'hEB,//      EX DE,HL     ; EB
740
//ED_INsREG_6C7  =    5'b01___000,// compair with {ir2[9:6],ir2[2:0]} really (BCio)
741
 
742
//---------------------------------- dr ------------------------------------
743
 
744
assign upd_dr = upd_d_alu8 | up_d_src_pqr | up_d_add16 | LDsDE_NN  == ir2 |
745
                POPsDE    == ir2 | EXX       == ir2 | EXsDE_HL == ir2 | LDsD_N    == ir2 |
746
                ir2[2:0] == REG8_D & bit_alu_act | ir2[2:0] == REG8_D & sh_alu_act |
747
                 (ED_INsREG_6C7 == {ir2[9:6],ir2[2:0]}) & (ir2[5:3] == REG8_D)     |
748
                 (ED_LDsREG_6NN7 == {ir2[9:6],ir2[3:0]}) & (ir2[5:4] == DBL_REG_DE);
749
 
750
 
751
 
752
 
753
 
754
wire ed_ld_dereg = (ED_LDsREG_6NN7 == {ir2[9:6],ir2[3:0]}) & (ir2[5:4] == DBL_REG_DE);
755
always @(posedge clk)
756
begin
757
    if ( upd_d_alu8 & exec_ir2)        dr <= alu8_out;
758
    if ( up_d_src_pqr & exec_ir2)      dr <= src_pqr20;
759
    if ( up_d_add16   & exec_ir2)      dr <= add16[15:8];
760
    if ( LDsDE_NN  == ir2 & exec_ir2)  dr <= nn[15:8];
761
    if ( POPsDE    == ir2 & exec_ir2)  dr <= nn[15:8];
762
    if ( EXX       == ir2  & exec_ir2) dr <= dp;
763
    if ( EXsDE_HL  == ir2  & exec_ir2) dr <= hr;
764
    if ( LDsD_N    == ir2  & exec_ir2) dr <= nn[15:8];
765
    if (ir2[2:0] == REG8_D &
766
             bit_alu_act & exec_ir2)   dr <= bit_alu;
767
    if (ir2[2:0] == REG8_D &
768
             sh_alu_act & exec_ir2)    dr <= sh_alu;
769
    if ((ED_INsREG_6C7 == {ir2[9:6],ir2[2:0]})
770
         & (ir2[5:3] == REG8_D) & exec_ir2)
771
                                        dr <= nn[15:8];
772
    if ( ed_ld_dereg & exec_ir2 )
773
                                        dr <= nn[15:8];
774
    if (blk_mv_upd_de)                  dr <= adr_alu[15:8];
775
end
776
 
777
//  update er
778
assign up_e_add16 =
779
    INCsDE       == ir2 |//      INC DE       ; 13
780
    DECsDE       == ir2 ;//      DEC DE       ; 1B
781
assign upd_e_alu8 =
782
    INCsE        == ir2 |//      INC E        ; 1C
783
    DECsE        == ir2 ;//      DEC E        ; 1D
784
assign up_e_src_pqr =
785
    LDsE_B       == ir2 |//      LD E,B       ; 58
786
    LDsE_C       == ir2 |//      LD E,C       ; 59
787
    LDsE_D       == ir2 |//      LD E,D       ; 5A
788
    LDsE_E       == ir2 |//      LD E,E       ; 5B
789
    LDsE_H       == ir2 |//      LD E,H       ; 5C
790
    LDsE_L       == ir2 |//      LD E,L       ; 5D
791
    LDsE_6HL7    == ir2 |//      LD E,(HL)    ; 5E
792
    LDsE_A       == ir2 ;//      LD E,A       ; 5F
793
 
794
//LDsE_N       = 10'h1E,//      LD E,N       ; 1E XX
795
//LDsDE_NN     = 10'h11,//      LD DE,NN     ; 11 XX XX
796
//POPsDE       = 10'hD1,//      POP DE       ; D1
797
//EXX          = 10'hD9,//      EXX          ; D9
798
//EXsDE_HL     = 10'hEB,//      EX DE,HL     ; EB
799
//ED_INsREG_6C7  =    5'b01___000,// compair with {ir2[9:6],ir2[2:0]} really (BCio)
800
 
801
//---------------------------------- er ------------------------------------
802
 
803
 
804
assign upd_er = upd_e_alu8 | up_e_src_pqr | up_e_add16 | LDsDE_NN  == ir2 |
805
                POPsDE    == ir2 | EXX       == ir2 | EXsDE_HL == ir2 | LDsE_N    == ir2 |
806
                ir2[2:0] == REG8_E & bit_alu_act | ir2[2:0] == REG8_E & sh_alu_act |
807
                 (ED_INsREG_6C7 == {ir2[9:6],ir2[2:0]}) & (ir2[5:3] == REG8_E)     |
808
                 (ED_LDsREG_6NN7 == {ir2[9:6],ir2[3:0]}) & (ir2[5:4] == DBL_REG_DE);
809
 
810
always @(posedge clk)
811
begin
812
    if ( upd_e_alu8 & exec_ir2)        er <= alu8_out;
813
    if ( up_e_src_pqr & exec_ir2)      er <= src_pqr20;
814
    if ( up_e_add16   & exec_ir2)      er <= add16[7:0];
815
    if ( LDsDE_NN  == ir2 & exec_ir2)  er <= nn[7:0];
816
    if ( POPsDE    == ir2 & exec_ir2)  er <= nn[7:0];
817
    if ( EXX       == ir2  & exec_ir2) er <= ep;
818
    if ( EXsDE_HL  == ir2  & exec_ir2) er <= lr; // hharte was er <= hr
819
    if ( LDsE_N    == ir2  & exec_ir2) er <= nn[15:8];
820
    if (ir2[2:0] == REG8_E &
821
             bit_alu_act & exec_ir2)   er <= bit_alu;
822
    if (ir2[2:0] == REG8_E &
823
             sh_alu_act & exec_ir2)    er <= sh_alu;
824
    if ((ED_INsREG_6C7 == {ir2[9:6],ir2[2:0]}) & (ir2[5:3] == REG8_E) & exec_ir2)
825
                                           er <= nn[15:8];
826
    if ( ed_ld_dereg & exec_ir2 )
827
                                        er <= nn[7:0];
828
    if (blk_mv_upd_de)                  er <= adr_alu[7:0];
829
 
830
end
831
 
832
 
833
//  update hr
834
assign up_h_add16 =
835
    ADDsHL_BC    == ir2 | //      ADD HL,BC   ; 09
836
    ADDsHL_DE    == ir2 | //      ADD HL,DE    ; 19
837
    ADDsHL_HL    == ir2 | //      ADD HL,HL    ; 29
838
    ADDsHL_SP    == ir2 | //      ADD HL,SP    ; 39
839
    ED_SBCsHL_REG  == {ir2[9:6],ir2[3:0]}  |  // compair with {ir[9:6],ir[3:0]}
840
    ED_ADCsHL_REG  == {ir2[9:6],ir2[3:0]}  |  // compair with {ir[9:6],ir[3:0]}
841
 
842
    INCsHL       == ir2 | //      INC HL       ; 23
843
    DECsHL       == ir2 ; //      DEC HL       ; 2B
844
assign upd_h_alu8 =
845
    INCsH        == ir2 | //      INC H        ; 24
846
    DECsH        == ir2 ; //      DEC H        ; 25
847
assign upd_h_src_pqr =
848
    LDsH_B       == ir2 | //      LD H,B       ; 60
849
    LDsH_C       == ir2 | //      LD H,C       ; 61
850
    LDsH_D       == ir2 | //      LD H,D       ; 62
851
    LDsH_E       == ir2 | //      LD H,E       ; 63
852
    LDsH_H       == ir2 | //      LD H,H       ; 64
853
    LDsH_L       == ir2 | //      LD H,L       ; 65
854
    LDsH_6HL7    == ir2 | //      LD H,(HL)    ; 66
855
    LDsH_A       == ir2 ; //      LD H,A       ; 67
856
//ED_INsREG_6C7  =    5'b01___000,// compair with {ir2[9:6],ir2[2:0]} really (BCio)
857
 
858
//POPsHL       = 10'hE1,//      POP HL       ; E1
859
//EXs6SP7_HL   = 10'hE3,//      EX (SP),HL   ; E3
860
//LDsHL_NN     = 10'h21,//      LD HL,NN     ; 21 XX XX
861
//LDsHL_6NN7   = 10'h2A,//      LD HL,(NN)   ; 2A XX XX
862
//LDsH_N       = 10'h26,//      LD H,N       ; 26 XX
863
 
864
//  only these are not affected by dd and fd prefixes
865
//EXsDE_HL     = 10'hEB,//      EX DE,HL     ; EB
866
//EXX          = 10'hD9,//      EXX          ; D9
867
 
868
//---------------------------------- hr ------------------------------------
869
// we just check hr and lr - the prefixes for use of ix and iy imply that something
870
// pretty strange has to happen for a hazard related to use of those registers.  We can 
871
// assume upd hr impies upd ix and iy without adverse timing consequences.
872
// 
873
assign upd_hr = upd_h_alu8 | upd_h_src_pqr | up_h_add16 | LDsHL_NN  == ir2 | LDsHL_6NN7== ir2 |
874
                POPsHL    == ir2 | EXX       == ir2 | EXsDE_HL == ir2 | LDsH_N    == ir2 |
875
                ir2[2:0] == REG8_H & bit_alu_act | ir2[2:0] == REG8_H & sh_alu_act |
876
                 (ED_INsREG_6C7 == {ir2[9:6],ir2[2:0]}) & (ir2[5:3] == REG8_H)     |
877
                 (ED_LDsREG_6NN7 == {ir2[9:6],ir2[3:0]}) & (ir2[5:4] == DBL_REG_HL);
878
 
879
 
880
 
881
 
882
wire exec_hlir2 = exec_ir2 & !(ir2dd | ir2fd);
883
 
884
always @(posedge clk)
885
begin
886
    if ( upd_h_alu8 & exec_hlir2)        hr <= alu8_out;
887
    if ( upd_h_src_pqr & exec_hlir2)      hr <= src_pqr20;
888
    if ( up_h_add16   & exec_hlir2)      hr <= add16[15:8];
889
    if ( LDsHL_NN  == ir2 & exec_hlir2)  hr <= nn[15:8];
890
    if ( LDsHL_6NN7== ir2 & exec_hlir2)  hr <= nn[15:8];
891
    if ( POPsHL    == ir2 & exec_hlir2)  hr <= nn[15:8];
892
    if ( EXs6SP7_HL== ir2 & exec_hlir2)  hr <= nn[15:8];
893
    if ( EXX       == ir2  & exec_ir2)   hr <= hp;
894
    if ( EXsDE_HL  == ir2  & exec_ir2)   hr <= dr;
895
    if ( LDsH_N    == ir2  & exec_hlir2) hr <= nn[15:8];
896
    if (ir2[2:0] == REG8_H &
897
             bit_alu_act & exec_hlir2)   hr <= bit_alu;
898
    if (ir2[2:0] == REG8_H &
899
             sh_alu_act & exec_hlir2)    hr <= sh_alu;
900
    if ((ED_INsREG_6C7 == {ir2[9:6],ir2[2:0]}) & (ir2[5:3] == REG8_H) & exec_ir2)
901
                                           hr <= nn[15:8];
902
    if ( (ED_LDsREG_6NN7 == {ir2[9:6],ir2[3:0]}) & (ir2[5:4] == DBL_REG_HL) & exec_ir2 )
903
                                         hr <= nn[15:8];
904
    if (blk_mv_upd_hl)                   hr <= adr_alu[15:8];
905
 
906
end
907
 
908
//  update lr
909
assign up_l_add16 =
910
    ADDsHL_BC    == ir2 |//      ADD HL,BC   ; 09
911
    ADDsHL_DE    == ir2 |//      ADD HL,DE    ; 19
912
    ADDsHL_HL    == ir2 |//      ADD HL,HL    ; 29
913
    ADDsHL_SP    == ir2 |//      ADD HL,SP    ; 39
914
    ED_SBCsHL_REG  == {ir2[9:6],ir2[3:0]}  |  // compair with {ir[9:6],ir[3:0]}
915
    ED_ADCsHL_REG  == {ir2[9:6],ir2[3:0]}  |  // compair with {ir[9:6],ir[3:0]}
916
    INCsHL       == ir2 |//      INC HL       ; 23
917
    DECsHL       == ir2 ;//      DEC HL       ; 2B
918
assign upd_l_alu8 =
919
    INCsL        == ir2 |//      INC L        ; 2C
920
    DECsL        == ir2 ;//      DEC L        ; 2D
921
assign upd_l_src_pqr =
922
    LDsL_B       == ir2 |//      LD L,B       ; 68
923
    LDsL_C       == ir2 |//      LD L,C       ; 69
924
    LDsL_D       == ir2 |//      LD L,D       ; 6A
925
    LDsL_E       == ir2 |//      LD L,E       ; 6B
926
    LDsL_H       == ir2 |//      LD L,H       ; 6C
927
    LDsL_L       == ir2 |//      LD L,L       ; 6D
928
    LDsL_6HL7    == ir2 |//      LD L,(HL)    ; 6E
929
    LDsL_A       == ir2 ;//      LD L,A       ; 6F
930
//EXX          = 10'hD9,//      EXX          ; D9
931
//POPsHL       = 10'hE1,//      POP HL       ; E1
932
//EXs6SP7_HL   = 10'hE3,//      EX (SP),HL   ; E3
933
//EXsDE_HL     = 10'hEB,//      EX DE,HL     ; EB
934
//LDsHL_NN     = 10'h21,//      LD HL,NN     ; 21 XX XX
935
//LDsHL_6NN7   = 10'h2A,//      LD HL,(NN)   ; 2A XX XX
936
//LDsL_N       = 10'h2E,//      LD L,N       ; 2E XX
937
//ED_INsREG_6C7
938
 
939
 
940
 
941
//---------------------------------- lr ------------------------------------
942
assign upd_lr = upd_l_alu8 | upd_l_src_pqr | up_l_add16 | LDsHL_NN  == ir2 | LDsHL_6NN7== ir2 |
943
                POPsHL    == ir2 | EXX       == ir2 | EXsDE_HL == ir2 | LDsL_N    == ir2 |
944
                ir2[2:0] == REG8_L & bit_alu_act | ir2[2:0] == REG8_L & sh_alu_act |
945
                 (ED_INsREG_6C7 == {ir2[9:6],ir2[2:0]}) & (ir2[5:3] == REG8_L)     |
946
                 (ED_LDsREG_6NN7 == {ir2[9:6],ir2[3:0]}) & (ir2[5:4] == DBL_REG_HL);
947
 
948
 
949
 
950
always @(posedge clk)
951
begin
952
    if ( upd_l_alu8 & exec_hlir2)        lr <= alu8_out;
953
    if ( upd_l_src_pqr & exec_hlir2)      lr <= src_pqr20;
954
    if ( up_l_add16   & exec_hlir2)      lr <= add16[7:0];
955
    if ( LDsHL_NN  == ir2 & exec_hlir2)  lr <= nn[7:0];
956
    if ( LDsHL_6NN7== ir2 & exec_hlir2)  lr <= nn[7:0];
957
    if ( POPsHL    == ir2 & exec_hlir2)  lr <= nn[7:0];
958
    if ( EXs6SP7_HL== ir2 & exec_hlir2)  lr <= nn[7:0];
959
    if ( EXX       == ir2  & exec_ir2)   lr <= lp;
960
    if ( EXsDE_HL  == ir2  & exec_ir2)   lr <= er;
961
    if ( LDsL_N    == ir2  & exec_hlir2) lr <= nn[15:8];
962
    if (ir2[2:0] == REG8_L &
963
             bit_alu_act & exec_hlir2)   lr <= bit_alu;
964
    if (ir2[2:0] == REG8_L &
965
             sh_alu_act & exec_hlir2)    lr <= sh_alu;
966
    if ((ED_INsREG_6C7 == {ir2[9:6],ir2[2:0]}) & (ir2[5:3] == REG8_L) & exec_ir2)
967
                                         lr <= nn[15:8];
968
    if ( (ED_LDsREG_6NN7 == {ir2[9:6],ir2[3:0]}) & (ir2[5:4] == DBL_REG_HL) & exec_ir2 )
969
                                         lr <= nn[7:0];
970
    if (blk_mv_upd_hl)                   lr <= adr_alu[7:0];
971
 
972
end
973
//------------------------ ixr ---------------------------------------------
974
wire exec_ixir2 = exec_ir2 & ir2dd;
975
always @(posedge clk)
976
begin
977
    if ( upd_l_alu8 & exec_ixir2)        ixr[7:0] <= alu8_out;
978
    if ( upd_l_src_pqr & exec_ixir2)      ixr[7:0] <= src_pqr20;
979
    if ( up_l_add16   & exec_ixir2)      ixr[7:0] <= add16[7:0];
980
    if ( LDsHL_NN  == ir2 & exec_ixir2)  ixr[7:0] <= nn[7:0];
981
    if ( LDsHL_6NN7== ir2 & exec_ixir2)  ixr[7:0] <= nn[7:0];
982
    if ( POPsHL    == ir2 & exec_ixir2)  ixr[7:0] <= nn[7:0];
983
    if ( EXs6SP7_HL== ir2 & exec_ixir2)  ixr[7:0] <= nn[7:0];
984
 
985
    if ( LDsL_N    == ir2  & exec_ixir2) ixr[7:0] <= nn[15:8];
986
    if (ir2[2:0] == REG8_L &
987
             bit_alu_act & exec_ixir2)   ixr[7:0] <= bit_alu;
988
    if (ir2[2:0] == REG8_L &
989
             sh_alu_act & exec_ixir2)    ixr[7:0] <= sh_alu;
990
 
991
end
992
 
993
always @(posedge clk)
994
begin
995
    if ( upd_h_alu8 & exec_ixir2)        ixr[15:8] <= alu8_out;
996
    if ( upd_h_src_pqr & exec_ixir2)      ixr[15:8] <= src_pqr20;
997
    if ( up_h_add16   & exec_ixir2)      ixr[15:8] <= add16[15:8];
998
    if ( LDsHL_NN  == ir2 & exec_ixir2)  ixr[15:8] <= nn[15:8];
999
    if ( LDsHL_6NN7== ir2 & exec_ixir2)  ixr[15:8] <= nn[15:8];
1000
    if ( POPsHL    == ir2 & exec_ixir2)  ixr[15:8] <= nn[15:8];
1001
    if ( EXs6SP7_HL== ir2 & exec_ixir2)  ixr[15:8] <= nn[15:8];
1002
 
1003
    if ( LDsH_N    == ir2  & exec_ixir2) ixr[15:8] <= nn[15:8];
1004
    if (ir2[2:0] == REG8_H &
1005
             bit_alu_act & exec_ixir2)   ixr[15:8] <= bit_alu;
1006
    if (ir2[2:0] == REG8_H &
1007
             sh_alu_act & exec_ixir2)    ixr[15:8] <= sh_alu;
1008
 
1009
end
1010
 
1011
//------------------------ iyr ---------------------------------------------
1012
wire exec_iyir2 = exec_ir2 & ir2fd;
1013
always @(posedge clk)
1014
begin
1015
    if ( upd_l_alu8 & exec_iyir2)        iyr[7:0] <= alu8_out;
1016
    if ( upd_l_src_pqr & exec_iyir2)      iyr[7:0] <= src_pqr20;
1017
    if ( up_l_add16   & exec_iyir2)      iyr[7:0] <= add16[7:0];
1018
    if ( LDsHL_NN  == ir2 & exec_iyir2)  iyr[7:0] <= nn[7:0];
1019
    if ( LDsHL_6NN7== ir2 & exec_iyir2)  iyr[7:0] <= nn[7:0];
1020
    if ( POPsHL    == ir2 & exec_iyir2)  iyr[7:0] <= nn[7:0];
1021
    if ( EXs6SP7_HL== ir2 & exec_iyir2)  iyr[7:0] <= nn[7:0];
1022
 
1023
    if ( LDsL_N    == ir2  & exec_iyir2) iyr[7:0] <= nn[15:8];
1024
    if (ir2[2:0] == REG8_L &
1025
             bit_alu_act & exec_iyir2)   iyr[7:0] <= bit_alu;
1026
    if (ir2[2:0] == REG8_L &
1027
             sh_alu_act & exec_iyir2)    iyr[7:0] <= sh_alu;
1028
 
1029
end
1030
 
1031
always @(posedge clk)
1032
begin
1033
    if ( upd_h_alu8 & exec_iyir2)        iyr[15:8] <= alu8_out;
1034
    if ( upd_h_src_pqr & exec_iyir2)      iyr[15:8] <= src_pqr20;
1035
    if ( up_h_add16   & exec_iyir2)      iyr[15:8] <= add16[15:8];
1036
    if ( LDsHL_NN  == ir2 & exec_iyir2)  iyr[15:8] <= nn[15:8];
1037
    if ( LDsHL_6NN7== ir2 & exec_iyir2)  iyr[15:8] <= nn[15:8];
1038
    if ( POPsHL    == ir2 & exec_iyir2)  iyr[15:8] <= nn[15:8];
1039
    if ( EXs6SP7_HL== ir2 & exec_iyir2)  iyr[15:8] <= nn[15:8];
1040
 
1041
    if ( LDsH_N    == ir2  & exec_iyir2) iyr[15:8] <= nn[15:8];
1042
    if (ir2[2:0] == REG8_H &
1043
             bit_alu_act & exec_iyir2)   iyr[15:8] <= bit_alu;
1044
    if (ir2[2:0] == REG8_H &
1045
             sh_alu_act & exec_iyir2)    iyr[15:8] <= sh_alu;
1046
 
1047
end
1048
 
1049
 
1050
//---------------------------- prime regiters  (shadows?) ----------------
1051
 
1052
always @(posedge clk)
1053
begin
1054
    if (ir2 == EXsAF_AFp & exec_ir2)
1055
    begin
1056
        ap <= ar;
1057
        fp <= fr;
1058
    end
1059
    if (ir2 == EXX & exec_ir2)
1060
    begin
1061
        ap <= ar;
1062
        fp <= fr;
1063
        bp <= br;
1064
        cp <= cr;
1065
        dp <= dr;
1066
        ep <= er;
1067
        hp <= hr;
1068
        lp <= lr;
1069
    end
1070
end
1071
//-------------------------- flag registers -------------------------------
1072
//  This is a mess  -  There is in general no reasonable way to get this stuff to follow
1073
//  z80 exactly. ---   in some of the undocumented cases, there is not even a 
1074
//  guess expressed about what is actually done.   In some of the other undocumented
1075
//  cases, what is claimed happens is soo silly that It is hard for me to believe
1076
//  it matters   ( unfortunately i am far too aware that one man's garbage can be 
1077
//  anothers treasure  --- or....., its amazing how silly 
1078
//  behavior (bug?) can become a feature.  In any case, The attempt (at first blush) is 
1079
//  only to get the documented stuff right  --  although if undocumented behavior
1080
//  falls out, great. For exmple, I will typically update f3f and f5f with alu output - 
1081
//  these flags are documented as "undefined".  
1082
//
1083
//   some of the wierd stuff to worry about:
1084
//   16 bit ops:
1085
//   the ed insts SBC ADC muck with all flags  but 
1086
//   the ADD inst doesn't change sf zf or pvf.
1087
//   and the 16 bit INC and DEC insts touch nothing
1088
//  
1089
//   the ED_RLD and RRD instructions muck with flags based on ar  -- these operations
1090
//   should be correct rleative to subsequent DAA's i suppose.
1091
 
1092
//  update all flags from alu8   for logic operations pv <= parity else ofl
1093
//  INC and DEC same as  but no cf change  oh my god why?  done in logic above
1094
 
1095
assign upd_fr_alu8 =
1096
    ADCsA_A  == ir2 |   ANDsA    == ir2 |  ORsA     == ir2 | SUBsA    == ir2 | DECsA       == ir2 |
1097
    ADCsA_B  == ir2 |   ANDsB    == ir2 |  ORsB     == ir2 | SUBsB    == ir2 | DECsB       == ir2 |
1098
    ADCsA_C  == ir2 |   ANDsC    == ir2 |  ORsC     == ir2 | SUBsC    == ir2 | DECsC       == ir2 |
1099
    ADCsA_D  == ir2 |   ANDsD    == ir2 |  ORsD     == ir2 | SUBsD    == ir2 | DECsD       == ir2 |
1100
    ADCsA_E  == ir2 |   ANDsE    == ir2 |  ORsE     == ir2 | SUBsE    == ir2 | DECsE       == ir2 |
1101
    ADCsA_H  == ir2 |   ANDsH    == ir2 |  ORsH     == ir2 | SUBsH    == ir2 | DECsH       == ir2 |
1102
    ADCsA_L  == ir2 |   ANDsL    == ir2 |  ORsL     == ir2 | SUBsL    == ir2 | DECsL       == ir2 |
1103
    ADCsA_6HL7==ir2 |   ANDs6HL7  ==ir2 |  ORs6HL7  ==ir2  | SUBs6HL7  ==ir2 | INCsA       == ir2 |
1104
    ADDsA_A  == ir2 |   CPsA     == ir2 |  SBCsA    == ir2 | XORsA    == ir2 | INCsB       == ir2 |
1105
    ADDsA_B  == ir2 |   CPsB     == ir2 |  SBCs6HL7  ==ir2 | XORsB    == ir2 | INCsC       == ir2 |
1106
    ADDsA_C  == ir2 |   CPsC     == ir2 |  SBCsB    == ir2 | XORsC    == ir2 | INCsD       == ir2 |
1107
    ADDsA_D  == ir2 |   CPsD     == ir2 |  SBCsC    == ir2 | XORsD    == ir2 | INCsE       == ir2 |
1108
    ADDsA_E  == ir2 |   CPsE     == ir2 |  SBCsD    == ir2 | XORsE    == ir2 | INCsH       == ir2 |
1109
    ADDsA_H  == ir2 |   CPsH     == ir2 |  SBCsE    == ir2 | XORsH    == ir2 | INCsL       == ir2 |
1110
    ADDsA_L  == ir2 |   CPsL     == ir2 |  SBCsH    == ir2 | XORsL    == ir2 | INCs6HL7    == ir2 |
1111
    ADDsA_6HL7== ir2|   CPs6HL7  == ir2 |  SBCsL    == ir2 | XORs6HL7 == ir2 | DECs6HL7    == ir2 |
1112
    ADDsA_N   == ir2|   SUBsN    == ir2 | ANDsN     == ir2 |  ORsN    == ir2 |
1113
    ADCsA_N   ==ir2 |   SBCsA_N  ==ir2  | XORsN     ==ir2  |  CPsN    == ir2 |
1114
    ED_NEG   ==  {ir2[9:6],ir2[2:0]} ; //7'b1001___100,   A<= -A                  
1115
 
1116
 
1117
 
1118
// update h n c (f5, f3) from alu16
1119
assign upd_fr_add16 =
1120
    ADDsHL_BC    == ir2 |      //      ADD HL,BC    ; 09
1121
    ADDsHL_DE    == ir2 |      //      ADD HL,DE    ; 19
1122
    ADDsHL_HL    == ir2 |      //      ADD HL,HL    ; 29
1123
    ADDsHL_SP    == ir2 ;      //      ADD HL,SP    ; 39
1124
//    INCsBC       == ir2 |      //      INC BC       ; 03    no flag changes for these
1125
//    INCsDE       == ir2 |      //      INC DE       ; 13
1126
//    INCsHL       == ir2 |      //      INC HL       ; 23
1127
//    INCsSP       == ir2 ;      //      INC SP       ; 33
1128
 
1129
// update all flags from alu16
1130
assign upd_fr_edadd16 =
1131
    ED_SBCsHL_REG  == {ir2[9:6],ir2[3:0]} | // compair with {ir2[9:6],ir2[3:0]}
1132
    ED_ADCsHL_REG  == {ir2[9:6],ir2[3:0]} ; // compair with {ir2[9:6],ir2[3:0]}
1133
 
1134
wire borrow = ED_SBCsHL_REG  == {ir2[9:6],ir2[3:0]};
1135
//  the shifts probably muck with all flags (some operations are 
1136
//  guarenteed not to change certain flags )
1137
//   docs say sf and zf  never change for these ops.
1138
assign upd_fr_sh =
1139
    RLA          == ir2 |//      RLA          ; 17
1140
    RLCA         == ir2 |//      RLCA         ; 07
1141
    RRA          == ir2 |//      RRA          ; 1F
1142
    RRCA         == ir2 ;//      RRCA         ; 0F
1143
// sf and zf do change for theses
1144
assign upd_fr_cbsh =
1145
    CB_RLC   == ir2[9:3] |  // these must be compaired with ir2[9:3]
1146
    CB_RRC   == ir2[9:3] |  // these must be compaired with ir2[9:3]
1147
    CB_RL    == ir2[9:3] |  // these must be compaired with ir2[9:3]
1148
    CB_RR    == ir2[9:3] |  // these must be compaired with ir2[9:3]
1149
    CB_SLA   == ir2[9:3] |  // these must be compaired with ir2[9:3]
1150
    CB_SRA   == ir2[9:3] |  // these must be compaired with ir2[9:3]
1151
    CB_SLL   == ir2[9:3] |  // these must be compaired with ir2[9:3]
1152
    CB_SRL   == ir2[9:3] ;  // these must be compaired with ir2[9:3]
1153
 
1154
//  pretty nomal stuff here
1155
//CB_BIT   = 4'b01_01,    // these must be compaired with ir2[9:6]
1156
//  which alu? --  done from alu8  
1157
//ED_NEG         =    5'b01___100, // compair with {ir2[9:6],ir2[2:0]} all A<= -A
1158
 
1159
// rmw 8 types    these handled by standard INC and DEC logic    done.
1160
//INCs6HL7     = 'h34,//      INC (HL)     ; 34
1161
//DECs6HL7     = 'h35,//      DEC (HL)     ; 35
1162
 
1163
//  ED Block Move messyness    upd_b_decbc  4/19/2004 not used  - probably not needed
1164
//  hf and nf <= 0   pnf<= BC==0
1165
//assign eb_blk_mv = 
1166
//    ED_LDI       == ir2 | //      LDI        ; ED A0   (DE++) <= (HL++) , BC-- 
1167
//    ED_LDD       == ir2 | //      LDD        ; ED A8   (DE--) <= (HL--) , BC--
1168
//    ED_LDIR      == ir2 | //      LDIR       ; ED B0   (DE++) <= (HL++) , BC-- Repeat til BC==0 
1169
//    ED_LDDR      == ir2  ;//      LDDR       ; ED B8   (DE--) <= (HL--) , BC-- Repeat til BC==0
1170
// only c not affected - nf<=1 ? 
1171
assign ed_blk_cp =
1172
    ED_CPI       == ir2 | //      CPI        ; ED A1    A - (HL++) , BC--
1173
    ED_CPD       == ir2 | //      CPD        ; ED A9    A - (HL--) , BC--
1174
    ED_CPIR      == ir2 | //      CPIR       ; ED B1    A - (HL++) , BC-- repeat if(|B
1175
    ED_CPDR      == ir2  ;//      CPDR       ; ED B9    A - (HL--) , BC-- repeat if(|B
1176
 
1177
//  all the ed i/o muck with all flags  -- wonderful  cf?
1178
//  use the aluoutput for the b-1 computation.  
1179
// --------- eb_io
1180
//ED_INI       =  'hA2//      INI        ; ED A2   (HL++) <- (Cio) , B--
1181
//ED_IND       =  'hAA//      IND        ; ED AA   (HL--) <- (Cio) , B--
1182
//ED_INIR      =  'hB2//      INIR       ; ED B2   (HL++) <- (Cio) , B-- repeat if(|B)
1183
//ED_INDR      =  'hBA//      INDR       ; ED BA   (HL--) <- (Cio) , B-- repeat if(|B)
1184
//ED_OUTI      =  'hA3//      OUTI       ; ED A3   (Cio)  <-(HL++) , B--
1185
//ED_OUTD      =  'hAB//      OUTD       ; ED AB   (Cio)  <-(HL--) , B--
1186
//ED_OTIR      =  'hB3//      OTIR       ; ED B3   (Cio)  <-(HL++) , B--  rpt if(|B)
1187
//ED_OTDR      =  'hBB//      OTDR       ; ED BB   (Cio)  <-(HL--) , B--  rpt if(|B)
1188
 
1189
//ED_INsREG_6C7  =    5'b01___000,// compair with {ir2[9:6],ir2[2:0]} really (BCio)
1190
 
1191
 
1192
 
1193
// special problems  --  lol   more special problems ????
1194
//CCF          = 'h3F,//      CCF          ; 3F  // h<=c  c<=~C N<=0  F3,F5?
1195
//CPL          = 'h2F,//      CPL          ; 2F  // H<=1  N<=1  F3,F5?
1196
//DAA          = 'h27,//      DAA          ; 27  // H<=0???  
1197
//SCF          = 'h37,//      SCF          ; 37
1198
//ED_RRD       =  'h67//      RRD        ; ED 67   nibble roates A HL
1199
//ED_RLD       =  'h6F//      RLD        ; ED 6F   nibble roates A HL
1200
//ED_LDsA_I    =  'h57//      LD A,I     ; ED 57   move I to A
1201
 
1202
assign { sf, zf, f5f, hf, f3f, pvf, nf,  cf} = fr;
1203
//   gotta say those little ~^ operators down there worry me.  Only 4 levels of xor - but jeeze
1204
//   there are a lot of them.  I guess in most FPGA's it doesn't matter what the op is  - just
1205
//   how many terms.   
1206
 
1207
 
1208
// do we need the exe_ir2 term here?   isn't it added in the hazard term anyway?
1209
assign upd_fr =  exec_ir2 & ( ( upd_fr_alu8 )                       |
1210
                              ( upd_fr_add16)                       |
1211
                              ( upd_fr_edadd16)                     |
1212
                              ( upd_fr_sh )                         |
1213
                              ( upd_fr_cbsh )                       |
1214
                              (CB_BIT == ir2[9:6])                  |
1215
                              ( ed_blk_cp )                         |
1216
                              (ED_INsREG_6C7 == {ir2[9:6],ir2[2:0]})  |
1217
                              (CCF == ir2 )                         |
1218
                              (CPL == ir2 )                         |
1219
                              (DAA == ir2 )                         |
1220
                              (SCF == ir2 )                         |
1221
                              (ED_RRD == ir2)                       |
1222
                              (ED_RLD == ir2)                       |
1223
                              (ED_LDsA_I == ir2)                    |
1224
                              (ir2 == EXsAF_AFp )                   |
1225
                              (ir2 == EXX )                         );
1226
 
1227
 
1228
 
1229
wire iff2 = 1'b0; // this is supposed to be int ff #2  which is not (yet) implmented
1230
wire upd_fr_ed_in =  ED_INsREG_6C7 == {ir2[9:6],ir2[2:0]} ;
1231
wire bc_eq1 = {br,cr} == 16'h1;
1232
always @(posedge clk)
1233
begin
1234
    if (exec_ir2)
1235
    begin
1236
        if ( upd_fr_alu8 )      fr <= alu8_fr;    //  assembled above with 8 bit ALU
1237
        if ( upd_fr_add16)      fr <= {sf, zf, add16[13], c_16out11, add16[11], pvf, 1'b0, c_16out15};
1238
        if ( upd_fr_edadd16)    fr <= {add16[15],   ~|add16, add16[13], c_16out11,
1239
                                       add16[11], add16_ofl,   ~ir2[3], borrow ^ c_16out15};
1240
        if ( upd_fr_sh )        fr <= {sf, zf, sh_alu[5], 1'b0, sh_alu[3], pvf, 1'b0, sh_cry};
1241
        if ( upd_fr_cbsh )      fr <= {sh_alu[7], ~|sh_alu, sh_alu[5], 1'b0,
1242
                                       sh_alu[3], ~^sh_alu,      1'b0, sh_cry};
1243
        if (CB_BIT == ir2[9:6]) fr <={bit_alu[7], ~|bit_alu, bit_alu[5], 1'b1, //no idea why hf<=1
1244
                                      bit_alu[3], ~|bit_alu, 1'b0      , cf  };// pvf == zf ??? 
1245
        if ( ed_blk_cp )        fr <= {alu8_out[7], ~|alu8_out, alu8_out[5], alu8_hcry,//std a-n stuff
1246
                                   alu8_out[3], ~bc_eq1,       1'b1,  cf };    //cept nf and cf
1247
        if (upd_fr_ed_in)
1248
                                fr <= {nn[15], ~|nn[15:8], nn[13], 1'b0, nn[11], ~^nn[15:8], 1'b0, cf};
1249
        if (CCF == ir2 )        fr <= {sf, zf, f5f, cf, f3f, pvf, nf, ~cf};
1250
        if (CPL == ir2 )        fr <= {sf, zf, ar[5], 1'b1, ar[3], pvf, 1'b1, cf};
1251
        if (DAA == ir2 )        fr <= {daa_alu[7], ~|daa_alu, daa_alu[5], 1'b0, // hf sb (logically) 0
1252
                                    daa_alu[3], ~^daa_alu,         nf, daa_cry };
1253
        if (SCF == ir2 )        fr <= { sf, zf, ar[5], 1'b0, ar[3], pvf, 1'b0, 1'b1 }; // very strange
1254
        if (ED_RRD == ir2)      fr <= {     ar[7], ~|{ar[7:4],nn[11:8]}, ar[5], 1'b0,
1255
                                     ar[3],  ~^{ar[7:4],nn[11:8]}, 1'b0 , cf    };
1256
        if (ED_RLD == ir2)      fr <= {     ar[7], ~|{ar[7:4],nn[15:12]}, ar[5], 1'b0,
1257
                                     ar[3],  ~^{ar[7:4],nn[15:12]}, 1'b0 , cf    };
1258
        if (ED_LDsA_I == ir2)   fr <= { intr[7], ~|intr, intr[5], 1'b0, intr[3], iff2, 1'b0, cf }; // iff2 ?
1259
        if (ir2 == EXsAF_AFp)   fr <= fp;
1260
        if (ir2 == EXX  )       fr <= fp;
1261
        if (ir2 == POPsAF)      fr <= nn[7:0];
1262
 
1263
 
1264
    end
1265
    // in the case of blk_cp the update above is executed 2nd - and so these are don't cares.
1266
    if (exec_decb )          fr <=  {decb_alu[7], ~|decb_alu, decb_alu[5], hf,
1267
                                     decb_alu[3],        pvf,        1'b0, cf };
1268
    if (exec_decbc )         fr[5:1] <= { decb_alu[5], 1'b0, decb_alu[3],
1269
                                           ((|decb_alu) | (|decc_alu)) , 1'b0 };
1270
end
1271
 
1272
 
1273
 //----------------------- intr -----------------------------------------------------------
1274
 
1275
always @(posedge clk or posedge rst)
1276
    if (rst) intr <= 8'h0;
1277
    else if (( ED_LDsI_A == ir2) & exec_ir2) intr <= ar;
1278
 
1279
endmodule

powered by: WebSVN 2.1.0

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