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

Subversion Repositories wb_z80

[/] [wb_z80/] [tags/] [arelease/] [rtl/] [memstate2.v] - Blame information for rev 39

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 bporcella
///////////////////////////////////////////////////////////////////////////////////////////////////
2
//                                                                                               //
3
//  file name:   memstate2.v                                                                       //
4
//  description: memory opertions for  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
// The memory state controller controls the wb bus, and provides address sequencing.
39
// Insructions are fetched in order (using PC) until the istate machine indicates that 
40
// a complete instruction is in the first pipline stage (ir1). In general, operands are being
41
// fetched (stored) to satisfy ir1 while concurrently instructions are being executed from ir2.
42
// this situation can result in a number of potential hazards.   As an example, if the ir2
43
// instruction changes the flag register and the ir1 instruction is a conditional jump, 
44
// a hazard is generated by the hazard logic, and execution of the ir1 operation is delayed 
45
// until the completion of the flag update.
46
//
47
// Reset starts execution at 0.  
48
// The PC and SP are described in this file.   modifications to other index registers - 
49
// HL IX and IY are computed here -- 
50
// For the block moves address updates are computed here   -- and commanded here.
51
// Strobes for the second address update are generally co-incident with count updates, but
52
// we provide seperate strobe update lines for clarity.
53
//
54
//  BASIC ARCHITECTURE OF THIS FILE   pc  and sp not shown, but are inputs to src mux.
55
//                    _____           and may be updated from adder output.
56
//                   |     |
57
//                   |     |          pc-1 register is required to implement relative jumps.
58
//                   |     |                     
59
//      _____        |lit  |      |\             
60
//     |     |       |     |      |  \           
61
//     |     |       |src2 |      |    \          _____          _____ 
62
//     |     |       |     |----->|     |        |     |        |     |
63
//     |src  |       |_____|      |adder|------->|     |        |     |
64
//     |mux  |                    |     |        |     |        |     |
65
//     |     |------------------->|    /         |2/1  |------->|wb   |
66
//     |     |              |     |  /           |mux  |        |adr  |
67
//     |_____|              |     |/             |     |        |     |
68
//                           ------------------->|     |        |     |
69
//                                               |_____|        |_____|
70
//
71
//
72
//
73
//
74
//  Operand Stores:
75
//  At first cut, I thought I'ld execute operand stores immediately from the memory sequencer
76
//  (essentially before ir2 got the store data).  While this might be modestly faster in 
77
//  systems that take multiple clocks to complete a memory store, On consideration, I decided 
78
//  to forgo the extra speed for conceptual simplicity....   execute operand stores on op_ph1,
79
//  and let the inst_exec engine suply the operand.
80
//
81
//  On second thought, above is not only wastful of time, but also inconsistent with the overall
82
//  schems of things - and so somewhat more complex. If we simply execute the OS from ir1, 
83
//  There is less state to contdend with, as well as extra speed.
84
//
85
//  Block Moves fundamentally execute from ir2.  We initiate the first operand fetch from ir1.
86
//
87
//  3/18/2004 Second time through.   In impleenting the execution logic it became clear that
88
//  there were "minor" problems with the handling of the DD and FD prefix insts (especially
89
//  DDCD and FDCB  ---  collectively called PFxCB below.  On review, I had to question the
90
//  value of "breaking up" the ir0 execution engine between the istate sequencer and the 
91
//  memstate sequencer.   While I dislike state sequencers of much more than 16 states  --  
92
//  the interaction between these sequencers was becomming harder to track than a single
93
//  state macine.   Thus - this file is getting re-worked.   I will call it memstate2 (at least
94
//  for awhile) as I wish to keep the old file around.  I want to show (in the state machine
95
//  logic) what the next memory operation is....   guess the best method consistent with my
96
//  documentation practices is to define a register (mem_op)  = { if, wb_we, wb_cyc }.  
97
//  This will require auxillary logic for computing the address ---  but most of the decodes
98
//  required will be there anyway.   
99
//  On further reflection, I think I will bite-the-bullet and use an always to define next_state.
100
//  I don't like to use always to define wires, but I also want to dicument the setting of 
101
//  exec_ir2 in the same place - that is 3 different things.  
102
//  
103
//  Hazards:
104
//  There are 2 kinds of hazards:  mem_hazard => we are storing into the next instruction location
105
//                                 reg_hazard => we are modifying a register (ir2) that we are using
106
//                                                here (ir1)
107
//  In the former case, we throw out the instruction that arrives on the next tick, and restart the
108
//  instruction pipeline,   In the latter case, we simply wait a tick for the ir2 operaton to 
109
//  complete before starting the ir1 operation  
110
//-------1---------2---------3--------CVS Log -----------------------7---------8---------9--------0
111
//
112
//  $Id: memstate2.v,v 1.1.1.1 2004-04-13 23:50:19 bporcella Exp $
113
//
114
//  $Date: 2004-04-13 23:50:19 $
115
//  $Revision: 1.1.1.1 $
116
//  $Author: bporcella $
117
//  $Locker:  $
118
//  $State: Exp $
119
//
120
// Change History:
121
//      $Log: not supported by cvs2svn $
122
//
123
//
124
//-------1---------2---------3--------Module Name and Port List------7---------8---------9--------0
125
module memstate2(wb_adr, wb_we, wb_cyc, wb_stb, wb_lock, wb_tga_io, wb_dat_o,  add_out,
126
                exec_ir2, ir1, ir2, ir1dd, ir1fd, ir2dd, ir2fd, nn, sp,
127
 
128
                upd_ar, upd_br, upd_cr, upd_dr, upd_er, upd_hr, upd_lr,upd_fr,
129
                beq0, ceq0,
130
                ar, fr, br, cr, dr, er, hr, lr,
131
                ixr, iyr,
132
                wb_dat_i, wb_ack, clk, rst,
133
                wb_int_rq,
134
                add16
135
 
136
 
137
);
138
 
139
//-------1---------2---------3--------Output Ports---------6---------7---------8---------9--------0
140
 
141
output [15:0]  wb_adr;
142
output         wb_we;
143
output         wb_cyc;
144
output         wb_stb;
145
output         wb_lock;     // bit set and clear insts should be atomic - could matter sometime
146
output         wb_tga_io;
147
output         wb_dat_o;   // from nn
148
output [15:0]  add_out;     // output of adder  (may not wb_adr)
149
 
150
output         exec_ir2;
151
output [9:0]   ir1, ir2;
152
output         ir1dd, ir2dd;
153
output          ir1fd, ir2fd;
154
output [15:0]   nn;
155
output [15:0]   sp;
156
 
157
 
158
 
159
 
160
//-------1---------2---------3--------Input Ports----------6---------7---------8---------9--------0
161
input           upd_ar, upd_br, upd_cr, upd_dr, upd_er, upd_hr, upd_lr,upd_fr;
162
 
163
input           beq0, ceq0;
164
input [7:0]     ar, fr, br, cr, dr, er, hr, lr;
165
input [15:0]    ixr, iyr;
166
input [7:0]     wb_dat_i;
167
input           wb_ack, clk, rst;
168
input           wb_int_rq;
169
input [15:0]    add16;         //  ir2 execution engine output for sp updates
170
 
171
 
