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

Subversion Repositories wb_z80

[/] [wb_z80/] [trunk/] [rtl/] [z80_memstate2.v] - Blame information for rev 35

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

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

powered by: WebSVN 2.1.0

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