172
//-------1---------2---------3--------Parameters-----------6---------7---------8---------9--------0
173
`include "opcodes.v"            //  states of the main memory sequencer
174
 
175
 
176
parameter   TAG_IO    = 2'b01,   // need to review general wb usage to undrstand how best to 
177
            TAG_INT   = 2'b10;   // document this.
178
            //                  12na
179
parameter   IPIPE_NOP       = 4'b0000,
180
            IPIPE_A2        = 4'b0001,
181
            IPIPE_ENN       = 4'b0010,
182
            IPIPE_ENNA2     = 4'b0011,
183
            IPIPE_EN2       = 4'b0100,
184
            IPIPE_EN2A2     = 4'b0101,
185
            IPIPE_ENNEN2    = 4'b0110,
186
            IPIPE_ENNEN2A2  = 4'b0111,
187
            IPIPE_EN1       = 4'b1000,
188
            IPIPE_EN1A2     = 4'b1001,
189
            IPIPE_BOGUS     = 4'b1010,  // no reason (yet) to load both n and ir1
190
            IPIPE_BOUS2     = 4'b1011,
191
            IPIPE_EN12      = 4'b1100,
192
            IPIPE_EN12A2    = 4'b1101,
193
            IPIPE_BOGUS3    = 4'b1110,
194
            IPIPE_BOGUS4    = 4'b1111;
195
 
196
//  well at first cut I tried to make this 2 state macines both less than 16 states.
197
//  this is 56 states at first cut.   Assignemnt is subject to change.
198
 
199
// ------  mem state decoder state machine states --------------------------------
200
parameter       DEC_IDLE      = 6'h00,
201
                DEC_HALT      = 6'h01,
202
                DEC_IF1       = 6'h02,
203
                DEC_IF2       = 6'h03,
204
                DEC_IF2A      = 6'h04,
205
                DEC_EXEC      = 6'h05,
206
                DEC_CB        = 6'h06,
207
                DEC_DDFD      = 6'h07,
208
                DEC_ED        = 6'h08,
209
                DEC_EDNN1     = 6'h09,
210
                DEC_EDNN2     = 6'h0a,
211
                DEC_EDRD1     = 6'h0b,
212
                DEC_EDRD2     = 6'h0c,
213
                DEC_EDWR      = 6'h0d,
214
                DEC_EDBCP1    = 6'h0e,
215
                DEC_EDBCP2    = 6'h0f,
216
                DEC_EDBCP3    = 6'h10,
217
                DEC_EDBIN1    = 6'h11,
218
                DEC_EDBIN2    = 6'h12,
219
                DEC_EDBIN3    = 6'h13,
220
                DEC_EDBOUT1   = 6'h14,
221
                DEC_EDBOUT2   = 6'h15,
222
                DEC_EDBOUT3   = 6'h16,
223
                DEC_EDBMV1    = 6'h17,
224
                DEC_EDBMV2    = 6'h18,
225
                DEC_EDBMV3    = 6'h19,
226
                DEC_N         = 6'h1a,
227
                DEC_NIN       = 6'h1b,
228
                DEC_NN        = 6'h1c,
229
                DEC_NNCALL1   = 6'h1d,
230
                DEC_NNCALL2   = 6'h1e,
231
                DEC_NNOS1     = 6'h1f,
232
                DEC_NNOS2     = 6'h20,
233
                DEC_NNOS3     = 6'h21,
234
                DEC_NNOF1     = 6'h22,
235
                DEC_NNOF2     = 6'h23,
236
                DEC_NNOF3     = 6'h24,
237
                DEC_NNOF4     = 6'h25,
238
                DEC_DDOS      = 6'h26,
239
                DEC_DDOF      = 6'h27,
240
                DEC_OF        = 6'h28,
241
                DEC_POP       = 6'h29,
242
                DEC_PUSH      = 6'h2a,
243
                DEC_RMW       = 6'h2b,
244
                DEC_RMW2      = 6'h2c,
245
                DEC_CBM       = 6'h2d,
246
                DEC_PFxCB     = 6'h2e,
247
                DEC_PFxCB2    = 6'h2f,
248
                DEC_PFxCB3    = 6'h30,
249
                DEC_PFxCB4    = 6'h31,
250
                DEC_INT1      = 6'h32,
251
                DEC_INT2      = 6'h33,
252
                DEC_INT3      = 6'h34,
253
                DEC_INT4      = 6'h35,
254
                DEC_INT5      = 6'h36,
255
                DEC_RET       = 6'h37,
256
                DEC_NNJMP     = 6'h38,
257
                DEC_RET2      = 6'h39 ;
258
 
259
//  initial decode assignemnts.   These assignemens are made to wires on an initial decode
260
//  to help document next state transitions
261
parameter      I1_CB    = 4'h0,
262
               I1_DDFD  = 4'h1,
263
               I1_ED    = 4'h2,
264
               I1_JMP   = 4'h3,
265
               I1_N     = 4'h4,
266
               I1_NN    = 4'h5,
267
               I1_OF    = 4'h6,
268
               I1_OS    = 4'h7,
269
               I1_POP   = 4'h8,
270
               I1_PUSH  = 4'h9,
271
               I1_RET   = 4'ha,
272
               I1_RMW   = 4'hb,
273
               I1_RST   = 4'hc,
274
               I1_R2R   = 4'hd ;
275
 
276
 
277
// A note here on the choices of mnemonics.....   in general, the target registers of 
278
// memory ops are specified by an instruction register  (ir1 for stores ir2 for loads).
279
// so Menomics in general are specifying the address source.   However, there are exceptions.
280
//
281
parameter       MEM_NOP      = 5'h00,
282
                MEM_IFPP1    = 5'h01,
283
                MEM_OS1      = 5'h02,      //  only invoked on I1 OS  multiple address sources and data sources
284
                MEM_OF1      = 5'h03,     //  Address from HL  unless   LD A,(BC) or LD A,(DE)  (used for rmw)
285
                MEM_OFSP     = 5'h04,     //  works for both POP and RET 
286
                MEM_OSSP     = 5'h05,     //  if DEC_EXEC  op from ir1  else msb nn  (implies we store from lsb nn)
287
                                          //  used in CALL also.  
288
                MEM_OFIXpD   = 5'h06,     //  used for prefix op fetches  - all single bytes
289
                MEM_OSIXpD   = 5'h07,     //  data source is same as MEM_OS1
290
                MEM_OSADR    = 5'h08,     //  used (at lesat)  for prefixed rmw --  perhaps others.
291
 
292
                MEM_CALL     = 5'h09,     // pc<=nn, nn<=pc, wb_adr<=sp   OS 
293
                MEM_OSNN     = 5'h0a,     //  if DEC_EXEC  op from ir1  else msb nn
294
                MEM_OFNN     = 5'h0b,     // striaghtfoward
295
                MEM_OFADRP1  = 5'h0c,     // used (at least) when double ops above
296
                MEM_OSADRP1  = 5'h0d,     //  ""              ""              ""
297
 
298
                MEM_IFRST    = 5'h0e,     // special address transfer
299
                MEM_IFREL_N  = 5'h0f,     // special address transfer for jmp rel
300
                MEM_JMPHL    = 5'h10,     // another special jump transfer
301
                MEM_IFNN     = 5'h11,        //  used by call and return
302
 
303
 
304
                MEM_OFHL_PM  = 5'h12,             // special block move ops  
305
                MEM_OSHL_PM  = 5'h13,             // special block move ops
306
                MEM_OSDE_PM  = 5'h14,             // special block move ops
307
 
308
                MEM_IOF_C    = 5'h15,             // special i/o ops
309
                MEM_IOS_C    = 5'h16,             // operand is ar
310
                MEM_IOF_N    = 5'h17,
311
                MEM_IOS_N    = 5'h18,
312
                MEM_OS_HL_N  = 5'h19,
313
 
314
                MEM_OSSP_PCM2 = 5'h1a,              // int code  (call 
315
                MEM_OSSP_P   = 5'h1b,              //
316
                MEM_INTA     = 5'h1c,
317
                MEM_IFINT    = 5'h1d,
318
                MEM_DECPC    = 5'h1e ;
319
 
320
 
321
 
322
 
323
 
324
 
325
 
326
 
327
 
328
//-------1---------2---------3--------Wires----------------6---------7---------8---------9--------0
329
 
330
 
331
wire        use_sp;
332
wire        use_pc;
333
wire        use_hl;
334
wire        use_de;
335
wire        use_bc;
336
wire        use_flags;
337
wire        cb_mem;
338
wire        br_test8t;  // branch test true  (8 test field)
339
wire        br_test4t;  // branch test true  (4 test field)
340
 
341
wire        ofos;
342
wire        any_os;   // most terms above only valid on mem_exec  this includes all stores
343
wire        wb_rdy_nhz;
344
wire        dec_blk_inc;
345
wire        we_next;
346
wire        hazard;
347
wire        wb_int;
348
wire [15:0] hl, de, bc;
349
wire        mem_exec_dec;
350
 
351
wire  use_a  ;
352
wire  use_b  ;
353
wire  use_c  ;
354
wire  use_d  ;
355
wire  use_e  ;
356
wire  use_h  ;
357
wire  use_l  ;
358
// don't forget that as 1r1 is executed it is transferred to ir2.  Anything I need to know
359
// about subsequent operations must be stored.
360
//               6              5              4                15
361
// assign {next_dec_state, next_mem_state, next_pipe_state} = next_state;
362
wire  [5:0]        next_dec_state;
363
wire  [4:0]        next_mem_state;
364
wire  [3:0]        next_pipe_state;
365
wire               ed_dbl_rd;
366
//-------1---------2---------3--------Registers------------6---------7---------8---------9--------0
367
 
368
reg [15:0]   pc;
369
reg [15:0]   sp;
370
reg [15:0]   wb_adr;
371
reg          wb_we;
372
reg          wb_cyc;
373
reg          wb_stb;
374
reg          wb_lock;
375
reg          wb_tga_io;
376
 
377
reg          blk_inc_flg;
378
reg [9:0]    ir1, ir2;
379
reg          ir1dd, ir2dd;
380
reg          ir1fd, ir2fd;
381
reg [15:0]   nn;
382
 
383
reg   [15:0]       next_state;      // a wire assigned in an alowys loop.
384
 
385
reg   [5:0]  dec_state;    // the register set each clock from next_dec_state;
386
 
387
reg          of16_reg,  os16_reg, rmw8_reg, call_reg, ret_reg, ioi;
388
reg          push_reg;
389
reg          pop_reg;
390
reg          inst_haz;
391
reg          exec_ir2;
392
reg          blk_rpt_flg;
393
reg          blk_io_flg;
394
reg          flag_os1;
395
reg          int_en, en_int_next;
396
reg          wb_irq_sync;
397
//-------1---------2---------3--------Assignments----------6---------7---------8---------9--------0
398
//
399
// ir is 10 bits most significant codes ir1[9:8] = { EDgrp, CBgrp }  DDgrp and FDgrp are modifiers
400
 
401
 
402
assign wb_dat_o = nn[15:8];
403
 
404
wire   sf, zf, f5f, hf, f3f, pvf, nf, cf;
405
assign { sf, zf, f5f, hf, f3f, pvf, nf, cf} = fr;
406
 
407
 
408
assign hl = {hr, lr};
409
assign de = {dr, er};
410
assign bc = {br, cr};
411
 
412
 
413
//  this "groups" the instructions to determine first memory operation
414
 
415
parameter  I1DCNT = 4;  // parameter used below simply to make possible change easier.
416
assign mem_exec_dec =
417
    {I1DCNT {CBgrp        == ir1}} & I1_CB  |//       CBgrp is rotates and bi
418
    {I1DCNT {DDgrp        == ir1}} & I1_DDFD|//      DDgrp   
419
    {I1DCNT {FDgrp        == ir1}} & I1_DDFD|//      FDgrp          FD
420
    {I1DCNT {EDgrp        == ir1}} & I1_ED  |//      EDgrp          ED
421
    {I1DCNT {JPsHL        == ir1}} & I1_JMP |//      JP HL        ; E9 // doc
422
    {I1DCNT {ADCsA_N      == ir1}} & I1_N   |//      ADC A,N      ; CE XX
423
    {I1DCNT {ADDsA_N      == ir1}} & I1_N   |//      ADD A,N      ; C6 XX
424
    {I1DCNT {ANDsN        == ir1}} & I1_N   |//      AND N        ; E6 XX
425
    {I1DCNT {CPsN         == ir1}} & I1_N   |//      CP N         ; FE XX
426
    {I1DCNT {INsA_6N7     == ir1}} & I1_N   |//      IN A,(N)     ; DB XX
427
    {I1DCNT {JRs$t2       == ir1}} & I1_N   |//      JR $+2       ; 18 XX
428
    {I1DCNT {JRsC_$t2     == ir1}} & I1_N   |//      JR C,$+2     ; 38 XX
429
    {I1DCNT {JRsNC_$t2    == ir1}} & I1_N   |//      JR NC,$+2    ; 30 XX
430
    {I1DCNT {JRsZ_$t2     == ir1}} & I1_N   |//      JR Z,$+2     ; 28 XX
431
    {I1DCNT {JRsNZ_$t2    == ir1}} & I1_N   |//      JR NZ,$+2    ; 20 XX
432
    {I1DCNT {LDs6HL7_N    == ir1}} & I1_N   |//      LD (HL),N    ; 36 XX
433
    {I1DCNT {LDsA_N       == ir1}} & I1_N   |//      LD A,N       ; 3E XX
434
    {I1DCNT {LDsB_N       == ir1}} & I1_N   |//      LD B,N       ; 06 XX
435
    {I1DCNT {LDsC_N       == ir1}} & I1_N   |//      LD C,N       ; 0E XX
436
    {I1DCNT {LDsD_N       == ir1}} & I1_N   |//      LD D,N       ; 16 XX
437
    {I1DCNT {LDsE_N       == ir1}} & I1_N   |//      LD E,N       ; 1E XX
438
    {I1DCNT {LDsH_N       == ir1}} & I1_N   |//      LD H,N       ; 26 XX
439
    {I1DCNT {LDsL_N       == ir1}} & I1_N   |//      LD L,N       ; 2E XX
440
    {I1DCNT {ORsN         == ir1}} & I1_N   |//      OR N         ; F6 XX
441
    {I1DCNT {OUTs6N7_A    == ir1}} & I1_N   |//      OUT (N),A    ; D3 XX
442
    {I1DCNT {SBCsA_N      == ir1}} & I1_N   |//      SBC A,N      ; DE XX
443
    {I1DCNT {SUBsN        == ir1}} & I1_N   |//      SUB N        ; D6 XX
444
    {I1DCNT {XORsN        == ir1}} & I1_N   |//      XOR N        ; EE XX
445
    {I1DCNT {CALLsC_NN    == ir1}} & I1_NN  |//      CALL C,NN    ; DC XX XX
446
    {I1DCNT {CALLsNC_NN   == ir1}} & I1_NN  |//      CALL NC,NN   ; D4 XX XX
447
    {I1DCNT {CALLsNN      == ir1}} & I1_NN  |//      CALL NN      ; CD XX XX
448
    {I1DCNT {CALLsNZ_NN   == ir1}} & I1_NN  |//      CALL NZ,NN   ; C4 XX XX
449
    {I1DCNT {CALLsPE_NN   == ir1}} & I1_NN  |//      CALL PE,NN   ; EC XX XX
450
    {I1DCNT {CALLsPO_NN   == ir1}} & I1_NN  |//      CALL PO,NN   ; E4 XX XX
451
    {I1DCNT {CALLsP_NN    == ir1}} & I1_NN  |//      CALL P,NN    ; F4 XX XX
452
    {I1DCNT {CALLsZ_NN    == ir1}} & I1_NN  |//      CALL Z,NN    ; CC XX XX
453
    {I1DCNT {CALLsM_NN    == ir1}} & I1_NN  |//      CALL M,NN    ; FC XX XX
454
    {I1DCNT {JP           == ir1}} & I1_NN  |//      JP           ; C3 XX XX
455
    {I1DCNT {JPsC         == ir1}} & I1_NN  |//      JP C         ; DA XX XX
456
    {I1DCNT {JPsM         == ir1}} & I1_NN  |//      JP M,        ; FA XX XX
457
    {I1DCNT {JPsNC        == ir1}} & I1_NN  |//      JP NC,       ; D2 XX XX
458
    {I1DCNT {JPsNZ        == ir1}} & I1_NN  |//      JP NZ        ; C2 XX XX
459
    {I1DCNT {JPsP         == ir1}} & I1_NN  |//      JP P         ; F2 XX XX
460
    {I1DCNT {JPsPE        == ir1}} & I1_NN  |//      JP PE,       ; EA XX XX
461
    {I1DCNT {JPsPO        == ir1}} & I1_NN  |//      JP PO        ; E2 XX XX
462
    {I1DCNT {JPsZ         == ir1}} & I1_NN  |//      JP Z         ; CA XX XX
463
    {I1DCNT {LDs6NN7_A    == ir1}} & I1_NN  |//      LD (NN),A    ; 32 XX XX
464
    {I1DCNT {LDs6NN7_HL   == ir1}} & I1_NN  |//      LD (NN),HL   ; 22 XX XX
465
    {I1DCNT {LDsA_6NN7    == ir1}} & I1_NN  |//      LD A,(NN)    ; 3A XX XX
466
    {I1DCNT {LDsBC_NN     == ir1}} & I1_NN  |//      LD BC,NN     ; 01 XX XX
467
    {I1DCNT {LDsDE_NN     == ir1}} & I1_NN  |//      LD DE,NN     ; 11 XX XX
468
    {I1DCNT {LDsHL_6NN7   == ir1}} & I1_NN  |//      LD HL,(NN)   ; 2A XX XX
469
    {I1DCNT {LDsHL_NN     == ir1}} & I1_NN  |//      LD HL,NN     ; 21 XX XX
470
    {I1DCNT {LDsSP_NN     == ir1}} & I1_NN  |//      LD SP,NN     ; 31 XX XX
471
    {I1DCNT {ADCsA_6HL7   == ir1}} & I1_OF  |//      ADC A,(HL)   ; 8E
472
    {I1DCNT {ADDsA_6HL7   == ir1}} & I1_OF  |//      ADD A,(HL)   ; 86
473
    {I1DCNT {ANDs6HL7     == ir1}} & I1_OF  |//      AND (HL)     ; A6
474
    {I1DCNT {CPs6HL7      == ir1}} & I1_OF  |//      CP (HL)      ; BE
475
    {I1DCNT {LDsA_6BC7    == ir1}} & I1_OF  |//      LD A,(BC)    ; 0A
476
    {I1DCNT {LDsA_6DE7    == ir1}} & I1_OF  |//      LD A,(DE)    ; 1A
477
    {I1DCNT {LDsA_6HL7    == ir1}} & I1_OF  |//      LD A,(HL)    ; 7E
478
    {I1DCNT {LDsB_6HL7    == ir1}} & I1_OF  |//      LD B,(HL)    ; 46
479
    {I1DCNT {LDsC_6HL7    == ir1}} & I1_OF  |//      LD C,(HL)    ; 4E
480
    {I1DCNT {LDsD_6HL7    == ir1}} & I1_OF  |//      LD D,(HL)    ; 56
481
    {I1DCNT {LDsE_6HL7    == ir1}} & I1_OF  |//      LD E,(HL)    ; 5E
482
    {I1DCNT {LDsH_6HL7    == ir1}} & I1_OF  |//      LD H,(HL)    ; 66
483
    {I1DCNT {LDsL_6HL7    == ir1}} & I1_OF  |//      LD L,(HL)    ; 6E
484
    {I1DCNT {ORs6HL7      == ir1}} & I1_OF  |//      OR (HL)      ; B6
485
    {I1DCNT {SBCs6HL7     == ir1}} & I1_OF  |//      SBC (HL)     ; 9E
486
    {I1DCNT {SUBs6HL7     == ir1}} & I1_OF  |//      SUB (HL)     ; 96
487
    {I1DCNT {XORs6HL7     == ir1}} & I1_OF  |//      XOR (HL)     ; AE
488
    {I1DCNT {LDs6BC7_A    == ir1}} & I1_OS  |//      LD (BC),A    ; 02 
489
    {I1DCNT {LDs6DE7_A    == ir1}} & I1_OS  |//      LD (DE),A    ; 12
490
    {I1DCNT {LDs6HL7_A    == ir1}} & I1_OS  |//      LD (HL),A    ; 77
491
    {I1DCNT {LDs6HL7_B    == ir1}} & I1_OS  |//      LD (HL),B    ; 70
492
    {I1DCNT {LDs6HL7_C    == ir1}} & I1_OS  |//      LD (HL),C    ; 71
493
    {I1DCNT {LDs6HL7_D    == ir1}} & I1_OS  |//      LD (HL),D    ; 72
494
    {I1DCNT {LDs6HL7_E    == ir1}} & I1_OS  |//      LD (HL),E    ; 73
495
    {I1DCNT {LDs6HL7_H    == ir1}} & I1_OS  |//      LD (HL),H    ; 74
496
    {I1DCNT {LDs6HL7_L    == ir1}} & I1_OS  |//      LD (HL),L    ; 75
497
    {I1DCNT {POPsAF       == ir1}} & I1_POP |//      POP AF       ; F1
498
    {I1DCNT {POPsBC       == ir1}} & I1_POP |//      POP BC       ; C1
499
    {I1DCNT {POPsDE       == ir1}} & I1_POP |//      POP DE       ; D1
500
    {I1DCNT {POPsHL       == ir1}} & I1_POP |//      POP HL       ; E1
501
    {I1DCNT {PUSHsAF      == ir1}} & I1_PUSH|//      PUSH AF      ; F5
502
    {I1DCNT {PUSHsBC      == ir1}} & I1_PUSH|//      PUSH BC      ; C5
503
    {I1DCNT {PUSHsDE      == ir1}} & I1_PUSH|//      PUSH DE      ; D5
504
    {I1DCNT {PUSHsHL      == ir1}} & I1_PUSH|//      PUSH HL      ; E5
505
    {I1DCNT {ADCsA_A      == ir1}} & I1_R2R |//      ADC A,A      ; 8F
506
    {I1DCNT {ADCsA_B      == ir1}} & I1_R2R |//      ADC A,B      ; 88
507
    {I1DCNT {ADCsA_C      == ir1}} & I1_R2R |//      ADC A,C      ; 89
508
    {I1DCNT {ADCsA_D      == ir1}} & I1_R2R |//      ADC A,D      ; 8A
509
    {I1DCNT {ADCsA_E      == ir1}} & I1_R2R |//      ADC A,E      ; 8B
510
    {I1DCNT {ADCsA_H      == ir1}} & I1_R2R |//      ADC A,H      ; 8C
511
    {I1DCNT {ADCsA_L      == ir1}} & I1_R2R |//      ADC A,L      ; 8D
512
    {I1DCNT {ADDsA_A      == ir1}} & I1_R2R |//      ADD A,A      ; 87
513
    {I1DCNT {ADDsA_B      == ir1}} & I1_R2R |//      ADD A,B      ; 80
514
    {I1DCNT {ADDsA_C      == ir1}} & I1_R2R |//      ADD A,C      ; 81
515
    {I1DCNT {ADDsA_D      == ir1}} & I1_R2R |//      ADD A,D      ; 82
516
    {I1DCNT {ADDsA_E      == ir1}} & I1_R2R |//      ADD A,E      ; 83
517
    {I1DCNT {ADDsA_H      == ir1}} & I1_R2R |//      ADD A,H      ; 84
518
    {I1DCNT {ADDsA_L      == ir1}} & I1_R2R |//      ADD A,L      ; 85
519
    {I1DCNT {ADDsHL_BC    == ir1}} & I1_R2R |//      ADD HL,BC    ; 09
520
    {I1DCNT {ADDsHL_DE    == ir1}} & I1_R2R |//      ADD HL,DE    ; 19
521
    {I1DCNT {ADDsHL_HL    == ir1}} & I1_R2R |//      ADD HL,HL    ; 29
522
    {I1DCNT {ADDsHL_SP    == ir1}} & I1_R2R |//      ADD HL,SP    ; 39
523
    {I1DCNT {ANDsA        == ir1}} & I1_R2R |//      AND A        ; A7
524
    {I1DCNT {ANDsB        == ir1}} & I1_R2R |//      AND B        ; A0
525
    {I1DCNT {ANDsC        == ir1}} & I1_R2R |//      AND C        ; A1
526
    {I1DCNT {ANDsD        == ir1}} & I1_R2R |//      AND D        ; A2
527
    {I1DCNT {ANDsE        == ir1}} & I1_R2R |//      AND E        ; A3
528
    {I1DCNT {ANDsH        == ir1}} & I1_R2R |//      AND H        ; A4
529
    {I1DCNT {ANDsL        == ir1}} & I1_R2R |//      AND L        ; A5
530
    {I1DCNT {CCF          == ir1}} & I1_R2R |//      CCF          ; 3F
531
    {I1DCNT {CPL          == ir1}} & I1_R2R |//      CPL          ; 2F
532
    {I1DCNT {CPsA         == ir1}} & I1_R2R |//      CP A         ; BF
533
    {I1DCNT {CPsB         == ir1}} & I1_R2R |//      CP B         ; B8
534
    {I1DCNT {CPsC         == ir1}} & I1_R2R |//      CP C         ; B9
535
    {I1DCNT {CPsD         == ir1}} & I1_R2R |//      CP D         ; BA
536
    {I1DCNT {CPsE         == ir1}} & I1_R2R |//      CP E         ; BB
537
    {I1DCNT {CPsH         == ir1}} & I1_R2R |//      CP H         ; BC
538
    {I1DCNT {CPsL         == ir1}} & I1_R2R |//      CP L         ; BD
539
    {I1DCNT {DAA          == ir1}} & I1_R2R |//      DAA          ; 27
540
    {I1DCNT {DECsA        == ir1}} & I1_R2R |//      DEC A        ; 3D
541
    {I1DCNT {DECsB        == ir1}} & I1_R2R |//      DEC B        ; 05
542
    {I1DCNT {DECsBC       == ir1}} & I1_R2R |//      DEC BC       ; 0B
543
    {I1DCNT {DECsC        == ir1}} & I1_R2R |//      DEC C        ; 0D
544
    {I1DCNT {DECsD        == ir1}} & I1_R2R |//      DEC D        ; 15
545
    {I1DCNT {DECsDE       == ir1}} & I1_R2R |//      DEC DE       ; 1B
546
    {I1DCNT {DECsE        == ir1}} & I1_R2R |//      DEC E        ; 1D
547
    {I1DCNT {DECsH        == ir1}} & I1_R2R |//      DEC H        ; 25
548
    {I1DCNT {DECsHL       == ir1}} & I1_R2R |//      DEC HL       ; 2B
549
    {I1DCNT {DECsL        == ir1}} & I1_R2R |//      DEC L        ; 2D
550
    {I1DCNT {DECsSP       == ir1}} & I1_R2R |//      DEC SP       ; 3B
551
    {I1DCNT {DI           == ir1}} & I1_R2R |//      DI           ; F3
552
    {I1DCNT {DJNZs$t2     == ir1}} & I1_R2R |//      DJNZ $+2     ; 10 XX
553
    {I1DCNT {EI           == ir1}} & I1_R2R |//      EI           ; FB
554
    {I1DCNT {EXX          == ir1}} & I1_R2R |//      EXX          ; D9
555
    {I1DCNT {EXsAF_AFp    == ir1}} & I1_R2R |//      EX AF,AF'    ; 08
556
    {I1DCNT {EXsDE_HL     == ir1}} & I1_R2R |//      EX DE,HL     ; EB
557
    {I1DCNT {HALT         == ir1}} & I1_R2R |//      HALT         ; 76
558
    {I1DCNT {INCsA        == ir1}} & I1_R2R |//      INC A        ; 3C
559
    {I1DCNT {INCsB        == ir1}} & I1_R2R |//      INC B       ; 04
560
    {I1DCNT {INCsBC       == ir1}} & I1_R2R |//      INC BC      ; 03
561
    {I1DCNT {INCsC        == ir1}} & I1_R2R |//      INC C       ; 0C
562
    {I1DCNT {INCsD        == ir1}} & I1_R2R |//      INC D        ; 14
563
    {I1DCNT {INCsDE       == ir1}} & I1_R2R |//      INC DE       ; 13
564
    {I1DCNT {INCsE        == ir1}} & I1_R2R |//      INC E        ; 1C
565
    {I1DCNT {INCsH        == ir1}} & I1_R2R |//      INC H        ; 24
566
    {I1DCNT {INCsHL       == ir1}} & I1_R2R |//      INC HL       ; 23
567
    {I1DCNT {INCsL        == ir1}} & I1_R2R |//      INC L        ; 2C
568
    {I1DCNT {INCsSP       == ir1}} & I1_R2R |//      INC SP       ; 33
569
    {I1DCNT {LDsA_A       == ir1}} & I1_R2R |//      LD A,A       ; 7F
570
    {I1DCNT {LDsA_B       == ir1}} & I1_R2R |//      LD A,B       ; 78
571
    {I1DCNT {LDsA_C       == ir1}} & I1_R2R |//      LD A,C       ; 79
572
    {I1DCNT {LDsA_D       == ir1}} & I1_R2R |//      LD A,D       ; 7A
573
    {I1DCNT {LDsA_E       == ir1}} & I1_R2R |//      LD A,E       ; 7B
574
    {I1DCNT {LDsA_H       == ir1}} & I1_R2R |//      LD A,H       ; 7C
575
    {I1DCNT {LDsA_L       == ir1}} & I1_R2R |//      LD A,L       ; 7D
576
    {I1DCNT {LDsB_A       == ir1}} & I1_R2R |//      LD B,A       ; 47
577
    {I1DCNT {LDsB_B       == ir1}} & I1_R2R |//      LD B,B       ; 40
578
    {I1DCNT {LDsB_C       == ir1}} & I1_R2R |//      LD B,C       ; 41
579
    {I1DCNT {LDsB_D       == ir1}} & I1_R2R |//      LD B,D       ; 42
580
    {I1DCNT {LDsB_E       == ir1}} & I1_R2R |//      LD B,E       ; 43
581
    {I1DCNT {LDsB_H       == ir1}} & I1_R2R |//      LD B,H       ; 44
582
    {I1DCNT {LDsB_L       == ir1}} & I1_R2R |//      LD B,L       ; 45
583
    {I1DCNT {LDsC_A       == ir1}} & I1_R2R |//      LD C,A       ; 4F
584
    {I1DCNT {LDsC_B       == ir1}} & I1_R2R |//      LD C,B       ; 48
585
    {I1DCNT {LDsC_C       == ir1}} & I1_R2R |//      LD C,C       ; 49
586
    {I1DCNT {LDsC_D       == ir1}} & I1_R2R |//      LD C,D       ; 4A
587
    {I1DCNT {LDsC_E       == ir1}} & I1_R2R |//      LD C,E       ; 4B
588
    {I1DCNT {LDsC_H       == ir1}} & I1_R2R |//      LD C,H       ; 4C
589
    {I1DCNT {LDsC_L       == ir1}} & I1_R2R |//      LD C,L       ; 4D
590
    {I1DCNT {LDsD_A       == ir1}} & I1_R2R |//      LD D,A       ; 57
591
    {I1DCNT {LDsD_B       == ir1}} & I1_R2R |//      LD D,B       ; 50
592
    {I1DCNT {LDsD_C       == ir1}} & I1_R2R |//      LD D,C       ; 51
593
    {I1DCNT {LDsD_D       == ir1}} & I1_R2R |//      LD D,D       ; 52
594
    {I1DCNT {LDsD_E       == ir1}} & I1_R2R |//      LD D,E       ; 53
595
    {I1DCNT {LDsD_H       == ir1}} & I1_R2R |//      LD D,H       ; 54
596
    {I1DCNT {LDsD_L       == ir1}} & I1_R2R |//      LD D,L       ; 55
597
    {I1DCNT {LDsE_A       == ir1}} & I1_R2R |//      LD E,A       ; 5F
598
    {I1DCNT {LDsE_B       == ir1}} & I1_R2R |//      LD E,B       ; 58
599
    {I1DCNT {LDsE_C       == ir1}} & I1_R2R |//      LD E,C       ; 59
600
    {I1DCNT {LDsE_D       == ir1}} & I1_R2R |//      LD E,D       ; 5A
601
    {I1DCNT {LDsE_E       == ir1}} & I1_R2R |//      LD E,E       ; 5B
602
    {I1DCNT {LDsE_H       == ir1}} & I1_R2R |//      LD E,H       ; 5C
603
    {I1DCNT {LDsE_L       == ir1}} & I1_R2R |//      LD E,L       ; 5D
604
    {I1DCNT {LDsH_A       == ir1}} & I1_R2R |//      LD H,A       ; 67
605
    {I1DCNT {LDsH_B       == ir1}} & I1_R2R |//      LD H,B       ; 60
606
    {I1DCNT {LDsH_C       == ir1}} & I1_R2R |//      LD H,C       ; 61
607
    {I1DCNT {LDsH_D       == ir1}} & I1_R2R |//      LD H,D       ; 62
608
    {I1DCNT {LDsH_E       == ir1}} & I1_R2R |//      LD H,E       ; 63
609
    {I1DCNT {LDsH_H       == ir1}} & I1_R2R |//      LD H,H       ; 64
610
    {I1DCNT {LDsH_L       == ir1}} & I1_R2R |//      LD H,L       ; 65
611
    {I1DCNT {LDsL_A       == ir1}} & I1_R2R |//      LD L,A       ; 6F
612
    {I1DCNT {LDsL_B       == ir1}} & I1_R2R |//      LD L,B       ; 68
613
    {I1DCNT {LDsL_C       == ir1}} & I1_R2R |//      LD L,C       ; 69
614
    {I1DCNT {LDsL_D       == ir1}} & I1_R2R |//      LD L,D       ; 6A
615
    {I1DCNT {LDsL_E       == ir1}} & I1_R2R |//      LD L,E       ; 6B
616
    {I1DCNT {LDsL_H       == ir1}} & I1_R2R |//      LD L,H       ; 6C
617
    {I1DCNT {LDsL_L       == ir1}} & I1_R2R |//      LD L,L       ; 6D
618
    {I1DCNT {LDsSP_HL     == ir1}} & I1_R2R |//      LD SP,HL     ; F9
619
    {I1DCNT {NOP          == ir1}} & I1_R2R |//      NOP         ; 00
620
    {I1DCNT {ORsA         == ir1}} & I1_R2R |//      OR A         ; B7
621
    {I1DCNT {ORsB         == ir1}} & I1_R2R |//      OR B         ; B0
622
    {I1DCNT {ORsC         == ir1}} & I1_R2R |//      OR C         ; B1
623
    {I1DCNT {ORsD         == ir1}} & I1_R2R |//      OR D         ; B2
624
    {I1DCNT {ORsE         == ir1}} & I1_R2R |//      OR E         ; B3
625
    {I1DCNT {ORsH         == ir1}} & I1_R2R |//      OR H         ; B4
626
    {I1DCNT {ORsL         == ir1}} & I1_R2R |//      OR L         ; B5
627
    {I1DCNT {RLA          == ir1}} & I1_R2R |//      RLA          ; 17
628
    {I1DCNT {RLCA         == ir1}} & I1_R2R |//      RLCA        ; 07
629
    {I1DCNT {RRA          == ir1}} & I1_R2R |//      RRA          ; 1F
630
    {I1DCNT {RRCA         == ir1}} & I1_R2R |//      RRCA        ; 0F
631
    {I1DCNT {SBCsA        == ir1}} & I1_R2R |//      SBC A        ; 9F
632
    {I1DCNT {SBCsB        == ir1}} & I1_R2R |//      SBC B        ; 98
633
    {I1DCNT {SBCsC        == ir1}} & I1_R2R |//      SBC C        ; 99
634
    {I1DCNT {SBCsD        == ir1}} & I1_R2R |//      SBC D        ; 9A
635
    {I1DCNT {SBCsE        == ir1}} & I1_R2R |//      SBC E        ; 9B
636
    {I1DCNT {SBCsH        == ir1}} & I1_R2R |//      SBC H        ; 9C
637
    {I1DCNT {SBCsL        == ir1}} & I1_R2R |//      SBC L        ; 9D
638
    {I1DCNT {SCF          == ir1}} & I1_R2R |//      SCF          ; 37
639
    {I1DCNT {SUBsA        == ir1}} & I1_R2R |//      SUB A        ; 97
640
    {I1DCNT {SUBsB        == ir1}} & I1_R2R |//      SUB B        ; 90
641
    {I1DCNT {SUBsC        == ir1}} & I1_R2R |//      SUB C        ; 91
642
    {I1DCNT {SUBsD        == ir1}} & I1_R2R |//      SUB D        ; 92
643
    {I1DCNT {SUBsE        == ir1}} & I1_R2R |//      SUB E        ; 93
644
    {I1DCNT {SUBsH        == ir1}} & I1_R2R |//      SUB H        ; 94
645
    {I1DCNT {SUBsL        == ir1}} & I1_R2R |//      SUB L        ; 95
646
    {I1DCNT {XORsA        == ir1}} & I1_R2R |//      XOR A        ; AF
647
    {I1DCNT {XORsB        == ir1}} & I1_R2R |//      XOR B        ; A8
648
    {I1DCNT {XORsC        == ir1}} & I1_R2R |//      XOR C        ; A9
649
    {I1DCNT {XORsD        == ir1}} & I1_R2R |//      XOR D        ; AA
650
    {I1DCNT {XORsE        == ir1}} & I1_R2R |//      XOR E        ; AB
651
    {I1DCNT {XORsH        == ir1}} & I1_R2R |//      XOR H        ; AC
652
    {I1DCNT {XORsL        == ir1}} & I1_R2R |//      XOR L        ; AD
653
    {I1DCNT {RET          == ir1}} & I1_RET |//      RET          ; C9
654
    {I1DCNT {RETsC == ir1 & cf  }} & I1_RET |//      RET C        ; D8
655
    {I1DCNT {RETsM == ir1 & sf  }} & I1_RET |//      RET M        ; F8
656
    {I1DCNT {RETsNC== ir1 & ~cf }} & I1_RET |//      RET NC       ; D0
657
    {I1DCNT {RETsP == ir1 & ~sf }} & I1_RET |//      RET P        ; F0
658
    {I1DCNT {RETsPE== ir1 & pvf }} & I1_RET |//      RET PE       ; E8
659
    {I1DCNT {RETsPO== ir1 & ~pvf}} & I1_RET |//      RET PO       ; E0
660
    {I1DCNT {RETsNZ== ir1 & ~zf }} & I1_RET |//      RET NZ       ; C0
661
    {I1DCNT {RETsZ == ir1 & zf  }} & I1_RET |//      RET Z        ; C8
662
    {I1DCNT {EXs6SP7_HL   == ir1}} & I1_RMW |//      EX (SP),HL   ; E3
663
    {I1DCNT {DECs6HL7     == ir1}} & I1_RMW |//      DEC (HL)     ; 35
664
    {I1DCNT {INCs6HL7     == ir1}} & I1_RMW |//      INC (HL)     ; 34
665
    {I1DCNT {RSTs0        == ir1}} & I1_RST |//      RST 0        ; C7
666
    {I1DCNT {RSTs10H      == ir1}} & I1_RST |//      RST 10H      ; D7
667
    {I1DCNT {RSTs18H      == ir1}} & I1_RST |//      RST 18H      ; DF
668
    {I1DCNT {RSTs20H      == ir1}} & I1_RST |//      RST 20H      ; E7
669
    {I1DCNT {RSTs28H      == ir1}} & I1_RST |//      RST 28H      ; EF       
670
    {I1DCNT {RSTs30H      == ir1}} & I1_RST |//      RST 30H      ; F7
671
    {I1DCNT {RSTs38H      == ir1}} & I1_RST |//      RST 38H      ; FF
672
    {I1DCNT {RSTs8H       == ir1}} & I1_RST ;//      RST 8H       ; CF 
673
 
674
//--------  CB decodes -----------------------
675
 
676
//  First cut below
677
//           CB_RLC   = 7'b01_00_000,  // these must be compaired with ir[9:3]
678
//           CB_RRC   = 7'b01_00_001,  // these must be compaired with ir[9:3]
679
//           CB_RL    = 7'b01_00_010,  // these must be compaired with ir[9:3]
680
//           CB_RR    = 7'b01_00_011,  // these must be compaired with ir[9:3]
681
//           CB_SLA   = 7'b01_00_100,  // these must be compaired with ir[9:3]
682
//           CB_SRA   = 7'b01_00_101,  // these must be compaired with ir[9:3]
683
//           CB_SLL   = 7'b01_00_110,  // these must be compaired with ir[9:3]
684
//           CB_SRL   = 7'b01_00_111,  // these must be compaired with ir[9:3]
685
 
686
//           CB_BIT   = 4'b01_01,    // these must be compaired with ir[9:6]
687
//           CB_RES   = 4'b01_10,    // these must be compaired with ir[9:6]
688
//           CB_SET   = 4'b01_11,    // these must be compaired with ir[9:6]
689
 
690
// note these are all read-modify-writ except CB_BIT
691
assign cb_mem =  (CB_MEM  == ir1[2:0]);   // this must be compaired with ir[2:0] 
692
 
693
//  The ED Group
694
// These are the "unique instructions in the 46, 47 rows that NEED? to be implemented
695
// Not sure I want to worry about all undocumented stuff in these rows - hard to believe
696
// It will matter.(IM modes are very system dependent  - hard to believe even a programmer
697
// would use undocumented instructions to muck with this stuff)
698
// reg 2 reg simply executed by ir2 logic
699
//           ED_IMs0      =  10'h246//      IM 0       ; ED 46   set IM0
700
//           ED_LDsI_A    =  10'h247//      LD I,A     ; ED 47   move a to I
701
//           ED_IMs1      =  10'h256//      IM 1       ; ED 56   set IM1
702
//           ED_LDsA_I    =  10'h257//      LD A,I     ; ED 57   move I to A
703
//           ED_IMs2      =  10'h25E//      IM 2       ; ED 5E   set IM2
704
//           ED_RRD       =  10'h267//      RRD        ; ED 67   nibble roates A HL
705
//           ED_RLD       =  10'h26F//      RLD        ; ED 6F   nibble roates A HL
706
 
707
//  set (or clear) repeat flag at  DEC_EB.
708
//  set (or clear) inc flag at     DEC_EB.
709
//  seperate flows for LD, CP, IN, OUT.
710
//           ED_LDI       == ir1//      LDI        ; ED A0    These are block move 
711
//           ED_CPI       == ir1//      CPI        ; ED A1    type insts that don't repeat
712
//           ED_INI       == ir1//      INI        ; ED A2
713
//           ED_OUTI      == ir1//      OUTI       ; ED A3
714
//           ED_LDD       == ir1//      LDD        ; ED A8
715
//           ED_CPD       == ir1//      CPD        ; ED A9
716
//           ED_IND       == ir1//      IND        ; ED AA
717
//           ED_OUTD      == ir1//      OUTD       ; ED AB
718
wire dec_blk_rpt =
719
           ED_LDIR      == ir1 |//      LDIR       ; ED B0    These are block move 
720
           ED_CPIR      == ir1 |//      CPIR       ; ED B1    type insts that DO repeat
721
           ED_INIR      == ir1 |//      INIR       ; ED B2
722
           ED_OTIR      == ir1 |//      OTIR       ; ED B3
723
           ED_LDDR      == ir1 |//      LDDR       ; ED B8
724
           ED_CPDR      == ir1 |//      CPDR       ; ED B9
725
           ED_INDR      == ir1 |//      INDR       ; ED BA
726
           ED_OTDR      == ir1 ;//      OTDR       ; ED BB
727
wire ed_blk_mv =  ED_LDIR      == ir1 |  ED_LDI       == ir1 |
728
                  ED_LDDR      == ir1 |  ED_LDD       == ir1 ;
729
wire ed_blk_cp =  ED_CPIR      == ir1 |  ED_CPI       == ir1 |
730
                  ED_CPDR      == ir1 |  ED_CPD       == ir1 ;
731
wire ed_blk_in =  ED_INIR      == ir1 |  ED_INI      == ir1 |
732
                  ED_INDR      == ir1 |  ED_IND      == ir1 ;
733
 
734
wire ed_blk_out = ED_OTIR      == ir1 |  ED_OUTI      == ir1 |
735
                  ED_OTDR      == ir1 |  ED_OUTD      == ir1 ;
736
 
737
wire dec_blk_io = ed_blk_in | ed_blk_in;
738
 
739
wire blk_done =  ~blk_rpt_flg |  beq0 & ceq0 | blk_io_flg & ceq0;
740
 
741
assign dec_blk_inc =  ED_LDIR      == ir1 |
742
                      ED_CPIR      == ir1 |
743
                      ED_INIR      == ir1 |
744
                      ED_OTIR      == ir1 |
745
                      ED_LDI       == ir1 |
746
                      ED_CPI       == ir1 |
747
                      ED_INI       == ir1 |
748
                      ED_OUTI      == ir1 ;
749
 
750
 
751
//The ED70 instruction reads from I/O port C, 
752
//but does not store the result.
753
//It just affects the flags.  Hard to test.    like the other IN x,(C) instruction. 
754
//
755
//ED71 simply outs the value 0 to I/O port C.
756
//  This suggests that we should decode as follows:
757
//  I hope if I don't get all the IM duplicates right it won't be a tragedy
758
//        ED_INsREG_6C7  =    7'b1001___000,// compair with {ir[7:6],ir[2:0]}
759
//        
760
//        ED_SBCsHL_REG  =    8'b1001__0010, // compair with {ir[9:6],ir[3:0]}
761
//        ED_ADCsHL_REG  =    8'b1001__1010, // compair with {ir[9:6],ir[3:0]}
762
//        ED_LDs6NN7_REG =    8'b1001__0011, // compair with {ir[9:6],ir[3:0]}  REG = BC,DE,HL,SP                   
763
//        ED_LDsREG_6NN7 =    8'b1001__1011, // compair with {ir[9:6],ir[3:0]}  REG = BC,DE,HL,SP
764
//        ED_NEG         =    7'b1001___100, // compair with {ir[9:6],ir[2:0]}  all A<= -A                  
765
//        ED_RETN        =    7'b1001___101, // compair with {ir[9:6],ir[2:0]} and !reti
766
wire ed_nn = ED_LDs6NN7_REG == {ir1[9:6],ir1[3:0]} |
767
             ED_LDsREG_6NN7 == {ir1[9:6],ir1[3:0]}  ;
768
 
769
//  we use all these to enable interrupts
770
wire ed_retn = ED_RETN == {ir1[9:6],ir1[2:0]};
771
 
772
assign ed_dbl_rd =  ED_LDsREG_6NN7 == {ir1[9:6],ir1[3:0]};
773
 
774
 
775
// assign   cb_mem = CB_MEM = ir1[2:0];                 // CB_MEM  = 3'h110,    
776
 
777
 
778
 
779
 
780
wire jmpr_true =
781
    JRs$t2       == ir1           |
782
    JRsC_$t2     == ir1  & fr[0]  |
783
    JRsNC_$t2    == ir1  & ~fr[0] |
784
    JRsZ_$t2     == ir1  & fr[6]  |
785
    JRsNZ_$t2    == ir1  & ~fr[6] ;
786
 
787
//assign { sf, zf. f5f, hf, f3f, pvf, nf, cf} = fr;              
788
wire callnn_true   =  CALLsC_NN    == ir1  & cf  |
789
                      CALLsNC_NN   == ir1  & ~cf |
790
                      CALLsNN      == ir1        |
791
                      CALLsNZ_NN   == ir1  & ~zf |
792
                      CALLsPE_NN   == ir1  & pvf |
793
                      CALLsPO_NN   == ir1  & ~pvf|
794
                      CALLsP_NN    == ir1  & ~sf |
795
                      CALLsZ_NN    == ir1  &  zf |
796
                      CALLsM_NN    == ir1  &  sf  ;
797
 
798
wire  jmpnn_true  =  JPsC         == ir1  & cf  |
799
                     JPsNC        == ir1  & ~cf |
800
                     JP           == ir1        |
801
                     JPsNZ        == ir1  & ~zf |
802
                     JPsPE        == ir1  & pvf |
803
                     JPsPO        == ir1  & ~pvf|
804
                     JPsP         == ir1  & ~sf |
805
                     JPsZ         == ir1  &  zf |
806
                     JPsM         == ir1  &  sf  ;
807
 
808
// PUSHsAF      == ir1
809
// PUSHsBC      == ir1
810
// PUSHsDE      == ir1
811
// PUSHsHL      == ir1
812
 
813
wire os_a  =  LDs6BC7_A    == ir1 |  //      LD (BC),A    ; 02
814
              LDs6DE7_A    == ir1 |  //      LD (DE),A    ; 12
815
              LDs6HL7_A    == ir1 |  //      LD (HL),A    ; 77
816
              LDs6NN7_A    == ir1 |  //      LD (NN),A    ; 32 XX XX
817
              PUSHsAF      == ir1 |
818
              OUTs6N7_A    == ir1 |
819
              ED_OUTs6C7_REG ==  {ir1[9:6],ir1[2:0] && REG8_A == ir1[5:3]} ;
820
 
821
wire os_b = LDs6HL7_B      == ir1                                       |  // LD (HL),B    ; 70
822
            ED_LDs6NN7_REG == {ir1[9:6],ir1[3:0]} & DBL_REG_BC == ir1[5:4] |
823
            ED_OUTs6C7_REG ==  {ir1[9:6],ir1[2:0]} & REG8_B == ir1[5:3] ;
824
 
825
wire os_c = LDs6HL7_C    == ir1                                         |  //      LD (HL),C    ; 71
826
            PUSHsBC        == ir1                                       |  // PUSH BC
827
            ED_OUTs6C7_REG ==  {ir1[9:6],ir1[2:0]} & REG8_C == ir1[5:3] ;
828
 
829
wire os_d = LDs6HL7_D    == ir1                                         |  //      LD (HL),D    ; 72
830
            ED_LDs6NN7_REG == {ir1[9:6],ir1[3:0]} & DBL_REG_DE == ir1[5:4] |
831
            ED_OUTs6C7_REG ==  {ir1[9:6],ir1[2:0]} & REG8_D == ir1[5:3] ;
832
 
833
 
834
wire os_e = LDs6HL7_E    == ir1                                     |  //      LD (HL),E    ; 73
835
            PUSHsDE      == ir1                                         |  //      PUSH DE
836
            ED_OUTs6C7_REG ==  {ir1[9:6],ir1[2:0]} & REG8_E == ir1[5:3] ;
837
 
838
wire os_h = LDs6HL7_H    == ir1                                         |  //      LD (HL),H    ; 74
839
            LDs6NN7_HL   == ir1                                         |  //      LD (NN),HL   ; 22 XX XX
840
            ED_LDs6NN7_REG == {ir1[9:6],ir1[3:0]} & DBL_REG_HL == ir1[5:4] |
841
            ED_OUTs6C7_REG ==  {ir1[9:6],ir1[2:0]} & REG8_H == ir1[5:3] ;
842
 
843
wire os_l = LDs6HL7_L    == ir1                                     |  //      LD (HL),L    ; 75
844
            PUSHsHL      == ir1                                     |
845
            ED_OUTs6C7_REG ==  {ir1[9:6],ir1[2:0]} & REG8_L == ir1[5:3] ;
846
 
847
wire os_sp = ED_LDs6NN7_REG == {ir1[9:6],ir1[3:0]} & DBL_REG_SP == ir1[5:4];
848
 
849
wire os_f  =  PUSHsAF     == ir1 ;
850
 
851
 
852
//---------------- inst hazard ----------------------------------------------------------
853
//
854
// On some reflection, I don't think I'm going to worry about this immediately - it 
855
// should be easy to kludge in a fix if necessary  -- and there are more important things
856
// todo.  It is a very bad programming practice to muck with the instruction stream in any
857
// case --  I have to believe most target applications do not do this -- although I'll probably
858
// get hit pretty early with a instruction test that does.   Oh well  -- if that happens we fix
859
// it.   
860
// Well --  think some here --  the hazard is because of a change in design. 
861
//  If used to any extent..  Somebody WILL
862
//  want this to act the same way as the origional - even if the programming is "poor".
863
//  >>>>>>>> bite the bullet and do it.
864
//
865
// if we do an operand store and the address == pc-1 its an inst hazard, We need to execute the 
866
// store decrement pc and re-fetch.  This is a high priority interrupt. 
867
// what about multi-byte stores  - like LDs6NN7_A  or LDs6NN7_HL - i guess we  do an IF - to start
868
// the pipe before the os -- same logic.   
869
// 
870
 
871
 
872
//-----------------data hazard ----------------------------------------------------------
873
//
874
// Issues here have evolved to a degree as the design progressed.  However the 
875
// Key has always been that for each instruction (no matter how complex) there 
876
// is only a single state in which the previous instruction can also be active
877
// and that is the DEC_EXEC state.  If there is a data hazard, we need to delay
878
// execution of that state until the ir2 execution completes (which it always does
879
// in a single tick).  Note that only the RET instructions test the flag register
880
// on DEC_EXEC.
881
//
882
// WARNING:  be very careful about this.  Data hazard logic is very difficult to 
883
// verify as there are so many instruction pairs to test.
884
//
885
//  Situations  1) operand stores from ir1 when register is updated in ir2
886
//              2) flag tests when fr is being updated
887
//              3) sp issues  see below  LDsSP_HL  DECsSP  INCsSP
888
//     ANY OTHERS ???
889
// 
890
// upd_ar, upd_br, upd_cr, upd_dr, upd_er, upd_hr, upd_lr,upd_fr,
891
wire  use_hl_exec =  LDsSP_HL == ir1;
892
wire  use_sp_exec =  MEM_OFSP == next_mem_state |
893
                     MEM_OSSP == next_mem_state  ;
894
wire  upd_sp_exec  = DECsSP == ir2 |
895
                     INCsSP == ir2   ;
896
 
897
 
898
 
899
wire use_fr_exec = ( RETsC        == ir1  |
900
                     RETsM        == ir1  |
901
                     RETsNC       == ir1  |
902
                     RETsP        == ir1  |
903
                     RETsPE       == ir1  |
904
                     RETsPO       == ir1  |
905
                     RETsNZ       == ir1  |
906
                     RETsZ        == ir1   ) ;
907
 
908
assign hazard =  (dec_state == DEC_EXEC  & exec_ir2 ) & ( upd_fr & use_fr_exec  |
909
                                                          upd_ar & os_a         |
910
                                                          upd_br & os_b         |
911
                                                          upd_cr & os_c         |
912
                                                          upd_dr & os_d         |
913
                                                          upd_er & os_e         |
914
                                                          upd_hr & os_h         |
915
                                                          upd_lr & os_l         |
916
                                                          upd_hr & use_hl_exec  |
917
                                                          upd_lr & use_hl_exec  |
918
                                                          upd_sp_exec & use_sp_exec );
919
 
920
 
921
 
922
 
923
 
924
 
925
// does not include extension stuff as we are mostly looking for hazards here
926
// course we do use these terms to build more decodes
927
//
928
wire  opadr_bc  =  LDsA_6BC7  == ir1 | LDs6BC7_A == ir1;
929
wire  opadr_de  =  LDsA_6DE7  == ir1 | LDs6DE7_A == ir1;
930
wire  opadr_hl  =  LDsB_6HL7  == ir1 | ORs6HL7    == ir1 | LDs6HL7_B == ir1 |
931
                   LDsD_6HL7  == ir1 | LDsC_6HL7  == ir1 | LDs6HL7_C == ir1 |
932
                   LDsH_6HL7  == ir1 | LDsE_6HL7  == ir1 | LDs6HL7_D == ir1 |
933
                   ADDsA_6HL7 == ir1 | LDsL_6HL7  == ir1 | LDs6HL7_E == ir1 |
934
                   SUBs6HL7   == ir1 | LDsA_6HL7  == ir1 | LDs6HL7_H == ir1 |
935
                   ANDs6HL7   == ir1 | ADCsA_6HL7 == ir1 | LDs6HL7_L == ir1 |
936
                   XORs6HL7   == ir1 | SBCs6HL7   == ir1 | CPs6HL7   == ir1 ;
937
 
938
assign  use_a = os_a;
939
assign  use_b = os_b  | opadr_bc;
940
assign  use_c = os_c  | opadr_bc;
941
assign  use_d = os_d  | opadr_de;
942
assign  use_e = os_e  | opadr_de;
943
assign  use_h = os_h  | opadr_hl;
944
assign  use_l = os_l  | opadr_hl;
945
 
946
 
947
 
948
assign   use_flags = c_jmp8 | c_jmp4 | c_call | c_ret;
949
 
950
 
951
 
952
wire bc_eq0 = beq0 & ceq0;
953
//  ???  not used ?  why defined ?
954
//assign rpt_blk_mv = (blk_mv_reg )  & !bc_eq0     |
955
//                    (blk_cmp_reg) & !bc_eq0 & (nn[7:0] != 8'h0)  |
956
//                    (blk_in_reg | blk_out_reg) & !b_eq0 ;
957
 
958
 
959
 
960
 
961
 
962
 
963
 
964
//  BASIC ARCHITECTURE OF THIS FILE   pc  and sp not shown, but are inputs to src mux.
965
//                    _____           and may be updated from adder output.
966
//                   |     |
967
//                   |     |          pc-1 register is required to implement relative jumps.
968
//                   |     |                     
969
//      _____        |lit  |      |\             
970
//     |     |       |     |      |  \           
971
//     |     |       |src2 |      |    \          _____          _____ 
972
//     |     |       |     |----->|     |        |     |        |     |
973
//     |src  |       |_____|      |adder|------->|     |        |     |
974
//     |mux  |                    |     |        |     |        |     |
975
//     |     |------------------->|    /         |2/1  |------->|wb   |
976
//     |     |              |     |  /           |mux  |        |adr  |
977
//     |_____|              |     |/             |     |        |     |
978
//                           ------------------->|     |        |     |
979
//                                               |_____|        |_____|
980
//  MEM_NOP  
981
//  MEM_IFPP1   MEM_OFIXpD     MEM_CALL    MEM_IFRST     MEM_OFHL_PM    MEM_IOF_C  
982
//  MEM_OS1,    MEM_OSIXpD     MEM_OSNN,   MEM_IFREL_N   MEM_OSHL_PM    MEM_IOS_C  
983
//  MEM_OF1,    MEM_OSADR      MEM_OFNN    MEM_JMPHL     MEM_OSDE_PM    MEM_IOF_N  
984
//  MEM_OFSP    MEM_OSSP_PCM2  MEM_OFADRP1 MEM_IFNN      MEM_INTA       MEM_IOS_N  
985
//  MEM_OSSP    MEM_OSSP_P     MEM_OSADRP1 MEM_IFINT     MEM_OS_HL_N
986
//                                                       
987
 
988
wire src_sp = next_mem_state == MEM_OF1  & EXs6SP7_HL == ir1 | //special case rmw 
989
              next_mem_state == MEM_OFSP                     |
990
              next_mem_state == MEM_OSSP                     |
991
              next_mem_state == MEM_CALL                       ;
992
wire src_pc =  next_mem_state ==   MEM_IFPP1   |
993
               next_mem_state ==  MEM_IFREL_N  ;
994
 
995
wire src_nn =  next_mem_state ==   MEM_IFNN |
996
               next_mem_state ==   MEM_OSNN |
997
               next_mem_state ==   MEM_OFNN  ;
998
 
999
 
1000
wire src_de  = dec_state == DEC_EXEC & LDsA_6DE7 == ir1  |      // MEM_OS1  MEM_OF1
1001
               dec_state == DEC_EXEC & LDs6DE7_A == ir1  |     // are both true at this time
1002
               next_mem_state == MEM_OSDE_PM               ;
1003
wire src_bc =  dec_state == DEC_EXEC & LDsA_6BC7 == ir1  |
1004
               dec_state == DEC_EXEC & LDs6BC7_A == ir1  |
1005
               next_mem_state ==MEM_IOF_C                |
1006
               next_mem_state ==MEM_IOS_C                 ;
1007
 
1008
 
1009
//  don't forget that hl source can be modified by prefix
1010
//  this gets messy as we use wb_adr for some of these.
1011
//
1012
wire src_hl =   next_mem_state == MEM_OF1  &
1013
                                  (dec_state == DEC_EXEC)  &
1014
                                   !src_de & !src_bc & !src_sp  |
1015
                next_mem_state == MEM_OS1  &
1016
                                  (dec_state == DEC_EXEC)  &
1017
                                   !src_de & !src_bc         |
1018
                next_mem_state == MEM_OFHL_PM                |
1019
                next_mem_state == MEM_OSHL_PM                |
1020
                next_mem_state == MEM_OS_HL_N                |
1021
                next_mem_state == MEM_JMPHL                   ;
1022
 
1023
wire src_ix =  next_mem_state == MEM_OFIXpD  &  ir1dd |
1024
               next_mem_state == MEM_OSIXpD  &  ir1dd  ;
1025
 
1026
wire src_iy =  next_mem_state == MEM_OFIXpD  &  ir1fd |
1027
               next_mem_state == MEM_OSIXpD  &  ir1fd  ;
1028
 
1029
wire src_adr = next_mem_state == MEM_OFADRP1  |
1030
               next_mem_state == MEM_OSADRP1  |
1031
               next_mem_state == MEM_NOP      |
1032
               next_mem_state == MEM_OSADR     ;
1033
 
1034
wire src_int = next_mem_state == MEM_IOF_N  |
1035
               next_mem_state == MEM_IOS_N   ;
1036
 
1037
 
1038
 
1039
wire   src_mux =   {16{ src_sp  }} & sp                 |
1040
                   {16{ src_pc  }} & pc                 |
1041
                   {16{ src_nn  }} & nn                 |
1042
                   {16{ src_hl  }} & hl                 |
1043
                   {16{ src_de  }} & de                 |
1044
                   {16{ src_bc  }} & bc                 |
1045
                   {16{ src_ix  }} & ixr                |
1046
                   {16{ src_iy  }} & iyr                |
1047
                   {16{ src_adr }} & wb_adr             |
1048
                   {16{ src_int }} & { intr, nn[15:8] } |
1049
                   {16{next_mem_state == MEM_IFRST}} & {10'h0, ir1[6:4], 3'h0} ;
1050
 
1051
wire block_mv_inc = (dec_state == DEC_ED) ? dec_blk_inc : blk_inc_flg; // flag set at DEC_ED
1052
 
1053
 
1054
 
1055
wire inc_s2 =     next_mem_state ==MEM_OFADRP1                |
1056
                  next_mem_state ==MEM_OSADRP1                |
1057
                  next_mem_state ==MEM_OFHL_PM & block_mv_inc |
1058
                  next_mem_state ==MEM_OSHL_PM & block_mv_inc |
1059
                  next_mem_state ==MEM_OSDE_PM & block_mv_inc |
1060
                  next_mem_state ==MEM_OFSP                   |
1061
                  next_mem_state ==MEM_IFPP1                  |
1062
                  next_mem_state ==MEM_OSSP_PCM2              |
1063
                  next_mem_state ==MEM_OSSP_P                  ;
1064
 
1065
wire dec_s2 =     next_mem_state ==MEM_OFHL_PM & ~block_mv_inc |
1066
                  next_mem_state ==MEM_OSHL_PM & ~block_mv_inc |
1067
                  next_mem_state ==MEM_OSDE_PM & ~block_mv_inc |
1068
                  next_mem_state == MEM_OFSP                    ;
1069
 
1070
 
1071
wire reln_s2 =    next_mem_state ==  MEM_IFREL_N   |
1072
                  next_mem_state ==  MEM_OFIXpD    |
1073
                   next_mem_state ==  MEM_OSIXpD    ;
1074
 
1075
wire   src2    = {16{ inc }}  & 16'h0001           |
1076
                 {16{ dec }}  & 16'hffff           |
1077
                 {16{ rel }}  & {{8{nn[15]}},nn[15:8]}|
1078
                 {16{~(rel_jmp|inc|dec)}} & 16'h0   ;
1079
 
1080
wire   adr_alu     = src2 + src_mux;
1081
 
1082
 
1083
wire  pre_inc_dec =    next_mem_state ==  MEM_CALL    |
1084
                       next_mem_state ==  MEM_OSSP_P  |
1085
                       next_mem_state ==  MEM_OSSP     ;
1086
 
1087
 
1088
wire   mux21 =  pre_inc_dec ? alu : src_mux;
1089
 
1090
assign wb_rdy_nhz = (!wb_cyc | wb_ack ) & ~hazard;   //  wishbone ready with no hazard
1091
wire   wb_rdy     = !wb_cyc | wb_ack;
1092
 
1093
assign we_next = next_mem_state == MEM_OS1        |
1094
                 next_mem_state == MEM_OSP        |
1095
                 next_mem_state == MEM_OSIXpD     |
1096
                 next_mem_state == MEM_OSADR      |
1097
                 next_mem_state == MEM_OSSP_PCM2  |
1098
                 next_mem_state == MEM_OSSP_P     |
1099
                 next_mem_state == MEM_CALL       |
1100
                 next_mem_state == MEM_OSNN       |
1101
                 next_mem_state == MEM_OSADRP1    |
1102
                 next_mem_state == MEM_OSHL_PM    |
1103
                 next_mem_state == MEM_OSDE_PM    |
1104
                 next_mem_state == MEM_OS_HL_N    |
1105
                 next_mem_state == MEM_IOS_C      |
1106
                 next_mem_state == MEM_IOS_N       ;
1107
 
1108
 
1109
//-------1---------2---------3--------State Machines-------6---------7---------8---------9--------0
1110
// we do this just to save virtual paper below.
1111
//              6              5              4                15
1112
assign {next_dec_state, next_mem_state, next_pipe_state} = next_state;
1113
 
1114
always @(ir1 or wb_int or inst_haz or wb_int or dec_state or mem_exec_dec or cb_mem or ed_nn or
1115
         ed_blk_cp  or ed_blk_in or ed_blk_out or ed_retn or ed_blk_mv or ed_dbl_rd or blk_done or
1116
         fr or jmpr_true or callnn_true or jmpnn_true )
1117
 
1118
begin
1119
    case (dec_state)
1120
        DEC_IDLE:       next_state = {DEC_IF1, MEM_NOP, IPIPE_NOP};
1121
 
1122
        DEC_HALT:
1123
            if (wb_int)      next_state = {DEC_INT1,MEM_NOP   ,IPIPE_NOP};// stay here until interrupt or reset
1124
            else             next_state = {DEC_HALT,MEM_NOP   ,IPIPE_NOP};
1125
        DEC_IF1 :            next_state = {DEC_IF2 ,MEM_IFPP1 ,IPIPE_NOP};
1126
        DEC_IF2 :            next_state = {DEC_EXEC,MEM_IFPP1 ,IPIPE_EN1};
1127
        DEC_IF2A:            next_state = {DEC_EXEC,MEM_IFPP1 ,IPIPE_NOP};
1128
        DEC_EXEC:
1129
            if      (inst_haz)    next_state = {DEC_IF1, MEM_DECPC , IPIPE_NOP};
1130
            else if (wb_int)      next_state = {DEC_INT1,MEM_NOP   ,IPIPE_NOP};
1131
            else
1132
                case (mem_exec_dec) // full case but can all tools understand ? just make a default
1133
                I1_CB   : next_state = {DEC_CB,   MEM_IFPP1, IPIPE_EN1};// IF2_NOP -> nn <= (MEM)
1134
                I1_DDFD : next_state = {DEC_DDFD, MEM_IFPP1, IPIPE_EN1};// gets real inst     
1135
                I1_ED   : next_state = {DEC_ED,   MEM_IFPP1, IPIPE_EN1};
1136
                I1_JMP  : next_state = {DEC_IF2,  MEM_JMPHL, IPIPE_NOP};
1137
                I1_N    : next_state = {DEC_N,    MEM_IFPP1, IPIPE_ENN};
1138
                I1_NN   : next_state = {DEC_NN,   MEM_IFPP1, IPIPE_ENN};
1139
                I1_OF   : next_state = {DEC_OF,   MEM_OF1,   IPIPE_EN12};//transfer, don't activate
1140
                I1_OS   : next_state = {DEC_IF2,  MEM_OS1,   IPIPE_EN1}; // -> ir2_NOP
1141
                I1_POP  : next_state = {DEC_POP,  MEM_OFSP,  IPIPE_EN12};
1142
                I1_PUSH : next_state = {DEC_PUSH, MEM_OSSP,  IPIPE_EN12};
1143
                I1_RET  : next_state = {DEC_RET,  MEM_OFSP,  IPIPE_EN12};
1144
                I1_RMW  : next_state = {DEC_RMW,  MEM_OF1,   IPIPE_EN12};//can't activate till data rdy
1145
                I1_RST  : next_state = {DEC_IF2,  MEM_IFRST, IPIPE_ENN};
1146
                I1_R2R  : next_state = {DEC_EXEC, MEM_IFPP1, IPIPE_EN12A2};
1147
                default : next_state = {DEC_EXEC, MEM_IFPP1, IPIPE_EN12A2}; //I1_R2R  
1148
                endcase
1149
        DEC_CB: if (cb_mem) next_state = {DEC_CBM, MEM_OF1, IPIPE_EN12};
1150
                else        next_state = {DEC_EXEC, MEM_IFPP1, IPIPE_EN12A2};
1151
        DEC_DDFD:   // except for CB and EB these all act the same H and L get modified by prefix
1152
            case (mem_exec_dec)
1153
            I1_CB   : next_state = {DEC_PFxCB,MEM_IFPP1, IPIPE_EN1};// IF2_NOP -> nn <= (MEM)
1154
            I1_DDFD : next_state = {DEC_DDFD, MEM_IFPP1, IPIPE_EN1};
1155
            I1_ED   : next_state = {DEC_ED,   MEM_IFPP1, IPIPE_EN1};//How do we clear the prefix?
1156
            I1_JMP  : next_state = {DEC_IF2,  MEM_JMPHL, IPIPE_NOP};
1157
            I1_N    : next_state = {DEC_N,    MEM_IFPP1, IPIPE_ENN};
1158
            I1_NN   : next_state = {DEC_NN,   MEM_IFPP1, IPIPE_ENN};
1159
            I1_OF   : next_state = {DEC_DDOF, MEM_IFPP1, IPIPE_ENN};  // d to nn - need to get d
1160
                                                                      // LD A,(BC) LD A,(DE) will
1161
                                                                      // become ix+d - do we care ?
1162
                                                                      // i hope not
1163
            I1_OS   : next_state = {DEC_DDOS, MEM_IFPP1, IPIPE_ENN};  // d to nn
1164
            I1_POP  : next_state = {DEC_POP,  MEM_OFSP,  IPIPE_EN12};
1165
            I1_PUSH : next_state = {DEC_PUSH, MEM_OSSP,  IPIPE_EN12};
1166
            I1_RET  : next_state = {DEC_RET,  MEM_OFSP,  IPIPE_EN12};
1167
            I1_RMW  : next_state = {DEC_RMW,  MEM_OF1,  IPIPE_EN12};
1168
            I1_RST  : next_state = {DEC_IF2,  MEM_IFRST, IPIPE_NOP};  // just dump next inst
1169
            I1_R2R  : next_state = {DEC_EXEC, MEM_IFPP1, IPIPE_EN12A2}; //I1_R2R
1170
            default : next_state = {DEC_EXEC, MEM_IFPP1, IPIPE_EN12A2}; //I1_R2R  
1171
            endcase
1172
        DEC_ED:
1173
            if (ed_nn)            next_state = {DEC_EDNN1,  MEM_IFPP1,   IPIPE_ENN};
1174
            // we need to set inc and io and repeat flags on this state for continued block
1175
            // processing  --   keep the states of this machine somewhat manageable.
1176
            else if (ed_blk_cp )  next_state = {DEC_EDBCP1, MEM_OFHL_PM, IPIPE_EN12};// MEM_OFHL_PM triggers --BC
1177
            else if (ed_blk_in )  next_state = {DEC_EDBIN1, MEM_IOF_C,   IPIPE_EN12};// MEM_IOF_C triggers --B
1178
            else if (ed_blk_out)  next_state = {DEC_EDBOUT1,MEM_OFHL_PM, IPIPE_EN12};
1179
            else if (ed_blk_mv )  next_state = {DEC_EDBMV1, MEM_OFHL_PM, IPIPE_EN12};
1180
            else if (ed_retn   )  next_state = {DEC_RET,    MEM_OFSP,    IPIPE_EN12};// see int logic below
1181
            else                  next_state = {DEC_EXEC, MEM_IFPP1,    IPIPE_EN12A2};
1182
                   // double register reads and writes here    
1183
        DEC_EDNN1:                next_state = {DEC_EDNN2, MEM_NOP,     IPIPE_ENN}; // address to nn
1184
        DEC_EDNN2:
1185
            if (ed_dbl_rd)      next_state = {DEC_EDRD1, MEM_OFNN,    IPIPE_NOP};
1186
            else                next_state = {DEC_EDWR,  MEM_OSNN,    IPIPE_NOP};// OSNN selects data ok?                  
1187
        DEC_EDRD1:              next_state = {DEC_EDRD2, MEM_OFADRP1,  IPIPE_ENN};  // 1st byte 2n         
1188
        DEC_EDRD2:              next_state = {DEC_IF2,   MEM_IFPP1,   IPIPE_ENNA2}; // 2nd byte 2nn
1189
        DEC_EDWR:               next_state = {DEC_IF1,   MEM_OSADRP1,  IPIPE_NOP};
1190
 
1191
        //  ED  block moves
1192
        DEC_EDBCP1:
1193
            if (blk_done)   next_state = {DEC_EXEC, MEM_IFPP1,IPIPE_ENNA2};
1194
            else if(wb_int) next_state = {DEC_INT1, MEM_NOP, IPIPE_ENNA2};
1195
            else            next_state = {DEC_EDBCP2, MEM_NOP,  IPIPE_ENNA2};//set flags 
1196
        DEC_EDBCP2:                 next_state = {DEC_EDBCP3, MEM_NOP,     IPIPE_NOP};//wait for fr. alu_out is slow 
1197
        DEC_EDBCP3: if (fr[7])      next_state = {DEC_EXEC  , MEM_IFPP1,   IPIPE_NOP};
1198
                    else            next_state = {DEC_EDBCP1, MEM_OFHL_PM, IPIPE_NOP};
1199
 
1200
        DEC_EDBIN1:                  next_state = {DEC_EDBIN2, MEM_NOP,   IPIPE_ENN};
1201
        DEC_EDBIN2: if (blk_done)    next_state = {DEC_IF2A,  MEM_OSHL_PM,IPIPE_NOP}; // implies nn
1202
                    else if (wb_int) next_state = {DEC_INT1,  MEM_OSHL_PM,IPIPE_NOP};
1203
                    else             next_state = {DEC_EDBIN1,MEM_OSHL_PM,IPIPE_NOP};//set flags 
1204
        DEC_EDBIN3:                  next_state = {DEC_EDBIN1, MEM_IOF_C,   IPIPE_NOP};
1205
 
1206
        DEC_EDBOUT1:                 next_state = {DEC_EDBOUT2, MEM_NOP,   IPIPE_ENN};
1207
        DEC_EDBOUT2:if (blk_done)    next_state = {DEC_EXEC,  MEM_IOS_C,IPIPE_NOP};
1208
                    else if (wb_int) next_state = {DEC_INT1,  MEM_IOS_C,IPIPE_NOP}; // DEC_EDBOUT: if (blk_rpt)
1209
                    else             next_state = {DEC_EDBOUT3,MEM_IOS_C,IPIPE_NOP};
1210
 
1211
        DEC_EDBOUT3:                 next_state = {DEC_EDBOUT1,MEM_OFHL_PM, IPIPE_NOP};
1212
 
1213
        DEC_EDBMV1:                  next_state = {DEC_EDBMV2, MEM_NOP,   IPIPE_ENN};
1214
        DEC_EDBMV2: if (blk_done)    next_state = {DEC_EXEC,  MEM_OSDE_PM,IPIPE_NOP};
1215
                    else if (wb_int) next_state = {DEC_INT1,  MEM_OSDE_PM,IPIPE_NOP}; //DEC_EDBOUT: if (blk_rpt)
1216
                    else             next_state = {DEC_EDBMV3,MEM_OSDE_PM,IPIPE_NOP};
1217
 
1218
        DEC_EDBMV3:                  next_state = {DEC_EDBMV1,MEM_OFHL_PM, IPIPE_NOP};
1219
 
1220
        DEC_N:
1221
            if (INsA_6N7== ir1)      next_state = {DEC_NIN,  MEM_IOF_N, IPIPE_EN12};
1222
            else if (OUTs6N7_A==ir1) next_state = {DEC_IF1,  MEM_IOS_N, IPIPE_EN12};
1223
            else if (LDs6HL7_N==ir1) next_state = {DEC_IF1,  MEM_OS_HL_N, IPIPE_EN12};
1224
            else if (jmpr_true)      next_state = {DEC_IF2,  MEM_IFREL_N, IPIPE_NOP};
1225
            else                     next_state = {DEC_EXEC, MEM_IFPP1,  IPIPE_EN12A2};//r2r or false jumps
1226
        DEC_NIN:                     next_state = {DEC_IF2,  MEM_IFPP1,    IPIPE_ENNA2};
1227
 
1228
 
1229
        //ISSUES: LDsSP_NN - load commanded from ir2 decode?  and mechaninsm for updating PC on
1230
        //        JMP and CALL
1231
        //  on CALL   We have IFNN for JMP  
1232
        //   For CALL  Use MEM_CALL to transfer pc<=nn, nn<=pc, adr<=sp then MEM_OSSP then IFPP1
1233
        //   For  LDsSP_NN  yes  update from ir2 decode.                    
1234
        DEC_NN:
1235
            if      (callnn_true)     next_state = {DEC_NNCALL1, MEM_NOP, IPIPE_ENN}; // this gets new adr in nn
1236
                                                                                         // if we store from nn we can't do
1237
                                                                                         // a mem op now
1238
 
1239
            else if (jmpnn_true)      next_state = {DEC_NNJMP,  MEM_NOP,  IPIPE_ENN};    // gotta get nn before we can 
1240
                                                                                         // transfer to adr.
1241
            else if (LDs6NN7_A==ir1)  next_state = {DEC_NNOS3,   MEM_IFPP1,  IPIPE_ENN};
1242
            else if (LDs6NN7_HL==ir1) next_state = {DEC_NNOS1,   MEM_IFPP1,  IPIPE_ENN};
1243
            else if (LDsA_6NN7==ir1)  next_state = {DEC_NNOF3,    MEM_IFPP1,  IPIPE_ENN};
1244
            else if (LDsHL_6NN7==ir1) next_state = {DEC_NNOF1,    MEM_IFPP1,  IPIPE_ENN};
1245
            else                      next_state = { DEC_IF2, MEM_IFPP1, IPIPE_ENNEN2A2};
1246
 
1247
        DEC_NNCALL1:        next_state = {DEC_NNCALL2, MEM_CALL ,  IPIPE_NOP};
1248
        DEC_NNCALL2:        next_state = {DEC_IF1,    MEM_OSSP,   IPIPE_ENN};//A1 activates r2r xfers from ir1
1249
        DEC_NNJMP:        next_state = {DEC_IF2,     MEM_IFNN  , IPIPE_NOP};
1250
 
1251
        // ISSUE:  we blow out ir1 here - so need to keep some status to execute OSNN2.
1252
        //  general solution  if not DEC_EXEC we get op frmo nn high byte. 
1253
        //  note that first MEM_OSNN trabsferrs nn to wb_adr.
1254
        DEC_NNOS1:           next_state = {DEC_NNOS2,   MEM_OSNN,   IPIPE_EN1};
1255
        DEC_NNOS2:           next_state = {DEC_IF2A,    MEM_OSNN,   IPIPE_NOP};
1256
        DEC_NNOS3:           next_state = {DEC_IF2A,    MEM_OSNN,   IPIPE_EN1};
1257
 
1258
        DEC_NNOF1:           next_state = {DEC_NNOF2,  MEM_OFNN, IPIPE_EN12};
1259
        DEC_NNOF2:           next_state = {DEC_NNOF4,  MEM_OFNN, IPIPE_ENN};
1260
        DEC_NNOF3:           next_state = {DEC_NNOF4,  MEM_OFNN, IPIPE_EN12};
1261
        DEC_NNOF4:           next_state = {DEC_EXEC,   MEM_IFPP1, IPIPE_ENNA2};
1262
 
1263
        DEC_DDOS:            next_state = {DEC_IF2A, MEM_OSIXpD, IPIPE_EN12};
1264
        DEC_DDOF:            next_state = {DEC_OF  , MEM_OFIXpD,  IPIPE_EN12};
1265
 
1266
 
1267
        DEC_OF:              next_state = {DEC_EXEC,  MEM_IFPP1 , IPIPE_ENNA2};
1268
        DEC_POP:             next_state = {DEC_NNOF4,  MEM_OFSP, IPIPE_ENN };
1269
        DEC_PUSH:            next_state = {DEC_IF2A ,  MEM_OSSP, IPIPE_NOP };
1270
 
1271
 
1272
        DEC_RET:             next_state = { DEC_RET2, MEM_OFSP, IPIPE_ENN };
1273
        DEC_RET2:            next_state = { DEC_NNCALL2, MEM_NOP, IPIPE_ENN };
1274
                                                                 //  blow off a tick so we don't gronk adr
1275
        DEC_RMW:             next_state = {DEC_RMW2,  MEM_NOP,   IPIPE_ENNA2}; //activate
1276
        DEC_RMW2:            next_state = {DEC_IF1 ,  MEM_OSADR, IPIPE_NOP }; // from nn
1277
 
1278
 
1279
        //  IF memory -- rmw  else these are all reg 2 reg
1280
        DEC_CBM: if (CB_BIT==ir1[9:6]) next_state = {DEC_IF2, MEM_IFPP1,   IPIPE_ENNA2};
1281
                 else                 next_state = {DEC_RMW2 ,  MEM_NOP,  IPIPE_ENNA2};
1282
 
1283
        // The DDCB anf FDCB all assume memory operands 
1284
        // These beauties always rmw memory.  If a register op is default, they also 
1285
        // update the register.  Programmers think of this as 2 ops for the price of 1.
1286
        // unfortunately it is 2 ops for the price of 4.-- its not the number of lines 
1287
        // of assembler code that count but the number of bytes assembled. Oh well I signed
1288
        // up for this......  and had a notion of what I was getting into.
1289
        //
1290
        DEC_PFxCB:     next_state = { DEC_PFxCB2, MEM_IFPP1,  IPIPE_ENN}; // this gets d
1291
        DEC_PFxCB2:    next_state = { DEC_PFxCB3, MEM_OFIXpD, IPIPE_EN1}; //actual inst 
1292
        DEC_PFxCB3:    next_state = { DEC_PFxCB4, MEM_IFPP1,  IPIPE_ENNEN2A2};
1293
        DEC_PFxCB4:    next_state = { DEC_IF2A,   MEM_OSADR,  IPIPE_EN1};  //execute ir2
1294
 
1295
        //  crap   gotta subtract 2  (we always increment pc 2 times relative to the inst
1296
        //  that got interrupted. also can't push and dec pc without 2 adders.
1297
        //  choices:  1) fix up pc in 2 ticks 2) fix in 1 tick 3) add adder and do it fast
1298
        //   if there's anyone who knows is there anyone who cares.   
1299
        //   guess I'll do it fast  --   just a 16 bit subtractor.  heck silicon is 
1300
        //   cheap.  
1301
        DEC_INT1:       next_state <= {DEC_INT2, MEM_OSSP_PCM2, IPIPE_NOP};   //must derement PC
1302
        DEC_INT2:       next_state <= {DEC_INT3, MEM_OSSP_P,   IPIPE_NOP};    //must dec sp and PC  2 ops?
1303
        DEC_INT3:       next_state <= {DEC_INT4, MEM_INTA,     IPIPE_NOP};
1304
        DEC_INT4:       next_state <= {DEC_INT5, MEM_NOP,      IPIPE_ENN};
1305
        DEC_INT5:       next_state <= {DEC_IF2,  MEM_IFINT,    IPIPE_NOP};
1306
        default:        next_state <= {DEC_IDLE, MEM_NOP,      IPIPE_NOP};
1307
    endcase
1308
end
1309
 
1310
 
1311
always @(posedge clk or posedge rst)
1312
    if (rst) dec_state <= DEC_IDLE;
1313
    else   if (wb_rdy_nhz )   dec_state <= next_dec_state;
1314
 
1315
 
1316
//-----------------------instruction register #1 ----------------------------------
1317
//  //         next_pipe_state         {ir1,ir2,nn,act_ir2}
1318
 
1319
wire update_prefix =   dec_state == DEC_EXEC  | dec_state == DEC_DDFD;
1320
 
1321
always @(posedge clk or posedge rst)
1322
    if (rst) ir1 <=   NOP;
1323
    else if (wb_rdy_nhz & next_pipe_state[3]) ir1 <=  {2'b0, wb_dat_i} ;
1324
    else if ( wb_rdy_nhz &update_prefix )     ir1 <=  {ir1[7:0]==8'hed, ir1[7:0]==8'hcd, ir1[7:0]};
1325
 
1326
//----------- prefix states -----------------------------------------
1327
//  strings of prefix insts are ignored up to last one.  Also dded and fded are ignored 
1328
//  but ddcd and fdcd are defined prefix sets.
1329
//
1330
always @(posedge clk)
1331
    if  (wb_rdy_nhz & next_pipe_state[3]) {ir1dd, ir1fd } <= 2'b0;
1332
    else if ( wb_rdy_nhz & update_prefix )
1333
        {ir1dd, ir1fd } <= {ir1dd | (ir1[7:0]==8'hdd ) & (ir1[7:0]!=8'hed) & (ir1[7:0]!=8'hfd),
1334
                            ir1fd | (ir1[7:0]==8'hfd ) & (ir1[7:0]!=8'hed) & (ir1[7:0]!=8'hdd) };
1335
 
1336
//------------------- inst reg #2 -----------------------------------
1337
//  This stuff is key to the data hazard logic.  Hazards arise only AFTER activation of 
1338
//  a previous instruction.  Fundamentally all state changes related to ir1 may be 
1339
//  delayed eithor by a delay in wb response, or by a hazard.  Ir2 state changes
1340
//  are keyed off exec_ir2 - and always happen immediately.  ( exec_ir2 always is 
1341
//  immediately reset - unless of course a new instruction is transferred and executed.
1342
//
1343
// 
1344
//
1345
always @(posedge clk or posedge rst)
1346
    if (rst) ir2 <= 10'h0;
1347
    else if (wb_rdy_nhz & next_pipe_state[2]) ir2 <= ir1;
1348
 
1349
always @(posedge clk or posedge rst)
1350
    if (rst)
1351
    begin
1352
        ir2dd <= 1'b0;
1353
        ir2fd <= 1'b0;
1354
    end
1355
    else if (wb_rdy_nhz & next_pipe_state[2])
1356
    begin
1357
        ir2dd <= ir1dd;
1358
        ir2fd <= ir1fd;
1359
    end
1360
 
1361
always @(posedge clk )
1362
    if (wb_rdy_nhz & next_pipe_state[0]) exec_ir2 <= 1'b1;
1363
    else                                 exec_ir2 <= 1'b0;
1364
 
1365
 
1366
 
1367
 
1368
 
1369
//--------------- block move flags ------------------------
1370
always @(posedge clk)
1371
    if (dec_state == DEC_ED) blk_inc_flg <= dec_blk_inc;
1372
 
1373
always @(posedge clk)
1374
    if (dec_state == DEC_ED) blk_rpt_flg <= dec_blk_rpt;
1375
 
1376
 
1377
always @(posedge clk)
1378
    if (dec_state == DEC_ED) blk_io_flg <= dec_blk_io;
1379
 
1380
 
1381
//-------------------------- memory interface stuff ----------------------------
1382
 
1383
 
1384
// --  wb_adr
1385
always @(posedge clk) if (wb_rdy) wb_adr <= mux21;
1386
 
1387
// --  wb_we; 
1388
 
1389
always @(posedge clk or posedge rst)
1390
    if (rst)         wb_we <= 1'b0;
1391
    else if (wb_rdy_nhz) wb_we <= we_next;
1392
 
1393
 
1394
 
1395
// --  wb_cyc
1396
 
1397
wire no_wb_start = mem_idle | mem_halt | mem_op3 & blk_cmp_reg | mem_op1 & rmw_reg;
1398
always @(posedge clk or posedge rst)
1399
    if (rst)         wb_cyc <= 1'b0;
1400
    else if (wb_rdy_nhz) wb_cyc <= next_mem_state != MEM_NOP ;
1401
 
1402
// --  wb_stb; 
1403
 
1404
always @(posedge clk or posedge rst)
1405
    if (rst)         wb_stb <= 1'b0;
1406
    else if (wb_rdy_nhz) wb_stb <= next_mem_state != MEM_NOP ;
1407
 
1408
 
1409
// --  wb_lock  lets not worry about lock unless somebody thinks it matters.
1410
 
1411
// --  wb_tga_io
1412
always @(posedge clk or posedge rst)
1413
    if (rst)         wb_tga_io <= 2'b0;
1414
    else if (wb_rdy_nhz)
1415
    begin
1416
        if (next_mem_state == MEM_IOF_C |
1417
            next_mem_state == MEM_IOS_C |
1418
            next_mem_state == MEM_IOF_N |
1419
            next_mem_state == MEM_IOS_N     ) wb_tga_io <= TAG_IO;
1420
 
1421
        else if (next_mem_state == MEM_INTA ) wb_tga_io <= TAG_INT;
1422
        else                                  wb_tga_io <= 2'b0   ;
1423
    end
1424
 
1425
//------------ the input-output data register  (nn) -----------------------------------------
1426
//  basicaly we store lsb's folowed by msb's 
1427
//  input is always to msb (of input regiser) first (if a 2 byte operand, lsb<=msb before transfer)
1428
//   this gets nn to position { msb, lsb } before we execute 2 byte transfer.
1429
//
1430
//  if we don't update - we byte swap as well as
1431
//  when we read
1432
//  IMPORTANT  We store from MSB's so that on block moves read and write from same place.
1433
//  this makes the output look somewhat bass-ackwards   but who is looking?
1434
// 
1435
//  There is probably a simpler way to do this.   Unfortunately there are a lot of 
1436
//  dependencies here.   Ill continue as planned till it proves untractable.
1437
//  Issue is that we are using ir1 to provide the op specification  --  but in general
1438
//  ir1 gets gronked before 2nd store (if it happens) -  so we need to capture both
1439
//  data first time  OSIXpD OS1    OSSP, and   MEM_OSNN
1440
//
1441
// on consideration lets make a flag  flag_firstos  that gets set on first store after
1442
// DEC_EXEC
1443
// ISSUE reads both here and in ir1 need to execute on wb_ack ? 
1444
// I recall wb_ack must stay active until a change in cycle  ?
1445
//  need to review wb spec.
1446
//
1447
//issue:  how is EXs6SP7_HL implemented  --  it is known as a rmw  - and only trick for this file is
1448
// that nn must be properly updates with ir2
1449
 
1450
always @(posedge clk or posedge rst)
1451
    if       (rst)                                   flag_os1 <= 1'b0;
1452
    else if  ((DEC_EXEC == next_dec_state) & wb_rdy) flag_os1 <= 1'b0;
1453
    else if  ( we_next  )                            flag_os1 <= 1'b1;
1454
 
1455
 
1456
wire [15:0] pc_2 = pc - 16'h2;
1457
always @(posedge clk)
1458
    if (wb_rdy_nhz)
1459
    begin
1460
        if ( we_next & flag_os1)                            nn <= { nn[7:0], nn[15:8] } ;
1461
        else if(we_next & ( next_mem_state == MEM_CALL))     nn <= {pc[7:0], pc[15:8]};
1462
        else if(we_next & ( next_mem_state == MEM_OSSP_PCM2))  nn <= {pc_2[7:0], pc_2[15:8]};
1463
        else if(EXs6SP7_HL== ir2 & ir2dd & exec_ir2)         nn <= ixr;
1464
        else if(EXs6SP7_HL== ir2 & ir2fd & exec_ir2)         nn <= iyr;
1465
        else if(EXs6SP7_HL== ir2         & exec_ir2)          nn <= hl;
1466
        // these are the general cases with ir1 providing register specification
1467
        else if(we_next & ( next_mem_state == MEM_OS1     |
1468
                            next_mem_state == MEM_OSIXpD  |
1469
                            next_mem_state == MEM_OSSP    |
1470
                            next_mem_state == MEM_OSNN     ) )
1471
            begin
1472
                 if (os_a)     nn[15:8] <= ar;
1473
                 if (os_b)     nn[15:8] <= br;
1474
                 if (os_c)     nn       <= {cr, br };  // use for PUSHsBC
1475
                 if (os_d)     nn[15:8] <= dr;
1476
                 if (os_e)     nn       <= {er, dr };  // use for PUSHsDE
1477
                 if (os_h)     nn[15:8] <= hr;
1478
                 if (os_l)     nn       <= {lr, hr };  // use for PUSHsHL
1479
                 if (os_f)     nn       <= {fr, ar };  // use for PUSHsAF
1480
            end
1481
 
1482
        else   nn  <= { wb_dat_i, nn[15:8] };
1483
    end
1484
 
1485
 
1486
 
1487
//-------------------  pc  and sp ----------------------------------------------------
1488
always @(posedge clk or posedge rst)
1489
    if (rst)   pc <= 16'h0;
1490
    else if (wb_rdy_nhz)
1491
    begin
1492
        if (next_mem_state == MEM_DECPC) pc <= pc - 16'h1;  // decrementer could perhaps be shared.
1493
        if (next_mem_state == MEM_IFPP1) pc <= adr_alu;
1494
        if (next_mem_state == MEM_CALL ) pc <= nn;         //Use MEM_CALL to exchange pc<=>nn
1495
        if (next_mem_state == MEM_IFRST) pc <= src_mux;
1496
        if (next_mem_state == MEM_JMPHL) pc <= src_mux;
1497
        if (next_mem_state == MEM_IFNN ) pc <= src_mux;
1498
        if (next_mem_state == MEM_IFINT) pc <= src_mux;
1499
    end
1500
 
1501
//---------------------------------- sp -----------------------------------------------------
1502
//
1503
// with pc updates are always made from ir1  as the PC is so critical to instruction flow.
1504
// (this of course creates the possibility of an "inst_hazard" - where data is stored in an 
1505
//   instruction already fetched - see below)
1506
// with sp the situation is not so simple. 
1507
// Issues - especially regarding hazards.  
1508
//
1509
//     LDsSP_NN     this should be done from ir2 - no hazard as active state is ALWAYS IF2
1510
//                
1511
//     ADDsHL_SP    The add is a pre-add so sp cannot be modified before inst is executed from ir2
1512
//     DECsSP       Just do it with ir1 at DEC_EXEC   gotcha need -- IFPP1 in general use ir2 -> hazard
1513
//     EXs6SP7_HL    rmw - no change to sp - no issue here
1514
//     INCsSP       Just do it with ir1 at DEC_EXEC          gotcha  -- IFPP1  use ir2 -> hazard
1515
//     LDsSP_HL     do from ir1 and use standard hazard logic  (if H or L is being 
1516
//                    updated -- wait)
1517
//       
1518
//     ED_LDs6NN7_REG   REG== SP     // needs to be done from ir2
1519
//     ED_LDsREG_6NN7   REG== SP     //  do from ir2 - no hazard as executed on IF2 - refill pipe
1520
 
1521
always @(posedge clk )
1522
    if (exec_ir2 )   //  this has priority of course 
1523
        begin
1524
            if (LDsSP_NN     == ir2)   sp <= nn;
1525
            if (ED_LDsREG_6NN7 == ir2) sp <= nn;
1526
            if (  DECsSP   == ir2 )  sp <= add16;
1527
            if (  INCsSP   == ir2 )  sp <= add16;
1528
        end
1529
    else if (wb_rdy_nhz)
1530
    begin
1531
         if (  DECsSP   == ir1 & dec_state == DEC_EXEC)  sp <= adr_alu;
1532
         if (  INCsSP   == ir1 & dec_state == DEC_EXEC)  sp <= adr_alu;
1533
         if (  LDsSP_HL == ir1 & dec_state == DEC_EXEC)  sp <= {hr,lr};
1534
         if (next_mem_state == MEM_OFSP      ) sp <= adr_alu;
1535
         if (next_mem_state == MEM_OSSP      ) sp <= adr_alu;
1536
         if (next_mem_state == MEM_OSSP_PCM2 ) sp <= adr_alu;
1537
         if (next_mem_state == MEM_OSSP_P    ) sp <= adr_alu;
1538
    end
1539
//----------------- inst hazard logic ------------------------------------------
1540
 
1541
 
1542
 
1543
always @(posedge clk or posedge rst)
1544
    if (rst) inst_haz <= 1'b0;
1545
    else if  (we_next & (pc - 16'h1) == mux21)  inst_haz <= 1'b1;
1546
    else if  (dec_state == DEC_EXEC)  inst_haz <= 1'b0;   // highest priority interrupt
1547
 
1548
//-------------------- int logic ----------------------------------------
1549
//  We have a wishbone interrupt system  -  which i guess does not preclude a 
1550
//  non-maskable interrupt......   but bottom line is that such an interrupt is 
1551
//  definately out of favor with current system thinking.   Within an embedded system
1552
//  ( the target application here ) a single interrupt controller  capable of handeling
1553
//   as many interrupts as desired is the best choice.  
1554
//  Therefore we enable only mode 2 interrupts and a single enable ff.
1555
//
1556
//  This begs the question of what to do with the "RETI" instruction  -- ED4D.  We opt to 
1557
//  enable interrupts with this instruction (and all its "aliases").
1558
//
1559
always @(posedge clk or posedge rst)
1560
    if (rst)                   int_en <= 1'b0;
1561
    else if (wb_rdy_nhz)
1562
    begin
1563
        if      ((dec_state == DEC_EXEC) & (DI== ir1))  int_en <= 1'b0;
1564
        else if ((dec_state == DEC_EXEC) & en_int_next) int_en <= 1'b1;
1565
        else if ((dec_state == DEC_ED)   & ed_retn)     int_en <= 1'b0;
1566
        if      (dec_state == DEC_INT1)                 int_en <= 1'b0;
1567
    end
1568
 
1569
 
1570
always @(posedge clk or posedge rst)
1571
    if (rst)                                      en_int_next <=1'b0;
1572
    else if (wb_rdy_nhz)
1573
    begin
1574
        if ((dec_state == DEC_EXEC) & (EI== ir1)) en_int_next <=1'b1;
1575
        else if (dec_state == DEC_EXEC)           en_int_next <=1'b0;
1576
    end
1577
 
1578
always @(posedge clk)
1579
    wb_irq_sync <= wb_int_rq;
1580
 
1581
assign  wb_int = wb_irq_sync & int_en;
1582
 
1583
endmodule
1584
 

powered by: WebSVN 2.1.0

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