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

Subversion Repositories rf6809

[/] [rf6809/] [trunk/] [rtl/] [cpu/] [rf6809.sv] - Blame information for rev 21

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

Line No. Rev Author Line
1 2 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2022  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch@finitron.ca
6
//       ||
7
//
8
//      rf6809.sv
9
//
10
//
11
// BSD 3-Clause License
12
// Redistribution and use in source and binary forms, with or without
13
// modification, are permitted provided that the following conditions are met:
14
//
15
// 1. Redistributions of source code must retain the above copyright notice, this
16
//    list of conditions and the following disclaimer.
17
//
18
// 2. Redistributions in binary form must reproduce the above copyright notice,
19
//    this list of conditions and the following disclaimer in the documentation
20
//    and/or other materials provided with the distribution.
21
//
22
// 3. Neither the name of the copyright holder nor the names of its
23
//    contributors may be used to endorse or promote products derived from
24
//    this software without specific prior written permission.
25
//
26
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
30
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
//
37
// ============================================================================
38
 
39
import rf6809_pkg::*;
40
 
41
module rf6809(id, rst_i, clk_i, halt_i, nmi_i, irq_i, firq_i, vec_i, ba_o, bs_o, lic_o, tsc_i,
42
        rty_i, bte_o, cti_o, bl_o, lock_o, cyc_o, stb_o, we_o, ack_i, aack_i, atag_i,
43
        adr_o, dat_i, dat_o, state);
44
parameter RESET = 6'd0;
45
parameter IFETCH = 6'd1;
46
parameter DECODE = 6'd2;
47
parameter CALC = 6'd3;
48
parameter PULL1 = 6'd4;
49
parameter PUSH1 = 6'd5;
50
parameter PUSH2 = 6'd6;
51
parameter LOAD1 = 6'd7;
52
parameter LOAD2 = 6'd8;
53
parameter STORE1 = 6'd9;
54 21 robfinch
parameter STORE1a = 6'd10;
55
parameter STORE2 = 6'd11;
56
parameter OUTER_INDEXING = 6'd12;
57
parameter OUTER_INDEXING2 = 6'd13;
58 2 robfinch
parameter ICACHE1 = 6'd31;
59
parameter ICACHE2 = 6'd32;
60
parameter ICACHE3 = 6'd33;
61
parameter ICACHE4 = 6'd34;
62
parameter ICACHE5 = 6'd35;
63
parameter ICACHE6 = 6'd36;
64
parameter ICACHE7 = 6'd37;
65
parameter ICACHE8 = 6'd38;
66
parameter ICACHE9 = 6'd39;
67
parameter IBUF1 = 6'd40;
68
parameter IBUF2 = 6'd41;
69
parameter IBUF3 = 6'd42;
70
parameter IBUF4 = 6'd43;
71
parameter IBUF5 = 6'd44;
72
parameter IBUF6 = 6'd45;
73
input [5:0] id;
74
input rst_i;
75
input clk_i;
76
input halt_i;
77
input nmi_i;
78
input irq_i;
79
input firq_i;
80
input [`TRPBYTE] vec_i;
81
output reg ba_o;
82
output reg bs_o;
83
output lic_o;
84
input tsc_i;
85
input rty_i;
86
output reg [1:0] bte_o;
87
output reg [2:0] cti_o;
88
output reg [5:0] bl_o;
89
output reg cyc_o;
90
output reg stb_o;
91
output reg we_o;
92
output reg lock_o;
93
input ack_i;
94
input aack_i;
95
input [3:0] atag_i;
96
output reg [`TRPBYTE] adr_o;
97
input [`LOBYTE] dat_i;
98
output reg [`LOBYTE] dat_o;
99
output [5:0] state;
100
 
101
reg [5:0] state;
102
reg [5:0] load_what,store_what,load_what2;
103
reg [`TRPBYTE] pc;
104
wire [`TRPBYTE] pcp2 = pc + 4'd2;
105
wire [`TRPBYTE] pcp16 = pc + 5'd16;
106
wire [`HEXBYTE] insn;
107
wire icacheOn = 1'b1;
108
reg [`TRPBYTE] ibufadr, icwa;
109
reg [191:0] ibuf;
110
wire ibufhit = ibufadr==pc;
111
reg natMd,firqMd;
112
reg md32;
113
wire [`DBLBYTE] mask = 24'hFFFFFF;
114
reg [1:0] ipg;
115
reg isFar;
116
reg isOuterIndexed;
117
reg [`HEXBYTE] ir;
118
`ifdef EIGHTBIT
119
wire [9:0] ir12 = {ipg,ir[`LOBYTE]};
120
`endif
121
`ifdef TWELVEBIT
122
wire [`LOBYTE] ir12 = ir[`LOBYTE];
123
`endif
124
reg [`LOBYTE] dpr;              // direct page register
125
reg [`DBLBYTE] usppg;   // user stack pointer page
126
wire [`LOBYTE] ndxbyte;
127
reg cf,vf,zf,nf,hf,ef;
128
wire [`LOBYTE] cfx8 = cf;
129
wire [`DBLBYTE] cfx24 = {23'b0,cf};
130
reg im,firqim;
131
reg sync_state,wait_state;
132
wire [`LOBYTE] ccr = {ef,firqim,hf,im,nf,zf,vf,cf};
133
reg [`LOBYTE] acca,accb;
134 21 robfinch
`ifdef SUPPORT_6309
135
reg [`LOBYTE] acce,accf;
136
`endif
137 2 robfinch
reg [`DBLBYTE] accd;
138 21 robfinch
`ifdef SUPPORT_6309
139
reg [`DBLBYTE] accw;
140
`endif
141 2 robfinch
reg [`DBLBYTE] xr,yr,usp,ssp;
142
wire [`DBLBYTE] prod = acca * accb;
143
reg [`DBLBYTE] vect;
144
reg [`DBLBYTEP1] res;
145
reg [`LOBYTEP1] res12;
146
wire res12n = res12[BPBM1];
147
wire res12z = res12[`LOBYTE]==12'h000;
148
wire res12c = res12[bitsPerByte];
149
wire res24n = res[BPBX2M1];
150
wire res24z = res[`DBLBYTE]==24'h000000;
151
wire res24c = res[BPB*2];
152
reg [`TRPBYTE] ia;
153
reg ic_invalidate;
154
reg first_ifetch;
155
reg tsc_latched;
156
wire tsc = tsc_i|tsc_latched;
157
reg [`LOBYTE] chkpoint;
158
reg [15:0] icgot;
159
reg [23:0] btocnt;
160
reg bto;                                                        // bus timed out
161
 
162
reg [`DBLBYTE] a,b;
163
wire [`LOBYTE] b12 = b[`LOBYTE];
164
reg [`TRPBYTE] radr,wadr;
165
reg [`DBLBYTE] wdat;
166
 
167
reg nmi1,nmi_edge;
168
reg nmi_armed;
169
 
170
reg isStore;
171
reg isPULU,isPULS;
172
reg isPSHS,isPSHU;
173
reg isRTS,isRTI,isRTF;
174
reg isLEA;
175
reg isRMW;
176
 
177 21 robfinch
function fnAddOverflow;
178
input a;
179
input b;
180
input r;
181
begin
182
        fnAddOverflow = (r ^ b) & (1'b1 ^ a ^ b);
183
end
184
endfunction
185
 
186
function fnSubOverflow;
187
input a;
188
input b;
189
input r;
190
begin
191
        fnSubOverflow = (1'b1 ^ r ^ b) & (a ^ b);
192
end
193
endfunction
194
 
195 2 robfinch
// Data input path multiplexing
196 12 robfinch
reg [bitsPerByte-1:0] dati;
197 2 robfinch
always_comb
198
        dati = dat_i;
199
 
200
// Evaluate the branch conditional
201
reg takb;
202
always_comb
203
        case(ir12)
204
        `BRA,`LBRA:             takb <= 1'b1;
205
        `BRN,`LBRN:             takb <= 1'b0;
206
        `BHI,`LBHI:             takb <= !cf & !zf;
207
        `BLS,`LBLS:             takb <=  cf | zf;
208
        `BLO,`LBLO:             takb <=  cf;
209
        `BHS,`LBHS:             takb <= !cf;
210
        `BNE,`LBNE:             takb <= !zf;
211
        `BEQ,`LBEQ:             takb <=  zf;
212
        `BMI,`LBMI:             takb <=  nf;
213
        `BPL,`LBPL:             takb <= !nf;
214
        `BVS,`LBVS:             takb <=  vf;
215
        `BVC,`LBVC:             takb <= !vf;
216
        `BGT,`LBGT:             takb <= (nf & vf & !zf) | (!nf & !vf & !zf);
217
        `BGE,`LBGE:             takb <= (nf & vf) | (!nf & !vf);
218
        `BLE,`LBLE:             takb <= zf | (nf & !vf) | (!nf & vf);
219
        `BLT,`LBLT:             takb <= (nf & !vf) | (!nf & vf);
220
        default:        takb <= 1'b1;
221
        endcase
222
 
223
// This chunk of code takes care of calculating the number of bytes stacked
224
// by a push or pull operation.
225
//
226
reg [4:0] cnt;
227
always_comb
228
begin
229
        cnt =   (ir[bitsPerByte] ? 5'd1 : 5'd0) +
230
                        (ir[bitsPerByte+1] ? 5'd1 : 5'd0) +
231
                        (ir[bitsPerByte+2] ? 5'd1 : 5'd0) +
232
                        (ir[bitsPerByte+3] ? 5'd1 : 5'd0) +
233
                        (ir[bitsPerByte+4] ? 5'd2 : 5'd0) +
234
                        (ir[bitsPerByte+5] ? 5'd2 : 5'd0) +
235
                        (ir[bitsPerByte+6] ? 5'd2 : 5'd0) +
236 11 robfinch
                        (ir[bitsPerByte+7] ? (isFar ? 5'd3 : 5'd2) : 5'd0)
237 21 robfinch
`ifdef SUPPORT_6309
238
                        + (ir[bitsPerByte+8] ? 5'd1 : 5'd0) +
239
                        + (ir[bitsPerByte+9] ? 5'd1 : 5'd0)
240
`endif
241 2 robfinch
                        ;
242
//  cnt = 0;
243
//      if (ir[8]) cnt = cnt + 5'd1;    // CC
244
//      if (ir[9]) cnt = cnt + md32 ? 5'd4 : 5'd1;      // A
245
//      if (ir[10]) cnt = cnt + md32 ? 5'd4 : 5'd1;     // B
246
//      if (ir[BPBM1]) cnt = cnt + 5'd1;        // DP
247
//      if (ir[12]) cnt = cnt + md32 ? 5'd4 : 5'd2;     // X
248
//      if (ir[bitsPerByte+1]) cnt = cnt + md32 ? 5'd4 : 5'd2;  // Y
249
//      if (ir[bitsPerByte+2]) cnt = cnt + md32 ? 5'd4 : 5'd2;  // U/S
250
//      if (ir[bitsPerByte+3]) cnt = cnt + 5'd4;        // PC
251
end
252
 
253 21 robfinch
`ifdef SUPPORT_6309
254
wire isInMem =  ir12==`AIM_DP || ir12==`EIM_DP || ir12==`OIM_DP || ir12==`TIM_DP ||
255
                                ir12==`AIM_NDX || ir12==`EIM_NDX || ir12==`OIM_NDX || ir12==`TIM_NDX ||
256
                                ir12==`AIM_EXT || ir12==`EIM_EXT || ir12==`OIM_EXT || ir12==`TIM_EXT
257
                                ;
258
wire isRMW1 =   ir12==`AIM_DP || ir12==`EIM_DP || ir12==`OIM_DP ||
259
                                ir12==`NEG_DP || ir12==`COM_DP || ir12==`LSR_DP || ir12==`ROR_DP || ir12==`ASR_DP || ir12==`ASL_DP || ir12==`ROL_DP || ir12==`DEC_DP || ir12==`INC_DP ||
260
                                ir12==`AIM_NDX || ir12==`EIM_NDX || ir12==`OIM_NDX ||
261
                                ir12==`NEG_NDX || ir12==`COM_NDX || ir12==`LSR_NDX || ir12==`ROR_NDX || ir12==`ASR_NDX || ir12==`ASL_NDX || ir12==`ROL_NDX || ir12==`DEC_NDX || ir12==`INC_NDX ||
262
                                ir12==`AIM_EXT || ir12==`EIM_EXT || ir12==`OIM_EXT ||
263
                                ir12==`NEG_EXT || ir12==`COM_EXT || ir12==`LSR_EXT || ir12==`ROR_EXT || ir12==`ASR_EXT || ir12==`ASL_EXT || ir12==`ROL_EXT || ir12==`DEC_EXT || ir12==`INC_EXT
264
                                ;
265
`else
266
wire isInMem = 1'b0;
267 2 robfinch
wire isRMW1 =   ir12==`NEG_DP || ir12==`COM_DP || ir12==`LSR_DP || ir12==`ROR_DP || ir12==`ASR_DP || ir12==`ASL_DP || ir12==`ROL_DP || ir12==`DEC_DP || ir12==`INC_DP ||
268
                                ir12==`NEG_NDX || ir12==`COM_NDX || ir12==`LSR_NDX || ir12==`ROR_NDX || ir12==`ASR_NDX || ir12==`ASL_NDX || ir12==`ROL_NDX || ir12==`DEC_NDX || ir12==`INC_NDX ||
269
                                ir12==`NEG_EXT || ir12==`COM_EXT || ir12==`LSR_EXT || ir12==`ROR_EXT || ir12==`ASR_EXT || ir12==`ASL_EXT || ir12==`ROL_EXT || ir12==`DEC_EXT || ir12==`INC_EXT
270
                                ;
271 21 robfinch
`endif
272 2 robfinch
 
273
wire isIndexed =
274
        ir12[7:4]==4'h6 || ir12[7:4]==4'hA || ir12[7:4]==4'hE ||
275
        ir12==`LEAX_NDX || ir12==`LEAY_NDX || ir12==`LEAS_NDX || ir12==`LEAU_NDX
276
        ;
277
reg isDblIndirect;
278 12 robfinch
wire isIndirect = ndxbyte[bitsPerByte-4] & ndxbyte[bitsPerByte-1];
279 13 robfinch
`ifdef TWELVEBIT
280
always_comb
281
        isOuterIndexed = ndxbyte[bitsPerByte-5] & ndxbyte[bitsPerByte-1];
282
`endif
283
 
284 2 robfinch
assign ndxbyte = ir[`HIBYTE];
285
 
286
// Detect type of interrupt
287
wire isINT = ir12==`INT;
288
wire isRST = vect[3:0]==4'hE;
289
wire isNMI = vect[3:0]==4'hC;
290
wire isSWI = vect[3:0]==4'hA;
291
wire isIRQ = vect[3:0]==4'h8;
292
wire isFIRQ = vect[3:0]==4'h6;
293
wire isSWI2 = vect[3:0]==4'h4;
294
wire isSWI3 = vect[3:0]==4'h2;
295
 
296
wire [`TRPBYTE] far_address = {ir[`HIBYTE],ir[`BYTE3],ir[`BYTE4]};
297
wire [`TRPBYTE] address = {ir[`HIBYTE],ir[`BYTE3]};
298
wire [`TRPBYTE] dp_address = {dpr,ir[`HIBYTE]};
299
wire [`TRPBYTE] ex_address = isFar ? far_address : address;
300
wire [`TRPBYTE] offset12 = {{bitsPerByte{ir[bitsPerByte*3-1]}},ir[`BYTE3]};
301
wire [`TRPBYTE] offset24 = {ir[`BYTE3],ir[`BYTE4]};
302
wire [`TRPBYTE] offset36 = {ir[`BYTE3],ir[`BYTE4],ir[`BYTE5]};
303
 
304
// Choose the indexing register
305
reg [`TRPBYTE] ndxreg;
306
always_comb
307
        if (bitsPerByte==8)
308
                case(ndxbyte[6:5])
309
                2'b00:  ndxreg <= xr;
310
                2'b01:  ndxreg <= yr;
311
                2'b10:  ndxreg <= {usppg,8'h00} + usp;
312
                2'b11:  ndxreg <= ssp;
313
                endcase
314
        else if (bitsPerByte==12)
315
                case(ndxbyte[10:9])
316
                2'b00:  ndxreg <= xr;
317
                2'b01:  ndxreg <= yr;
318
                2'b10:  ndxreg <= {usppg,8'h00} + usp;
319
                2'b11:  ndxreg <= ssp;
320
                endcase
321
 
322
reg [`TRPBYTE] NdxAddr;
323
always_comb
324
        if (bitsPerByte==8)
325
                casez({isOuterIndexed,ndxbyte})
326
                9'b00???????:   NdxAddr <= ndxreg + {{19{ndxbyte[BPB-4]}},ndxbyte[BPB-4:0]};
327
                9'b01???0000:   NdxAddr <= ndxreg;
328
                9'b01???0001:   NdxAddr <= ndxreg;
329
                9'b01???0010:   NdxAddr <= ndxreg - 2'd1;
330
                9'b01???0011:   NdxAddr <= ndxreg - 2'd2;
331
                9'b01???0100:   NdxAddr <= ndxreg;
332
                9'b01???0101:   NdxAddr <= ndxreg + {{BPB*2{accb[BPBM1]}},accb};
333
                9'b01???0110:   NdxAddr <= ndxreg + {{BPB*2{acca[BPBM1]}},acca};
334
                9'b01???1000:   NdxAddr <= ndxreg + offset12;
335
                9'b01???1001:   NdxAddr <= ndxreg + offset24;
336
                9'b01???1010:   NdxAddr <= ndxreg + offset36;
337
                9'b01???1011:   NdxAddr <= ndxreg + {acca,accb};
338
                9'b01???1100:   NdxAddr <= pc + offset12 + 3'd3;
339
                9'b01???1101:   NdxAddr <= pc + offset24 + 3'd4;
340
                9'b01???1110:   NdxAddr <= pc + offset36 + 3'd5;
341
                9'b01??01111:   NdxAddr <= isFar ? offset36 : offset24;
342
                9'b01??11111:   NdxAddr <= offset24;
343
                9'b10???????:   NdxAddr <= {{11{ndxbyte[BPB-4]}},ndxbyte[BPB-4:0]};
344
                9'b11???0000:   NdxAddr <= 24'd0;
345
                9'b11???0001:   NdxAddr <= 24'd0;
346
                9'b11???0010:   NdxAddr <= 24'd0;
347
                9'b11???0011:   NdxAddr <= 24'd0;
348
                9'b11???0100:   NdxAddr <= 24'd0;
349
                9'b11???0101:   NdxAddr <= {{BPB*2{accb[BPBM1]}},accb};
350
                9'b11???0110:   NdxAddr <= {{BPB*2{acca[BPBM1]}},acca};
351
                9'b11???1000:   NdxAddr <= offset12;
352
                9'b11???1001:   NdxAddr <= offset24;
353
                9'b11???1010:   NdxAddr <= offset36;
354
                9'b11???1011:   NdxAddr <= {acca,accb};
355
                9'b11???1100:   NdxAddr <= pc + offset12 + 3'd3;
356
                9'b11???1101:   NdxAddr <= pc + offset24 + 3'd4;
357
                9'b11???1110:   NdxAddr <= pc + offset36 + 3'd5;
358
                9'b11??01111:   NdxAddr <= isFar ? offset36 : offset24;
359
                9'b11??11111:   NdxAddr <= offset24;
360
                default:                NdxAddr <= 24'hFFFFFF;
361
                endcase
362
        else if (bitsPerByte==12)
363
                casez({isOuterIndexed,ndxbyte})
364
                13'b00???????????:      NdxAddr <= ndxreg + {{27{ndxbyte[BPB-4]}},ndxbyte[BPB-4:0]};
365
                13'b01???00000000:      NdxAddr <= ndxreg;
366
                13'b01???00000001:      NdxAddr <= ndxreg;
367
                13'b01???00000010:      NdxAddr <= ndxreg - 2'd1;
368
                13'b01???00010010:      NdxAddr <= ndxreg - 2'd2;
369
                13'b01???00100010:      NdxAddr <= ndxreg - 2'd3;
370
                13'b01???00000011:      NdxAddr <= ndxreg - 2'd2;
371
                13'b01???00000100:      NdxAddr <= ndxreg;
372
                13'b01???00000101:      NdxAddr <= ndxreg + {{BPB*2{accb[BPBM1]}},accb};
373
                13'b01???00000110:      NdxAddr <= ndxreg + {{BPB*2{acca[BPBM1]}},acca};
374
                13'b01???00001000:      NdxAddr <= ndxreg + offset12;
375
                13'b01???00001001:      NdxAddr <= ndxreg + offset24;
376
                13'b01???00001010:      NdxAddr <= ndxreg + offset36;
377
                13'b01???00001011:      NdxAddr <= ndxreg + {acca,accb};
378
                13'b01???00001100:      NdxAddr <= pc + offset12 + 3'd3;
379
                13'b01???00001101:      NdxAddr <= pc + offset24 + 3'd4;
380
                13'b01???00001110:      NdxAddr <= pc + offset36 + 3'd5;
381
                13'b01??000001111:      NdxAddr <= isFar ? offset36 : offset24;
382
                13'b01??100001111:      NdxAddr <= offset24;
383
                13'b01???10000000:      NdxAddr <= 24'd0;
384
                13'b01???10000001:      NdxAddr <= 24'd0;
385
                13'b01???10000010:      NdxAddr <= 24'd0;
386
                13'b01???10000011:      NdxAddr <= 24'd0;
387
                13'b01???10000100:      NdxAddr <= 24'd0;
388
                13'b01???10000101:      NdxAddr <= {{BPB*2{accb[BPBM1]}},accb};
389
                13'b01???10000110:      NdxAddr <= {{BPB*2{acca[BPBM1]}},acca};
390
                13'b01???10001000:      NdxAddr <= offset12;
391
                13'b01???10001001:      NdxAddr <= offset24;
392 15 robfinch
                13'b01???10001010:      NdxAddr <= offset36;
393 2 robfinch
                13'b01???10001011:      NdxAddr <= {acca,accb};
394
                13'b01???10001100:      NdxAddr <= pc + offset12 + 3'd3;
395
                13'b01???10001101:      NdxAddr <= pc + offset24 + 3'd4;
396
                13'b01???10001110:      NdxAddr <= pc + offset36 + 3'd5;
397
                13'b01??010001111:      NdxAddr <= isFar ? offset36 : offset24;
398
                13'b01??110001111:      NdxAddr <= offset24;
399
                13'b10???????????:      NdxAddr <= {{15{ndxbyte[BPB-4]}},ndxbyte[BPB-4:0]};
400
                13'b11???00000000:      NdxAddr <= 24'd0;
401
                13'b11???00000001:      NdxAddr <= 24'd0;
402
                13'b11???00000010:      NdxAddr <= 24'd0;
403
                13'b11???00000011:      NdxAddr <= 24'd0;
404
                13'b11???00000100:      NdxAddr <= 24'd0;
405
                13'b11???00000101:      NdxAddr <= {{BPB*2{accb[BPBM1]}},accb};
406
                13'b11???00000110:      NdxAddr <= {{BPB*2{acca[BPBM1]}},acca};
407
                13'b11???00001000:      NdxAddr <= offset12;
408
                13'b11???00001001:      NdxAddr <= offset24;
409
                13'b11???00001010:      NdxAddr <= offset36;
410
                13'b11???00001011:      NdxAddr <= {acca,accb};
411
                13'b11???00001100:      NdxAddr <= pc + offset12 + 3'd3;
412
                13'b11???00001101:      NdxAddr <= pc + offset24 + 3'd4;
413
                13'b11???00001110:      NdxAddr <= pc + offset36 + 3'd5;
414
                13'b11??000001111:      NdxAddr <= isFar ? offset36 : offset24;
415
                13'b11??000011111:      NdxAddr <= offset24;
416
                default:                NdxAddr <= 24'hFFFFFF;
417
                endcase
418
 
419
// Compute instruction length depending on indexing byte
420
reg [2:0] insnsz;
421
always_comb
422
        if (bitsPerByte==8)
423
                casez(ndxbyte)
424
                8'b0???????:    insnsz <= 4'h2;
425
                8'b1??00000:    insnsz <= 4'h2;
426
                8'b1??00001:    insnsz <= 4'h2;
427
                8'b1??00010:    insnsz <= 4'h2;
428
                8'b1??00011:    insnsz <= 4'h2;
429
                8'b1??00100:    insnsz <= 4'h2;
430
                8'b1??00101:    insnsz <= 4'h2;
431
                8'b1??00110:    insnsz <= 4'h2;
432
                8'b1??01000:    insnsz <= 4'h3;
433
                8'b1??01001:    insnsz <= 4'h4;
434
                8'b1??01010:    insnsz <= 4'h5;
435
                8'b1??01011:    insnsz <= 4'h2;
436
                8'b1??01100:    insnsz <= 4'h3;
437
                8'b1??01101:    insnsz <= 4'h4;
438
                8'b1??01110:    insnsz <= 4'h5;
439
                8'b1??01111:    insnsz <= isFar ? 4'h5 : 4'h4;
440
                8'b1??11111:    insnsz <= 4'h4;
441
                default:        insnsz <= 4'h2;
442
                endcase
443
        else if (bitsPerByte==12)
444
                casez(ndxbyte)
445
                12'b0???????????:       insnsz <= 4'h2;
446
                12'b1???00000000:       insnsz <= 4'h2;
447
                12'b1???00000001:       insnsz <= 4'h2;
448
                12'b1???00000010:       insnsz <= 4'h2;
449
                12'b1???00000011:       insnsz <= 4'h2;
450
                12'b1???00000100:       insnsz <= 4'h2;
451
                12'b1???00000101:       insnsz <= 4'h2;
452
                12'b1???00000110:       insnsz <= 4'h2;
453
                12'b1???00001000:       insnsz <= 4'h3;
454
                12'b1???00001001:       insnsz <= 4'h4;
455
                12'b1???00001010:       insnsz <= 4'h5;
456
                12'b1???00001011:       insnsz <= 4'h2;
457
                12'b1???00001100:       insnsz <= 4'h3;
458
                12'b1???00001101:       insnsz <= 4'h4;
459
                12'b1???00001110:       insnsz <= 4'h5;
460
                12'b1??000001111:       insnsz <= isFar ? 4'h5 : 4'h4;
461 15 robfinch
                12'b1??100001111:       insnsz <= 4'h4;
462 2 robfinch
                default:        insnsz <= 4'h2;
463
                endcase
464
 
465
// Source registers for transfer or exchange instructions.
466
reg [`DBLBYTE] src1,src2;
467
always_comb
468
        case(ir[bitsPerByte+7:bitsPerByte+4])
469
        4'b0000:        src1 <= {acca[`LOBYTE],accb[`LOBYTE]};
470
        4'b0001:        src1 <= xr;
471
        4'b0010:        src1 <= yr;
472
        4'b0011:        src1 <= usp;
473
        4'b0100:        src1 <= ssp;
474
        4'b0101:        src1 <= pcp2;
475
        4'b1000:        src1 <= acca[`LOBYTE];
476
        4'b1001:        src1 <= accb[`LOBYTE];
477
        4'b1010:        src1 <= ccr;
478
        4'b1011:        src1 <= dpr;
479
        4'b1100:        src1 <= usppg;
480
        4'b1101:        src1 <= 24'h0000;
481 21 robfinch
`ifdef SUPPORT_6309
482
        4'b0110:        src1 <= {acce[`LOBYTE],accf[`LOBYTE]};
483
        4'b1110:        src1 <= acce;
484
        4'b1111:        src1 <= accf;
485
`else
486 2 robfinch
        4'b1110:        src1 <= 24'h0000;
487
        4'b1111:        src1 <= 24'h0000;
488 21 robfinch
`endif
489 2 robfinch
        default:        src1 <= 24'h0000;
490
        endcase
491
always_comb
492
        case(ir[bitsPerByte+3:bitsPerByte])
493
        4'b0000:        src2 <= {acca[`LOBYTE],accb[`LOBYTE]};
494
        4'b0001:        src2 <= xr;
495
        4'b0010:        src2 <= yr;
496
        4'b0011:        src2 <= usp;
497
        4'b0100:        src2 <= ssp;
498
        4'b0101:        src2 <= pcp2;
499
        4'b1000:        src2 <= acca[`LOBYTE];
500
        4'b1001:        src2 <= accb[`LOBYTE];
501
        4'b1010:        src2 <= ccr;
502
        4'b1011:        src2 <= dpr;
503
        4'b1100:        src2 <= usppg;
504
        4'b1101:        src2 <= 24'h0000;
505 21 robfinch
`ifdef SUPPORT_6309
506
        4'b0110:        src2 <= {acce[`LOBYTE],accf[`LOBYTE]};
507
        4'b1110:        src2 <= acce;
508
        4'b1111:        src2 <= accf;
509
`else
510 2 robfinch
        4'b1110:        src2 <= 24'h0000;
511
        4'b1111:        src2 <= 24'h0000;
512 21 robfinch
`endif
513 2 robfinch
        default:        src2 <= 24'h0000;
514
        endcase
515
 
516 21 robfinch
wire [bitsPerByte*2:0] sum12 = src1 + src2;
517
wire [bitsPerByte*2:0] sum12c = src1 + src2 + cf;
518
wire [bitsPerByte*2-1:0] and12 = src1 & src2;
519
wire [bitsPerByte*2-1:0] eor12 = src1 ^ src2;
520
wire [bitsPerByte*2-1:0] or12 = src1 | src2;
521
wire [bitsPerByte*2:0] dif12 = src1 - src2;
522
wire [bitsPerByte*2:0] dif12c = src1 - src2 - cf;
523
 
524 2 robfinch
wire isAcca     =       ir12==`NEGA || ir12==`COMA || ir12==`LSRA || ir12==`RORA || ir12==`ASRA || ir12==`ASLA ||
525
                                ir12==`ROLA || ir12==`DECA || ir12==`INCA || ir12==`TSTA || ir12==`CLRA ||
526
                                ir12==`SUBA_IMM || ir12==`CMPA_IMM || ir12==`SBCA_IMM || ir12==`ANDA_IMM || ir12==`BITA_IMM ||
527
                                ir12==`LDA_IMM || ir12==`EORA_IMM || ir12==`ADCA_IMM || ir12==`ORA_IMM || ir12==`ADDA_IMM ||
528
                                ir12==`SUBA_DP || ir12==`CMPA_DP || ir12==`SBCA_DP || ir12==`ANDA_DP || ir12==`BITA_DP ||
529
                                ir12==`LDA_DP || ir12==`EORA_DP || ir12==`ADCA_DP || ir12==`ORA_DP || ir12==`ADDA_DP ||
530
                                ir12==`SUBA_NDX || ir12==`CMPA_NDX || ir12==`SBCA_NDX || ir12==`ANDA_NDX || ir12==`BITA_NDX ||
531
                                ir12==`LDA_NDX || ir12==`EORA_NDX || ir12==`ADCA_NDX || ir12==`ORA_NDX || ir12==`ADDA_NDX ||
532
                                ir12==`SUBA_EXT || ir12==`CMPA_EXT || ir12==`SBCA_EXT || ir12==`ANDA_EXT || ir12==`BITA_EXT ||
533
                                ir12==`LDA_EXT || ir12==`EORA_EXT || ir12==`ADCA_EXT || ir12==`ORA_EXT || ir12==`ADDA_EXT
534
                                ;
535
 
536 21 robfinch
`ifdef SUPPORT_6309
537
wire isAcce =   ir12 == `ADDE_IMM || ir12==`ADDE_DP || ir12==`ADDE_NDX || ir12==`ADDE_EXT || ir12==`CLRE || ir12==`COME ||
538
                                                                ir12 == `SUBE_IMM || ir12==`SUBE_DP || ir12==`SUBE_NDX || ir12==`SUBE_EXT ||
539
                                                                ir12 == `LDE_IMM || ir12==`LDE_DP || ir12==`LDE_NDX || ir12==`LDE_EXT ||
540
                                                                ir12 == `DECE || ir12==`INCE ||
541
                                                                ir12 == `CMPE_IMM || ir12==`CMPE_DP || ir12==`CMPE_NDX || ir12==`CMPE_EXT
542
                                ;
543
wire isAccf =   ir12 == `ADDF_IMM || ir12==`ADDF_DP || ir12==`ADDF_NDX || ir12==`ADDF_EXT || ir12==`CLRF || ir12==`COMF ||
544
                                                                ir12 == `SUBF_IMM || ir12==`SUBF_DP || ir12==`SUBF_NDX || ir12==`SUBF_EXT ||
545
                                                                ir12 == `LDF_IMM || ir12==`LDF_DP || ir12==`LDF_NDX || ir12==`LDF_EXT ||
546
                                                                ir12 == `DECF || ir12==`INCF ||
547
                                                                ir12 == `CMPF_IMM || ir12==`CMPF_DP || ir12==`CMPF_NDX || ir12==`CMPF_EXT
548
                                ;
549
wire [`DBLBYTE] acc = isAcce ? acce : isAccf ? accf : isAcca ? acca : accb;
550
`else
551 2 robfinch
wire [`DBLBYTE] acc = isAcca ? acca : accb;
552 21 robfinch
`endif
553 2 robfinch
 
554
always_ff @(posedge clk_i)
555
if (state==DECODE) begin
556
        isStore <=      ir12==`STA_DP || ir12==`STB_DP || ir12==`STD_DP || ir12==`STX_DP || ir12==`STY_DP || ir12==`STU_DP || ir12==`STS_DP ||
557
                                ir12==`STA_NDX || ir12==`STB_NDX || ir12==`STD_NDX || ir12==`STX_NDX || ir12==`STY_NDX || ir12==`STU_NDX || ir12==`STS_NDX ||
558 21 robfinch
                                ir12==`STA_EXT || ir12==`STB_EXT || ir12==`STD_EXT || ir12==`STX_EXT || ir12==`STY_EXT || ir12==`STU_EXT || ir12==`STS_EXT ||
559
                                ir12==`STE_DP || ir12==`STE_NDX || ir12==`STE_EXT || ir12==`STF_DP || ir12==`STF_NDX || ir12==`STF_EXT ||
560
                                ir12==`STW_DP || ir12==`STW_NDX || ir12==`STW_EXT
561 2 robfinch
                                ;
562
        isPULU <= ir12==`PULU;
563
        isPULS <= ir12==`PULS;
564
        isPSHS <= ir12==`PSHS;
565
        isPSHU <= ir12==`PSHU;
566
        isRTI <= ir12==`RTI;
567
        isRTS <= ir12==`RTS;
568
        isRTF <= ir12==`RTF;
569
        isLEA <= ir12==`LEAX_NDX || ir12==`LEAY_NDX || ir12==`LEAU_NDX || ir12==`LEAS_NDX;
570
        isRMW <= isRMW1;
571
end
572
 
573
wire hit0, hit1;
574
wire ihit = hit0 & hit1;
575
reg rhit0;
576
 
577
assign lic_o =  (state==CALC && !isRMW) ||
578
                                (state==DECODE && (
579
                                        ir12==`NOP || ir12==`ORCC || ir12==`ANDCC || ir12==`DAA || ir12==`LDMD || ir12==`TFR || ir12==`EXG ||
580
                                        ir12==`NEGA || ir12==`COMA || ir12==`LSRA || ir12==`RORA || ir12==`ASRA || ir12==`ROLA || ir12==`DECA || ir12==`INCA || ir12==`TSTA || ir12==`CLRA ||
581 21 robfinch
                                        ir12==`DECE || ir12==`DECF || ir12==`DECD || ir12==`DECW || ir12==`INCE || ir12==`INCF || ir12==`INCD || ir12==`INCW ||
582 2 robfinch
                                        ir12==`NEGB || ir12==`COMB || ir12==`LSRB || ir12==`RORB || ir12==`ASRB || ir12==`ROLB || ir12==`DECB || ir12==`INCB || ir12==`TSTB || ir12==`CLRB ||
583 21 robfinch
                                        ir12==`COME || ir12==`COMF || ir12==`COMD || ir12==`COMW ||
584
                                        ir12==`ASLD || ir12==`ASRD || ir12==`TSTD || ir12==`ADDR || ir12==`ADCR || ir12==`ANDR ||
585
                                        ir12==`TSTE || ir12==`TSTF || ir12==`TSTW ||
586
                                        ir12==`LSRD || ir12==`LSRW || ir12==`NEGD || ir12==`ROLD || ir12==`ROLW || ir12==`RORD || ir12==`RORW ||
587 2 robfinch
                                        ir12==`SUBA_IMM || ir12==`CMPA_IMM || ir12==`SBCA_IMM || ir12==`ANDA_IMM || ir12==`BITA_IMM || ir12==`LDA_IMM || ir12==`EORA_IMM || ir12==`ADCA_IMM || ir12==`ORA_IMM || ir12==`ADDA_IMM ||
588
                                        ir12==`SUBB_IMM || ir12==`CMPB_IMM || ir12==`SBCB_IMM || ir12==`ANDB_IMM || ir12==`BITB_IMM || ir12==`LDB_IMM || ir12==`EORB_IMM || ir12==`ADCB_IMM || ir12==`ORB_IMM || ir12==`ADDB_IMM ||
589 21 robfinch
                                        ir12==`EORD_IMM || ir12==`ANDD_IMM || ir12==`ORD_IMM || ir12==`BITD_IMM || ir12==`ADDD_IMM || ir12==`ADCD_IMM || ir12==`SUBD_IMM || ir12==`SBCD_IMM || ir12==`LDD_IMM || ir12==`LDW_IMM ||
590
                                        ir12==`LDQ_IMM || ir12==`CMPD_IMM || ir12==`CMPX_IMM || ir12==`CMPY_IMM || ir12==`CMPU_IMM || ir12==`CMPS_IMM || ir12==`CMPW_IMM ||
591
                                        ir12==`LDE_IMM || ir12==`LDF_IMM ||
592
                                        ir12==`SUBE_IMM || ir12==`SUBF_IMM || ir12==`SUBW_IMM ||
593 2 robfinch
                                        ir12==`BEQ || ir12==`BNE || ir12==`BMI || ir12==`BPL || ir12==`BVS || ir12==`BVC || ir12==`BRA || ir12==`BRN ||
594
                                        ir12==`BHI || ir12==`BLS || ir12==`BHS || ir12==`BLO ||
595
                                        ir12==`BGT || ir12==`BGE || ir12==`BLT || ir12==`BLE ||
596
                                        ir12==`LBEQ || ir12==`LBNE || ir12==`LBMI || ir12==`LBPL || ir12==`LBVS || ir12==`LBVC || ir12==`LBRA || ir12==`LBRN ||
597
                                        ir12==`LBHI || ir12==`LBLS || ir12==`LBHS || ir12==`LBLO ||
598
                                        ir12==`LBGT || ir12==`LBGE || ir12==`LBLT || ir12==`LBLE
599
                                        )
600
                                ) ||
601
                                (state==STORE2 && (
602
                                        (store_what==`SW_ACCQ3124 && wadr[1:0]==2'b00) ||
603
                                        (store_what==`SW_ACCQ70) ||
604
                                        (store_what==`SW_ACCA && !(isINT || isPSHS || isPSHU)) ||
605
                                        (store_what==`SW_ACCB && !(isINT || isPSHS || isPSHU)) ||
606
                                        (store_what==`SW_ACCDH && wadr[1:0]!=2'b11) ||
607
                                        (store_what==`SW_ACCDL) ||
608
                                        (store_what==`SW_X3124 && wadr[1:0]==2'b00 && !(isINT || isPSHS || isPSHU)) ||
609
                                        (store_what==`SW_XL && !(isINT || isPSHS || isPSHU)) ||
610
                                        (store_what==`SW_YL && !(isINT || isPSHS || isPSHU)) ||
611
                                        (store_what==`SW_USPL && !(isINT || isPSHS || isPSHU)) ||
612
                                        (store_what==`SW_SSPL && !(isINT || isPSHS || isPSHU)) ||
613
                                        (store_what==`SW_PCL && !(isINT || isPSHS || isPSHU) && !(ir12==`JSR_NDX && isIndirect)) ||
614
                                        (store_what==`SW_ACCA70 && !(isINT || isPSHS || isPSHU)) ||
615
                                        (store_what==`SW_ACCB70 && !(isINT || isPSHS || isPSHU))
616
                                )) ||
617
                                (state==PUSH2 && ir[`HIBYTE]==12'h000 && !isINT) ||
618
                                (state==PULL1 && ir[`HIBYTE]==12'h000) ||
619
                                (state==OUTER_INDEXING2 && isLEA) ||
620
                                (state==LOAD2 &&
621
                                        (load_what==`LW_ACCA && !(isRTI || isPULU || isPULS)) ||
622
                                        (load_what==`LW_ACCB && !(isRTI || isPULU || isPULS)) ||
623
                                        (load_what==`LW_DPR && !(isRTI || isPULU || isPULS)) ||
624
                                        (load_what==`LW_XL && !(isRTI || isPULU || isPULS)) ||
625
                                        (load_what==`LW_YL && !(isRTI || isPULU || isPULS)) ||
626
                                        (load_what==`LW_USPL && !(isRTI || isPULU || isPULS)) ||
627
                                        (load_what==`LW_SSPL && !(isRTI || isPULU || isPULS)) ||
628
                                        (load_what==`LW_PCL) ||
629
                                        (load_what==`LW_IAL && !isOuterIndexed && isLEA) ||
630
                                        (load_what==`LW_IA3124 && radr[1:0]==2'b00 && !isOuterIndexed && isLEA)
631
                                )
632
                                ;
633
 
634
wire lock_bus = load_what==`LW_XH || load_what==`LW_YH || load_what==`LW_USPH || load_what==`LW_SSPH ||
635 21 robfinch
                                load_what==`LW_PCH || load_what==`LW_BH || load_what==`LW_IAH || load_what==`LW_PC2316 ||
636
                                load_what==`LW_IA2316 || load_what==`LW_B2316 ||
637
                                load_what==`LW_X2316 || load_what==`LW_Y2316 || load_what==`LW_USP2316 || load_what==`LW_SSP2316 ||
638 2 robfinch
                                isRMW ||
639
                                store_what==`SW_ACCDH || store_what==`SW_XH || store_what==`SW_YH || store_what==`SW_USPH || store_what==`SW_SSPH ||
640 21 robfinch
                                store_what==`SW_PCH || store_what==`SW_PC2316 || store_what==`SW_ACCQ2316 ||
641
                                store_what==`SW_X2316 || store_what==`SW_Y2316 || store_what==`SW_USP2316 || store_what==`SW_SSP2316
642 2 robfinch
                                ;
643
 
644
wire isPrefix = ir12==`PG2 || ir12==`PG3 || ir12==`OUTER;
645
 
646
reg rty;
647
reg [5:0] waitcnt;
648
reg [3:0] iccnt;
649
reg [bitsPerByte-1:0] icbuf [0:15];
650
reg [bitsPerByte*16-1:0] icbuf2;
651
reg [15:0] outstanding; // Outstanding async read cycles.
652
integer n4;
653
 
654
rf6809_icachemem u1
655
(
656
        .wclk(clk_i),
657
        .wce(1'b1),
658
        .wr(state==ICACHE6),
659
        .wa(icwa[11:0]),
660
        .i(icbuf2),
661
        .rclk(~clk_i),
662
        .rce(1'b1),
663
        .pc(pc[11:0]),
664
        .insn(insn)
665
);
666
 
667
rf6809_itagmem u2
668
(
669
        .wclk(clk_i),
670
        .wce(1'b1),
671
        .wr(state==ICACHE6),
672
        .wa(icwa[`TRPBYTE]),
673
        .invalidate(ic_invalidate),
674
        .rclk(~clk_i),
675
        .rce(1'b1),
676
        .pc(pc),
677
        .hit0(hit0),
678
        .hit1(hit1)
679
);
680
 
681 21 robfinch
/* Need to account for signed division
682
reg [35:0] divtbl [0:4095];
683
genvar g;
684
generate begin: gDivtbl
685
        for (g = 0; g < 4096; g = g + 1)
686
                divtbl[g] = 36'h800000000 / g;
687
endgenerate
688
wire [`DBLBYTE] divres = ({acca,accb} * divtbl[b12]) >> 36;
689
wire [11:0] divrem = {acca,accb} - divres * b12;
690
*/
691
 
692 2 robfinch
// For asynchronous reads,
693
// The read response might come back in any order (the packets could loop
694
// around in the network.
695
// We need to buffer and reorder the response correctly.
696
 
697
integer n3;
698
always_ff @(posedge clk_i)
699
if (rst_i) begin
700
        icgot <= 16'h0;
701
        for (n3 = 0; n3 < 16; n3 = n3 + 1)
702
                icbuf[n3] <= {bitsPerByte{1'b0}};
703
end
704
else begin
705
        if (state==ICACHE1)
706
                icgot <= 16'h0;
707
`ifdef SUPPORT_AREAD
708
        if (aack_i) begin
709
                icgot[atag_i] <= 1'b1;
710
                icbuf[atag_i] <= dati;
711
        end
712
`else
713
        if (ack_i) begin
714
                icgot[adr_o[3:0]] <= 1'b1;
715
                icbuf[adr_o[3:0]] <= dati;
716
        end
717
`endif
718
end
719
 
720
genvar g;
721
generate begin : gIcin
722
for (g = 0; g < 16; g = g + 1)
723
        always_comb
724
                icbuf2[(g+1)*bitsPerByte-1:g*bitsPerByte] <= icbuf[g];
725
end
726
endgenerate
727
 
728
// Bus timeout counter
729
always_ff @(posedge clk_i)
730
if (rst_i) begin
731
        btocnt <= 24'd0;
732
end
733
else begin
734
        if (cyc_o & stb_o)
735
                btocnt <= btocnt + 2'd1;
736
        else
737
                btocnt <= 24'd0;
738
end
739
always_comb
740
        bto = btocnt >= 24'd10000;
741
 
742
// Count  milliseconds
743
// Based on a count determined by the clock frequency
744
// 40MHz is assumed.
745
reg [23:0] ns_count;    // The counter to get to 1ms
746
reg [35:0] ms_count;    // Count of number of milliseconds
747
 
748
always_ff @(posedge clk_i)
749
if (rst_i) begin
750
        ns_count <= 16'd0;
751
        ms_count <= 36'd0;
752
end
753
else begin
754
        ns_count <= ns_count + 2'd1;
755
        if (ns_count>=24'd40000) begin
756
                ns_count <= 24'h0;
757
                ms_count <= ms_count + 2'd1;
758
        end
759
end
760
 
761
`ifdef SUPPORT_CHECKPOINT
762
always_ff @(posedge clk_i)
763
if (rst_i)
764
        chkpoint <= 12'h000;
765
else begin
766
        if (ns_count==16'd40000) begin
767
                if (ms_count[9:0]==10'h3FF)
768
                        chkpoint <= 12'hFFF;
769
        end
770
        if (state==STORE1 && (wadr=={{BPB*3-8{1'b1}},8'hE1}))
771
                chkpoint <= 12'h000;
772
end
773
`endif
774
 
775
always_ff @(posedge clk_i)
776
        tsc_latched <= tsc_i;
777
 
778
always_ff @(posedge clk_i)
779
        nmi1 <= nmi_i;
780
always_ff @(posedge clk_i)
781
`ifdef SUPPORT_CHECKPOINT
782
        if (ms_count[9:0]==10'h3FF && chkpoint!=12'h000)
783
                nmi_edge <= 1'b1;
784
        else
785
`endif
786
        if (nmi_i & !nmi1)
787
                nmi_edge <= 1'b1;
788
        else if (state==DECODE && ir12==`INT)
789
                nmi_edge <= 1'b0;
790
 
791 21 robfinch
reg [11:0] rst_cnt;
792 2 robfinch
 
793
always @(posedge clk_i)
794
if (rst_i) begin
795
        wb_nack();
796
        rty <= `FALSE;
797
        rst_cnt <= {id,4'd0};
798
        next_state(RESET);
799
        sync_state <= `FALSE;
800
        wait_state <= `FALSE;
801
        md32 <= `FALSE;
802
        ipg <= 2'b00;
803
        isFar <= `FALSE;
804 13 robfinch
`ifdef EIGHTBIT
805 2 robfinch
        isOuterIndexed <= `FALSE;
806 13 robfinch
`endif
807 2 robfinch
        dpr <= 12'h000;
808
        ibufadr <= {BPB*3{1'b0}};
809
//      pc <= 24'hFFFFFE;
810
        pc <= {{BPB*3-1{1'b1}},1'b0};   // FF...FE
811
        ir <= {4{`NOP}};
812
        ibuf <= {4{`NOP}};
813
        im <= 1'b1;
814
        firqim <= 1'b1;
815
        nmi_armed <= `FALSE;
816
        ic_invalidate <= `TRUE;
817
        first_ifetch <= `TRUE;
818
        acca <= 12'h0;
819
        accb <= 12'h0;
820
        accd <= 24'h0;
821
        xr <= 24'h0;
822
        yr <= 24'h0;
823 15 robfinch
        usppg <= 16'h0;
824 2 robfinch
        usp <= 24'h0;
825
        ssp <= 24'h0;
826
        if (halt_i) begin
827
                ba_o <= 1'b1;
828
                bs_o <= 1'b1;
829
        end
830
        else begin
831
                ba_o <= 1'b0;
832
                bs_o <= 1'b0;
833
        end
834
        outstanding <= 16'h0;
835
        iccnt <= 4'h0;
836
end
837
else begin
838
 
839
// Release any bus lock during the last state of an instruction.
840
if (lic_o && ack_i && (state==STORE2 || state==LOAD2))
841
        lock_o <= 1'b0;
842
 
843
case(state)
844
RESET:
845
        if (rst_cnt==10'd0) begin
846
                ic_invalidate <= `FALSE;
847
                ba_o <= 1'b0;
848
                bs_o <= 1'b0;
849
                vect <= `RST_VECT;
850
                radr <= `RST_VECT;
851
                load_what <= `LW_PCH;
852
                next_state(LOAD1);
853
        end
854
        else
855
                rst_cnt <= rst_cnt - 2'd1;
856
 
857
IFETCH:
858
        begin
859
                tIfetch();
860
                tWriteback();
861
        end
862
DECODE: tDecode();
863
LOAD1:  tLoad1();
864
LOAD2:  tLoad2();
865
CALC:           tExecute();
866
STORE1: tStore1();
867 21 robfinch
STORE1a:        tStore1a();
868 2 robfinch
STORE2: tStore2();
869
 
870
// ============================================================================
871
// ============================================================================
872
PUSH1:
873
        begin
874
                next_state(PUSH2);
875
                if (isINT | isPSHS) begin
876
                        wadr <= (ssp - cnt);
877
                        ssp <= (ssp - cnt);
878
                end
879
                else begin      // PSHU
880 15 robfinch
                        wadr <= ({usppg,{bitsPerByte{1'b0}}} + usp - cnt);
881 2 robfinch
                        usp <= (usp - cnt);
882
                end
883
        end
884
PUSH2:
885
        begin
886
                next_state(STORE1);
887
                if (ir[bitsPerByte]) begin
888
                        store_what <= `SW_CCR;
889
                        ir[bitsPerByte] <= 1'b0;
890
                end
891
                else if (ir[bitsPerByte+1]) begin
892
                        store_what <= `SW_ACCA;
893
                        ir[bitsPerByte+1] <= 1'b0;
894
                end
895
                else if (ir[bitsPerByte+2]) begin
896
                        store_what <= `SW_ACCB;
897
                        ir[bitsPerByte+2] <= 1'b0;
898
                end
899 21 robfinch
`ifdef SUPPORT_6309
900
                else if (ir[bitsPerByte+8]) begin
901
                        store_what <= `SW_ACCE;
902
                        ir[bitsPerByte+8] <= 1'b0;
903
                end
904
                else if (ir[bitsPerByte+9]) begin
905
                        store_what <= `SW_ACCF;
906
                        ir[bitsPerByte+9] <= 1'b0;
907
                end
908
`endif
909 2 robfinch
                else if (ir[bitsPerByte+3]) begin
910
                        store_what <= `SW_DPR;
911
                        ir[bitsPerByte+3] <= 1'b0;
912
                end
913
                else if (ir[bitsPerByte+4]) begin
914
                        store_what <= `SW_XH;
915
                        ir[bitsPerByte+4] <= 1'b0;
916
                end
917
                else if (ir[bitsPerByte+5]) begin
918
                        store_what <= `SW_YH;
919
                        ir[bitsPerByte+5] <= 1'b0;
920
                end
921
                else if (ir[bitsPerByte+6]) begin
922
                        if (isINT | isPSHS)
923
                                store_what <= `SW_USPH;
924
                        else
925
                                store_what <= `SW_SSPH;
926
                        ir[bitsPerByte+6] <= 1'b0;
927
                end
928
                else if (ir[bitsPerByte+7]) begin
929
                        store_what <= isFar ? `SW_PC2316 : `SW_PCH;
930
                        ir[bitsPerByte+7] <= 1'b0;
931
                end
932
                else begin
933
                        if (isINT) begin
934
                                radr <= vect;
935
                                if (vec_i != 24'h0) begin
936
                                        $display("vector: %h", vec_i);
937
                                        pc <= vec_i;
938
                                        next_state(IFETCH);
939
                                end
940
                                else begin
941
                                        pc[`BYTE3] <= 8'h00;
942
                                        load_what <= `LW_PCH;
943
                                        next_state(LOAD1);
944
                                end
945
                        end
946
                        else
947
                                next_state(IFETCH);
948
                end
949
        end
950
PULL1:
951
        begin
952
                next_state(LOAD1);
953
                if (ir[bitsPerByte]) begin
954
                        load_what <= `LW_CCR;
955
                        ir[bitsPerByte] <= 1'b0;
956
                end
957
                else if (ir[bitsPerByte+1]) begin
958
                        load_what <= `LW_ACCA;
959
                        ir[bitsPerByte+1] <= 1'b0;
960
                end
961
                else if (ir[bitsPerByte+2]) begin
962
                        load_what <= `LW_ACCB;
963
                        ir[bitsPerByte+2] <= 1'b0;
964
                end
965 21 robfinch
`ifdef SUPPORT_6309
966
                else if (ir[bitsPerByte+8]) begin
967
                        load_what <= `LW_ACCE;
968
                        ir[bitsPerByte+8] <= 1'b0;
969
                end
970
                else if (ir[bitsPerByte+9]) begin
971
                        load_what <= `LW_ACCF;
972
                        ir[bitsPerByte+9] <= 1'b0;
973
                end
974
`endif
975 2 robfinch
                else if (ir[bitsPerByte+3]) begin
976
                        load_what <= `LW_DPR;
977
                        ir[bitsPerByte+3] <= 1'b0;
978
                end
979
                else if (ir[bitsPerByte+4]) begin
980
                        load_what <= `LW_XH;
981
                        ir[bitsPerByte+4] <= 1'b0;
982
                end
983
                else if (ir[bitsPerByte+5]) begin
984
                        load_what <= `LW_YH;
985
                        ir[bitsPerByte+5] <= 1'b0;
986
                end
987
                else if (ir[bitsPerByte+6]) begin
988
                        if (ir12==`PULU)
989
                                load_what <= `LW_SSPH;
990
                        else
991
                                load_what <= `LW_USPH;
992
                        ir[bitsPerByte+6] <= 1'b0;
993
                end
994
                else if (ir[bitsPerByte+7]) begin
995
                        load_what <= isFar ? `LW_PC2316 : `LW_PCH;
996
                        ir[bitsPerByte+7] <= 1'b0;
997
                end
998
                else
999
                        next_state(IFETCH);
1000
        end
1001
 
1002
// ----------------------------------------------------------------------------
1003
// Outer Indexing Support
1004
// ----------------------------------------------------------------------------
1005
OUTER_INDEXING:
1006
        begin
1007
                if (bitsPerByte==8) begin
1008 12 robfinch
                        casez(ndxbyte)
1009
                        8'b0???????:    radr <= radr + ndxreg;
1010
                        8'b1???0000:
1011 2 robfinch
                                                        begin
1012
                                                                radr <= radr + ndxreg;
1013
                                                                case(ndxbyte[6:5])
1014
                                                                2'b00:  xr <= (xr + 2'd1);
1015
                                                                2'b01:  yr <= (yr + 2'd1);
1016
                                                                2'b10:  usp <= (usp + 2'd1);
1017
                                                                2'b11:  ssp <= (ssp + 2'd1);
1018
                                                                endcase
1019
                                                        end
1020 12 robfinch
                        8'b1???0001:    begin
1021 2 robfinch
                                                                radr <= radr + ndxreg;
1022
                                                                case(ndxbyte[6:5])
1023
                                                                2'b00:  xr <= (xr + 2'd2);
1024
                                                                2'b01:  yr <= (yr + 2'd2);
1025
                                                                2'b10:  usp <= (usp + 2'd2);
1026
                                                                2'b11:  ssp <= (ssp + 2'd2);
1027
                                                                endcase
1028
                                                        end
1029 12 robfinch
                        8'b1???0010:    radr <= radr + ndxreg;
1030
                        8'b1???0011:    radr <= radr + ndxreg;
1031
                        8'b1???0100:    radr <= radr + ndxreg;
1032
                        8'b1???0101:    radr <= radr + ndxreg;
1033
                        8'b1???0110:    radr <= radr + ndxreg;
1034
                        8'b1???1000:    radr <= radr + ndxreg;
1035
                        8'b1???1001:    radr <= radr + ndxreg;
1036
                        8'b1???1010:    radr <= radr + ndxreg;
1037
                        8'b1???1011:    radr <= radr + ndxreg;
1038 2 robfinch
                        default:        radr <= radr;
1039
                        endcase
1040
                end
1041
                else if (bitsPerByte==12) begin
1042 12 robfinch
                        casez(ndxbyte)
1043
                        12'b0???????????:       radr <= radr + ndxreg;
1044
                        12'b1????0000000:
1045 2 robfinch
                                                        begin
1046
                                                                radr <= radr + ndxreg;
1047
                                                                case(ndxbyte[10:9])
1048
                                                                2'b00:  xr <= (xr + 2'd1);
1049
                                                                2'b01:  yr <= (yr + 2'd1);
1050
                                                                2'b10:  usp <= (usp + 2'd1);
1051
                                                                2'b11:  ssp <= (ssp + 2'd1);
1052
                                                                endcase
1053
                                                        end
1054 12 robfinch
                        12'b1????0000001:       begin
1055 2 robfinch
                                                                radr <= radr + ndxreg;
1056
                                                                case(ndxbyte[10:9])
1057
                                                                2'b00:  xr <= (xr + 2'd2);
1058
                                                                2'b01:  yr <= (yr + 2'd2);
1059
                                                                2'b10:  usp <= (usp + 2'd2);
1060
                                                                2'b11:  ssp <= (ssp + 2'd2);
1061
                                                                endcase
1062
                                                        end
1063 12 robfinch
                        12'b1????0000010:       radr <= radr + ndxreg;
1064
                        12'b1????0000011:       radr <= radr + ndxreg;
1065
                        12'b1????0000100:       radr <= radr + ndxreg;
1066
                        12'b1????0000101:       radr <= radr + ndxreg;
1067
                        12'b1????0000110:       radr <= radr + ndxreg;
1068
                        12'b1????0001000:       radr <= radr + ndxreg;
1069
                        12'b1????0001001:       radr <= radr + ndxreg;
1070
                        12'b1????0001010:       radr <= radr + ndxreg;
1071
                        12'b1????0001011:       radr <= radr + ndxreg;
1072 2 robfinch
                        default:        radr <= radr;
1073
                        endcase
1074
                end
1075
                next_state(OUTER_INDEXING2);
1076
        end
1077
OUTER_INDEXING2:
1078
        begin
1079
                wadr <= radr;
1080
                res <= radr[`DBLBYTE];
1081
                load_what <= load_what2;
1082
                if (isLEA)
1083
                        next_state(IFETCH);
1084
                else if (isStore)
1085
                        next_state(STORE1);
1086
                else
1087
                        next_state(LOAD1);
1088
        end
1089
 
1090
// ============================================================================
1091
// Cache Control
1092
// ============================================================================
1093
ICACHE1:
1094
        begin
1095
                iccnt <= 4'h0;
1096
                outstanding <= 16'h0;
1097
                if (hit0 & hit1)
1098
                        next_state(IFETCH);
1099
                else if (!tsc && !ack_i) begin
1100
                        rhit0 <= hit0;
1101
                        bte_o <= 2'b00;
1102
                        cti_o <= 3'b001;
1103
                        cyc_o <= 1'b1;
1104
                        bl_o <= 6'd15;
1105
                        stb_o <= 1'b1;
1106
                        we_o <= 1'b0;
1107
                        adr_o <= !hit0 ? {pc[bitsPerByte*3-1:4],4'b00} : {pcp16[bitsPerByte*3-1:4],4'b0000};
1108
                        dat_o <= 12'd0;
1109
                        next_state(ICACHE2);
1110
                end
1111
        end
1112
// If tsc is asserted during an instruction cache fetch, then abort the fetch
1113
// cycle, and wait until tsc deactivates.
1114
// The instruction cache uses asynchronous reading through the network for
1115
// better performance. The read request and the read response are two
1116
// separate things.
1117
ICACHE2:
1118
`ifdef SUPPORT_AREAD
1119
        if (tsc) begin
1120
                wb_nack();
1121
                next_state(ICACHE3);
1122
        end
1123
        else if (ack_i|rty_i|bto) begin
1124
                stb_o <= 1'b0;
1125
                iccnt <= iccnt + 2'd1;
1126
                next_state(ICACHE4);
1127
                if (iccnt==4'b1110)
1128
                        cti_o <= 3'b111;
1129
                if (iccnt==4'b1111) begin
1130
                        icwa <= adr_o;
1131
                        wb_nack();
1132
                        next_state(ICACHE5);
1133
                end
1134
        end
1135
`else
1136
        if (tsc|rty_i) begin
1137
                wb_nack();
1138
                next_state(ICACHE3);
1139
        end
1140
        else if (ack_i) begin
1141
                stb_o <= 1'b0;
1142
                iccnt <= iccnt + 2'd1;
1143
                next_state(ICACHE4);
1144
                if (iccnt==4'b1110)
1145
                        cti_o <= 3'b111;
1146
                if (iccnt==4'b1111) begin
1147
                        icwa <= adr_o;
1148
                        wb_nack();
1149
                        next_state(ICACHE6);
1150
                end
1151
        end
1152
`endif
1153
 
1154
ICACHE4:
1155
        if (!ack_i) begin
1156
                adr_o[3:0] <= iccnt;
1157
                stb_o <= 1'b1;
1158
                next_state(ICACHE2);
1159
        end
1160
 
1161
ICACHE6:
1162
        next_state(ICACHE1);
1163
 
1164
// The following states to handle outstanding transfers.
1165
// The transfer might retry several times if it has not registered.
1166
`ifdef SUPPORT_AREAD
1167
ICACHE5:
1168
        // Line loaded?
1169
        if (icgot == 16'hFFFF)
1170
                next_state(ICACHE6);
1171
        else begin
1172
                waitcnt <= 6'd20;
1173
                next_state(ICACHE7);
1174
        end
1175
ICACHE7:
1176
        if (waitcnt==6'd0) begin
1177 11 robfinch
                next_state(ICACHE5);
1178 2 robfinch
                adr_o <= icwa;
1179
                for (n4 = 15; n4 >= 0; n4 = n4 - 1)
1180 11 robfinch
                        if (~icgot[n4]) begin// & ~outstanding[n4]) begin
1181 2 robfinch
                                cti_o <= 3'b001;
1182 11 robfinch
                                cyc_o <= `TRUE;
1183
                                stb_o <= `TRUE;
1184 2 robfinch
                                adr_o[3:0] <= n4[3:0];
1185
                                outstanding[n4[3:0]] <= 1'b1;
1186
                                next_state(ICACHE9);
1187
                        end
1188
        end
1189
        else
1190
                waitcnt <= waitcnt - 2'd1;
1191
ICACHE9:
1192
        begin
1193
                if (bto)
1194
                        outstanding <= 16'h0;
1195
                if (aack_i)
1196 11 robfinch
                        outstanding[atag_i] <= 1'b0;
1197 2 robfinch
                if (ack_i|rty_i|bto) begin
1198
                        wb_nack();
1199
                        waitcnt <= 6'd20;
1200
                        next_state(ICACHE7);
1201
                end
1202
        end
1203
`endif
1204
 
1205
// Restart a cache load aborted by the TSC signal. A registered version of the
1206
// hit signal must be used as the cache may be partially updated.
1207
ICACHE3:
1208
        if (!tsc) begin
1209
                bte_o <= 2'b00;
1210
                cti_o <= 3'b001;
1211
                cyc_o <= 1'b1;
1212
                bl_o <= 6'd15;
1213
                stb_o <= 1'b1;
1214
                we_o <= 1'b0;
1215
                adr_o <= !rhit0 ? {pc[bitsPerByte*3-1:4],4'b00} : {pcp16[bitsPerByte*3-1:4],4'b0000};
1216
                dat_o <= 12'd0;
1217
                next_state(ICACHE2);
1218
        end
1219
 
1220
`ifdef SUPPORT_IBUF
1221
IBUF1:
1222
        if (!tsc) begin
1223
                bte_o <= 2'b00;
1224
                cti_o <= 3'b001;
1225
                cyc_o <= 1'b1;
1226
                bl_o <= 6'd2;
1227
                stb_o <= 1'b1;
1228
                we_o <= 1'b0;
1229
                adr_o <= pc[`DBLBYTE];
1230
                dat_o <= 12'd0;
1231
                next_state(IBUF2);
1232
        end
1233
IBUF2:
1234
        if (tsc|rty_i) begin
1235
                wb_nack();
1236
                next_state(IBUF1);
1237
        end
1238
        else if (ack_i) begin
1239
                adr_o <= adr_o + 2'd1;
1240
                ibuf <= dat_i;
1241
                next_state(IBUF3);
1242
        end
1243
IBUF3:
1244
        if (tsc|rty_i) begin
1245
                wb_nack();
1246
                next_state(IBUF1);
1247
        end
1248
        else if (ack_i) begin
1249
                cti_o <= 3'b111;
1250
                adr_o <= adr_o + 2'd1;
1251
                ibuf[`HIBYTE] <= dat_i;
1252
                next_state(IBUF4);
1253
        end
1254
IBUF4:
1255
        if (tsc|rty_i) begin
1256
                wb_nack();
1257
                next_state(IBUF1);
1258
        end
1259
        else if (ack_i) begin
1260
                wb_nack();
1261
                ibuf[`BYTE3] <= dat_i;
1262
                next_state(IBUF5);
1263
        end
1264
IBUF5:
1265
        if (tsc|rty_i) begin
1266
                wb_nack();
1267
                next_state(IBUF1);
1268
        end
1269
        else if (ack_i) begin
1270
                wb_nack();
1271
                ibuf[`BYTE4] <= dat_i;
1272
                next_state(IBUF6);
1273
        end
1274
IBUF6:
1275
        if (tsc|rty_i) begin
1276
                wb_nack();
1277
                next_state(IBUF1);
1278
        end
1279
        else if (ack_i) begin
1280
                wb_nack();
1281
                ibuf[`BYTE5] <= dat_i;
1282
                ibufadr <= pc;
1283
                next_state(IFETCH);
1284
        end
1285
`endif
1286
 
1287
endcase
1288
end
1289
 
1290
// ============================================================================
1291
// ============================================================================
1292
// Supporting Tasks
1293
// ============================================================================
1294
// ============================================================================
1295
 
1296
// ============================================================================
1297
// IFETCH
1298
//
1299
// Fetch instructions.
1300
// ============================================================================
1301
 
1302
task tIfetch;
1303
begin
1304
        if (halt_i) begin
1305
                ba_o <= 1'b1;
1306
                bs_o <= 1'b1;
1307
        end
1308
        else begin
1309
                ba_o <= 1'b0;
1310
                bs_o <= 1'b0;
1311
                next_state(DECODE);
1312
                isFar <= `FALSE;
1313 13 robfinch
`ifdef EIGHTBIT
1314 2 robfinch
                isOuterIndexed <= `FALSE;
1315 13 robfinch
`endif
1316 2 robfinch
                ipg <= 2'b00;
1317 12 robfinch
                ia <= {bitsPerByte*3{1'b0}};
1318 2 robfinch
                res <= 24'd0;
1319
                load_what <= `LW_NOTHING;
1320
                store_what <= `SW_NOTHING;
1321
                if (nmi_edge | firq_i | irq_i)
1322
                        sync_state <= `FALSE;
1323
                if (nmi_edge & nmi_armed) begin
1324
                        bs_o <= 1'b1;
1325
                        ir[`LOBYTE] <= `INT;
1326
                        ipg <= 2'b11;
1327
                        vect <= `NMI_VECT;
1328
                end
1329
                else if (firq_i & !firqim & !sync_state) begin
1330
                        bs_o <= 1'b1;
1331
                        ir[`LOBYTE] <= `INT;
1332
                        ipg <= 2'b11;
1333
                        vect <= `FIRQ_VECT;
1334
                end
1335
                else if (irq_i & !im & !sync_state) begin
1336
                        $display("**************************************");
1337
                        $display("****** Interrupt *********************");
1338
                        $display("**************************************");
1339
                        bs_o <= 1'b1;
1340
                        ir[`LOBYTE] <= `INT;
1341
                        ipg <= 2'b11;
1342
                        vect <= `IRQ_VECT;
1343
                end
1344
                else begin
1345
                        if (sync_state) begin
1346
                                ba_o <= 1'b1;
1347
                                next_state(IFETCH);
1348
                        end
1349
                        else if (icacheOn) begin
1350
                                if (ihit) begin
1351
                                        ir <= insn;
1352
                                end
1353
                                else begin
1354
                                        ipg <= ipg;
1355
                                        isFar <= isFar;
1356 13 robfinch
`ifdef EIGHTBIT
1357 2 robfinch
                                        isOuterIndexed <= isOuterIndexed;
1358 13 robfinch
`endif
1359 2 robfinch
                                        next_state(ICACHE1);
1360
                                end
1361
                        end
1362
`ifdef SUPPORT_IBUF
1363
                        else begin
1364
                                if (ibufhit)
1365
                                        ir <= ibuf;
1366
                                else begin
1367
                                        ipg <= ipg;
1368
                                        isFar <= isFar;
1369 13 robfinch
`ifdef EIGHTBIT
1370 2 robfinch
                                        isOuterIndexed <= isOuterIndexed;
1371 13 robfinch
`endif
1372 2 robfinch
                                        next_state(IBUF1);
1373
                                end
1374
                        end
1375
`endif
1376
                end
1377
        end
1378
end
1379
endtask
1380
 
1381
// ============================================================================
1382
// DECODE
1383
//
1384
// Decode instruction and fetch register file values.
1385
// ============================================================================
1386
 
1387
task tDecode;
1388
begin
1389
        first_ifetch <= `TRUE;
1390
        next_state(IFETCH);             // default: move to IFETCH
1391
        pc <= pc + 2'd1;                // default: increment PC by one
1392
        a <= 24'd0;
1393
        b <= 24'd0;
1394 12 robfinch
        ia <= {bitsPerByte * 3{1'b0}};
1395 2 robfinch
        isDblIndirect <= `FALSE;//ndxbyte[11:4]==8'h8F;
1396
        if (isIndexed) begin
1397
                if (bitsPerByte==8) begin
1398
                        casez(ndxbyte)
1399
                        8'b1??00000:
1400
                                if (!isOuterIndexed)
1401
                                        case(ndxbyte[6:5])
1402
                                        2'b00:  xr <= (xr + 4'd1);
1403
                                        2'b01:  yr <= (yr + 4'd1);
1404
                                        2'b10:  usp <= (usp + 4'd1);
1405
                                        2'b11:  ssp <= (ssp + 4'd1);
1406
                                        endcase
1407
                        8'b1??00001:
1408
                                if (!isOuterIndexed)
1409
                                        case(ndxbyte[6:5])
1410
                                        2'b00:  xr <= (xr + 4'd2);
1411
                                        2'b01:  yr <= (yr + 4'd2);
1412
                                        2'b10:  usp <= (usp + 4'd2);
1413
                                        2'b11:  ssp <= (ssp + 4'd2);
1414
                                        endcase
1415
                        8'b1??00010:
1416
                                case(ndxbyte[6:5])
1417
                                2'b00:  xr <= (xr - 2'd1);
1418
                                2'b01:  yr <= (yr - 2'd1);
1419
                                2'b10:  usp <= (usp - 2'd1);
1420
                                2'b11:  ssp <= (ssp - 2'd1);
1421
                                endcase
1422
                        8'b1??00011:
1423
                                case(ndxbyte[6:5])
1424
                                2'b00:  xr <= (xr - 2'd2);
1425
                                2'b01:  yr <= (yr - 2'd2);
1426
                                2'b10:  usp <= (usp - 2'd2);
1427
                                2'b11:  ssp <= (ssp - 2'd2);
1428
                                endcase
1429
                        endcase
1430
                end
1431
                else if (bitsPerByte==12) begin
1432
                        casez(ndxbyte)
1433
                        12'b1??000000000:
1434 13 robfinch
                                if (!isOuterIndexed && ndxbyte[bitsPerByte-5]==1'b0)
1435 2 robfinch
                                        case(ndxbyte[10:9])
1436
                                        2'b00:  xr <= (xr + 4'd1);
1437
                                        2'b01:  yr <= (yr + 4'd1);
1438
                                        2'b10:  usp <= (usp + 4'd1);
1439
                                        2'b11:  ssp <= (ssp + 4'd1);
1440
                                        endcase
1441
                        12'b1??000000001:
1442 13 robfinch
                                if (!isOuterIndexed && ndxbyte[bitsPerByte-5]==1'b0)
1443 2 robfinch
                                        case(ndxbyte[10:9])
1444
                                        2'b00:  xr <= (xr + 4'd2);
1445
                                        2'b01:  yr <= (yr + 4'd2);
1446
                                        2'b10:  usp <= (usp + 4'd2);
1447
                                        2'b11:  ssp <= (ssp + 4'd2);
1448
                                        endcase
1449 15 robfinch
                        12'b1??0?0000010:
1450 2 robfinch
                                case(ndxbyte[10:9])
1451
                                2'b00:  xr <= (xr - 2'd1);
1452
                                2'b01:  yr <= (yr - 2'd1);
1453
                                2'b10:  usp <= (usp - 2'd1);
1454
                                2'b11:  ssp <= (ssp - 2'd1);
1455
                                endcase
1456 15 robfinch
                        12'b1??0?0000011:
1457 2 robfinch
                                case(ndxbyte[10:9])
1458
                                2'b00:  xr <= (xr - 2'd2);
1459
                                2'b01:  yr <= (yr - 2'd2);
1460
                                2'b10:  usp <= (usp - 2'd2);
1461
                                2'b11:  ssp <= (ssp - 2'd2);
1462
                                endcase
1463
                        endcase
1464
                end
1465
        end
1466
        case(ir12)
1467
        `NOP:   ;
1468
        `SYNC:  sync_state <= `TRUE;
1469
        `ORCC:  begin
1470
                        cf <= cf | ir[bitsPerByte];
1471
                        vf <= vf | ir[bitsPerByte+1];
1472
                        zf <= zf | ir[bitsPerByte+2];
1473
                        nf <= nf | ir[bitsPerByte+3];
1474
                        im <= im | ir[bitsPerByte+4];
1475
                        hf <= hf | ir[bitsPerByte+5];
1476
                        firqim <= firqim | ir[bitsPerByte+6];
1477
                        ef <= ef | ir[bitsPerByte+7];
1478
                        pc <= pcp2;
1479
                        end
1480
        `ANDCC:
1481
                        begin
1482
                        cf <= cf & ir[bitsPerByte];
1483
                        vf <= vf & ir[bitsPerByte+1];
1484
                        zf <= zf & ir[bitsPerByte+2];
1485
                        nf <= nf & ir[bitsPerByte+3];
1486
                        im <= im & ir[bitsPerByte+4];
1487
                        hf <= hf & ir[bitsPerByte+5];
1488
                        firqim <= firqim & ir[bitsPerByte+6];
1489
                        ef <= ef & ir[bitsPerByte+7];
1490
                        pc <= pcp2;
1491
                        end
1492
        `DAA:
1493
                        begin
1494
                                if (hf || acca[3:0] > 4'd9)
1495
                                        res12[3:0] <= acca[3:0] + 4'd6;
1496
                                if (cf || acca[7:4] > 4'd9 || (acca[7:4] > 4'd8 && acca[3:0] > 4'd9))
1497
                                        res12[8:4] <= acca[7:4] + 4'd6;
1498
                        end
1499
        `CWAI:
1500
                        begin
1501
                        cf <= cf & ir[bitsPerByte];
1502
                        vf <= vf & ir[bitsPerByte+1];
1503
                        zf <= zf & ir[bitsPerByte+2];
1504
                        nf <= nf & ir[bitsPerByte+3];
1505
                        im <= im & ir[bitsPerByte+4];
1506
                        hf <= hf & ir[bitsPerByte+5];
1507
                        firqim <= firqim & ir[bitsPerByte+6];
1508
                        ef <= 1'b1;
1509
                        pc <= pc + 2'd2;
1510
                        ir[`HIBYTE] <= -1;
1511
                        isFar <= `TRUE;
1512
                        wait_state <= `TRUE;
1513
                        next_state(PUSH1);
1514
                        end
1515
        `LDMD:  begin
1516
                        natMd <= ir[bitsPerByte];
1517
                        firqMd <= ir[bitsPerByte+1];
1518
                        pc <= pc + 2'd2;
1519
                        end
1520
        `TFR:   pc <= pc + 2'd2;
1521
        `EXG:   pc <= pc + 2'd2;
1522
        `ABX:   res <= xr + accb;
1523
        `SEX: res <= {{bitsPerByte{accb[BPBM1]}},accb[`LOBYTE]};
1524
        `PG2:   begin ipg <= 2'b01; ir <= ir[bitsPerByte*5-1:bitsPerByte]; next_state(DECODE); end
1525
        `PG3:   begin ipg <= 2'b10; ir <= ir[bitsPerByte*5-1:bitsPerByte]; next_state(DECODE); end
1526
        `FAR:   begin isFar <= `TRUE;  ir <= ir[bitsPerByte*5-1:bitsPerByte]; next_state(DECODE); end
1527 13 robfinch
`ifdef EIGHTBIT
1528 2 robfinch
        `OUTER: begin isOuterIndexed <= `TRUE;  ir <= ir[bitsPerByte*5-1:bitsPerByte]; next_state(DECODE); end
1529 13 robfinch
`endif
1530 2 robfinch
        `NEGA,`NEGB:    begin res12 <= -acc[`LOBYTE]; a <= 24'h00; b <= acc; end
1531
        `COMA,`COMB:    begin res12 <= ~acc[`LOBYTE]; end
1532
        `LSRA,`LSRB:    begin res12 <= {acc[0],1'b0,acc[BPBM1:1]}; end
1533
        `RORA,`RORB:    begin res12 <= {acc[0],cf,acc[BPBM1:1]}; end
1534
        `ASRA,`ASRB:    begin res12 <= {acc[0],acc[BPBM1],acc[BPBM1:1]}; end
1535
        `ASLA,`ASLB:    begin res12 <= {acc[`LOBYTE],1'b0}; end
1536
        `ROLA,`ROLB:    begin res12 <= {acc[`LOBYTE],cf}; end
1537
        `DECA,`DECB:    begin res12 <= acc[`LOBYTE] - 2'd1; end
1538
        `INCA,`INCB:    begin res12 <= acc[`LOBYTE] + 2'd1; end
1539
        `TSTA,`TSTB:    begin res12 <= acc[`LOBYTE]; end
1540
        `CLRA,`CLRB:    begin res12 <= 13'h000; end
1541 21 robfinch
`ifdef SUPPORT_6309
1542
        `TSTD:  res <= {acca,accb};
1543
        `TSTW:  res <= {acce,accf};
1544
        `TSTE:  res12 <= acce;
1545
        `TSTF:  res12 <= accf;
1546
        `NEGD:  begin res <= -{acca,accb}; a <= 'd0; b <= {acca,accb}; end
1547
        `INCE,`INCF:    begin res12 <= acc[`LOBYTE] + 2'd1; end
1548
        `INCD:  res <= {acca,accb} + 2'd1;
1549
        `INCW:  res <= {acce,accf} + 2'd1;
1550
        `DECE,`DECF:    begin res12 <= acc[`LOBYTE] - 2'd1; end
1551
        `DECD:  res <= {acca,accb} - 2'd1;
1552
        `DECW:  res <= {acce,accf} - 2'd1;
1553
        `COMD:  res <= ~{acca,accb};
1554
        `COME,`COMF:    res <= ~acc[`LOBYTE];
1555
        `COMW:  res <= ~{acce,accf};
1556
        `CLRD:  res <= 'b0;
1557
        `CLRW:  res <= 'b0;
1558
        `CLRE:  res12 <= 'b0;
1559
        `CLRF:  res12 <= 'b0;
1560
        `ASLD:
1561
                res <= {acca,accb,1'b0};
1562
        `ASRD:
1563
                res <= {accb[0],acca[bitsPerByte-1],acca,accb[bitsPerByte-1:1]};
1564
        `LSRD:
1565
                res <= {accb[0],acca,accb[bitsPerByte-1:1]};
1566
        `LSRW:
1567
                res <= {accf[0],accw,accf[bitsPerByte-1:1]};
1568
        `ROLD:
1569
                res <= {acca,accb,cf};
1570
        `ROLW:
1571
                res <= {acce,accf,cf};
1572
        `RORD:
1573
                res <= {accb[0],cf,acca,accb[bitsPerByte-1:1]};
1574
        `RORW:
1575
                res <= {accf[0],cf,acce,accf[bitsPerByte-1:1]};
1576
        `ADDR:
1577
                begin
1578
                        case(ir[bitsPerByte+3:bitsPerByte])
1579
                        4'b0000:        begin {acca,accb} <= sum12; nf <= sum12[bitsPerByte*2-1]; zf <= sum12[`DBLBYTE]=='b0; cf <= sum12[bitsPerByte*2]; vf <= fnAddOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],sum12[bitsPerByte*2-1]); end
1580
                        4'b0001:        begin xr <= sum12; nf <= sum12[bitsPerByte*2-1]; zf <= sum12[`DBLBYTE]=='b0; cf <= sum12[bitsPerByte*2]; vf <= fnAddOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],sum12[bitsPerByte*2-1]); end
1581
                        4'b0010:        begin yr <= sum12; nf <= sum12[bitsPerByte*2-1]; zf <= sum12[`DBLBYTE]=='b0; cf <= sum12[bitsPerByte*2]; vf <= fnAddOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],sum12[bitsPerByte*2-1]); end
1582
                        4'b0011:        begin usp <= sum12; nf <= sum12[bitsPerByte*2-1]; zf <= sum12[`DBLBYTE]=='b0; cf <= sum12[bitsPerByte*2]; vf <= fnAddOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],sum12[bitsPerByte*2-1]); end
1583
                        4'b0100:        begin ssp <= sum12; nf <= sum12[bitsPerByte*2-1]; zf <= sum12[`DBLBYTE]=='b0; cf <= sum12[bitsPerByte*2]; vf <= fnAddOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],sum12[bitsPerByte*2-1]); end
1584
                        4'b0101:        begin pc <= sum12; nf <= sum12[bitsPerByte*2-1]; zf <= sum12[`DBLBYTE]=='b0; cf <= sum12[bitsPerByte*2]; vf <= fnAddOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],sum12[bitsPerByte*2-1]); end
1585
                        4'b1000:        begin acca <= sum12; nf <= sum12[bitsPerByte-1]; zf <= sum12[`LOBYTE]=='b0; cf <= sum12[bitsPerByte]; vf <= fnAddOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],sum12[bitsPerByte-1]); end
1586
                        4'b1001:        begin accb <= sum12; nf <= sum12[bitsPerByte-1]; zf <= sum12[`LOBYTE]=='b0; cf <= sum12[bitsPerByte]; vf <= fnAddOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],sum12[bitsPerByte-1]); end
1587
                        4'b1010:
1588
                                begin
1589
                                        cf <= sum12[0];
1590
                                        vf <= sum12[1];
1591
                                        zf <= sum12[2];
1592
                                        nf <= sum12[3];
1593
                                        im <= sum12[4];
1594
                                        hf <= sum12[5];
1595
                                        firqim <= sum12[6];
1596
                                        ef <= sum12[7];
1597
                                end
1598
                        4'b1011:        begin dpr <= sum12; nf <= sum12[bitsPerByte-1]; zf <= sum12[`LOBYTE]=='b0; cf <= sum12[bitsPerByte]; vf <= fnAddOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],sum12[bitsPerByte-1]); end
1599
                        endcase
1600
                end
1601
        `ADCR:
1602
                begin
1603
                        case(ir[bitsPerByte+3:bitsPerByte])
1604
                        4'b0000:        begin {acca,accb} <= sum12c; nf <= sum12c[bitsPerByte*2-1]; zf <= sum12c[`DBLBYTE]=='b0; cf <= sum12c[bitsPerByte*2]; vf <= fnAddOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],sum12c[bitsPerByte*2-1]); end
1605
                        4'b0001:        begin xr <= sum12c; nf <= sum12c[bitsPerByte*2-1]; zf <= sum12c[`DBLBYTE]=='b0; cf <= sum12c[bitsPerByte*2]; vf <= fnAddOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],sum12c[bitsPerByte*2-1]); end
1606
                        4'b0010:        begin yr <= sum12c; nf <= sum12c[bitsPerByte*2-1]; zf <= sum12c[`DBLBYTE]=='b0; cf <= sum12c[bitsPerByte*2]; vf <= fnAddOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],sum12c[bitsPerByte*2-1]); end
1607
                        4'b0011:        begin usp <= sum12c; nf <= sum12c[bitsPerByte*2-1]; zf <= sum12c[`DBLBYTE]=='b0; cf <= sum12c[bitsPerByte*2]; vf <= fnAddOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],sum12c[bitsPerByte*2-1]); end
1608
                        4'b0100:        begin ssp <= sum12c; nf <= sum12c[bitsPerByte*2-1]; zf <= sum12c[`DBLBYTE]=='b0; cf <= sum12c[bitsPerByte*2]; vf <= fnAddOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],sum12c[bitsPerByte*2-1]); end
1609
                        4'b0101:        begin pc <= sum12c; nf <= sum12c[bitsPerByte*2-1]; zf <= sum12c[`DBLBYTE]=='b0; cf <= sum12c[bitsPerByte*2]; vf <= fnAddOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],sum12c[bitsPerByte*2-1]); end
1610
                        4'b1000:        begin acca <= sum12c; nf <= sum12c[bitsPerByte-1]; zf <= sum12c[`LOBYTE]=='b0; cf <= sum12c[bitsPerByte]; vf <= fnAddOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],sum12c[bitsPerByte-1]); end
1611
                        4'b1001:        begin accb <= sum12c; nf <= sum12c[bitsPerByte-1]; zf <= sum12c[`LOBYTE]=='b0; cf <= sum12c[bitsPerByte]; vf <= fnAddOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],sum12c[bitsPerByte-1]); end
1612
                        4'b1010:
1613
                                begin
1614
                                        cf <= sum12c[0];
1615
                                        vf <= sum12c[1];
1616
                                        zf <= sum12c[2];
1617
                                        nf <= sum12c[3];
1618
                                        im <= sum12c[4];
1619
                                        hf <= sum12c[5];
1620
                                        firqim <= sum12c[6];
1621
                                        ef <= sum12c[7];
1622
                                end
1623
                        4'b1011:        begin dpr <= sum12c; nf <= sum12c[bitsPerByte-1]; zf <= sum12c[`LOBYTE]=='b0; cf <= sum12c[bitsPerByte]; vf <= fnAddOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],sum12c[bitsPerByte-1]); end
1624
                        endcase
1625
                end
1626
        `ANDR:
1627
                begin
1628
                        case(ir[bitsPerByte+3:bitsPerByte])
1629
                        4'b0000:        begin {acca,accb} <= and12; nf <= and12[bitsPerByte*2-1]; zf <= and12[`DBLBYTE]=='b0; vf <= 1'b0; end
1630
                        4'b0001:        begin xr <= and12; nf <= and12[bitsPerByte*2-1]; zf <= and12[`DBLBYTE]=='b0; vf <= 1'b0; end
1631
                        4'b0010:        begin yr <= and12; nf <= and12[bitsPerByte*2-1]; zf <= and12[`DBLBYTE]=='b0; vf <= 1'b0; end
1632
                        4'b0011:        begin usp <= and12; nf <= and12[bitsPerByte*2-1]; zf <= and12[`DBLBYTE]=='b0; vf <= 1'b0; end
1633
                        4'b0100:        begin ssp <= and12; nf <= and12[bitsPerByte*2-1]; zf <= and12[`DBLBYTE]=='b0; vf <= 1'b0; end
1634
                        4'b0101:        begin pc <= and12; nf <= and12[bitsPerByte*2-1]; zf <= and12[`DBLBYTE]=='b0; vf <= 1'b0; end
1635
                        4'b1000:        begin acca <= and12; nf <= and12[bitsPerByte-1]; zf <= and12[`LOBYTE]=='b0; vf <= 1'b0; end
1636
                        4'b1001:        begin accb <= and12; nf <= and12[bitsPerByte-1]; zf <= and12[`LOBYTE]=='b0; vf <= 1'b0; end
1637
                        4'b1010:
1638
                                begin
1639
                                        cf <= and12[0];
1640
                                        vf <= and12[1];
1641
                                        zf <= and12[2];
1642
                                        nf <= and12[3];
1643
                                        im <= and12[4];
1644
                                        hf <= and12[5];
1645
                                        firqim <= and12[6];
1646
                                        ef <= and12[7];
1647
                                end
1648
                        4'b1011:        begin dpr <= and12; nf <= and12[bitsPerByte-1]; zf <= and12[`LOBYTE]=='b0; vf <= 1'b0; end
1649
                        endcase
1650
                end
1651
        `EORR:
1652
                begin
1653
                        case(ir[bitsPerByte+3:bitsPerByte])
1654
                        4'b0000:        begin {acca,accb} <= eor12; nf <= eor12[bitsPerByte*2-1]; zf <= eor12[`DBLBYTE]=='b0; vf <= 1'b0; end
1655
                        4'b0001:        begin xr <= eor12; nf <= eor12[bitsPerByte*2-1]; zf <= eor12[`DBLBYTE]=='b0; vf <= 1'b0; end
1656
                        4'b0010:        begin yr <= eor12; nf <= eor12[bitsPerByte*2-1]; zf <= eor12[`DBLBYTE]=='b0; vf <= 1'b0; end
1657
                        4'b0011:        begin usp <= eor12; nf <= eor12[bitsPerByte*2-1]; zf <= eor12[`DBLBYTE]=='b0; vf <= 1'b0; end
1658
                        4'b0100:        begin ssp <= eor12; nf <= eor12[bitsPerByte*2-1]; zf <= eor12[`DBLBYTE]=='b0; vf <= 1'b0; end
1659
                        4'b0101:        begin pc <= eor12; nf <= eor12[bitsPerByte*2-1]; zf <= eor12[`DBLBYTE]=='b0; vf <= 1'b0; end
1660
                        4'b1000:        begin acca <= eor12; nf <= eor12[bitsPerByte-1]; zf <= eor12[`LOBYTE]=='b0; vf <= 1'b0; end
1661
                        4'b1001:        begin accb <= eor12; nf <= eor12[bitsPerByte-1]; zf <= eor12[`LOBYTE]=='b0; vf <= 1'b0; end
1662
                        4'b1010:
1663
                                begin
1664
                                        cf <= eor12[0];
1665
                                        vf <= eor12[1];
1666
                                        zf <= eor12[2];
1667
                                        nf <= eor12[3];
1668
                                        im <= eor12[4];
1669
                                        hf <= eor12[5];
1670
                                        firqim <= eor12[6];
1671
                                        ef <= eor12[7];
1672
                                end
1673
                        4'b1011:        begin dpr <= eor12; nf <= eor12[bitsPerByte-1]; zf <= eor12[`LOBYTE]=='b0; vf <= 1'b0; end
1674
                        endcase
1675
                end
1676
        `ORR:
1677
                begin
1678
                        case(ir[bitsPerByte+3:bitsPerByte])
1679
                        4'b0000:        begin {acca,accb} <= or12; nf <= or12[bitsPerByte*2-1]; zf <= or12[`DBLBYTE]=='b0; vf <= 1'b0; end
1680
                        4'b0001:        begin xr <= or12; nf <= or12[bitsPerByte*2-1]; zf <= or12[`DBLBYTE]=='b0; vf <= 1'b0; end
1681
                        4'b0010:        begin yr <= or12; nf <= or12[bitsPerByte*2-1]; zf <= or12[`DBLBYTE]=='b0; vf <= 1'b0; end
1682
                        4'b0011:        begin usp <= or12; nf <= or12[bitsPerByte*2-1]; zf <= or12[`DBLBYTE]=='b0; vf <= 1'b0; end
1683
                        4'b0100:        begin ssp <= or12; nf <= or12[bitsPerByte*2-1]; zf <= or12[`DBLBYTE]=='b0; vf <= 1'b0; end
1684
                        4'b0101:        begin pc <= or12; nf <= or12[bitsPerByte*2-1]; zf <= or12[`DBLBYTE]=='b0; vf <= 1'b0; end
1685
                        4'b1000:        begin acca <= or12; nf <= or12[bitsPerByte-1]; zf <= or12[`LOBYTE]=='b0; vf <= 1'b0; end
1686
                        4'b1001:        begin accb <= or12; nf <= or12[bitsPerByte-1]; zf <= or12[`LOBYTE]=='b0; vf <= 1'b0; end
1687
                        4'b1010:
1688
                                begin
1689
                                        cf <= or12[0];
1690
                                        vf <= or12[1];
1691
                                        zf <= or12[2];
1692
                                        nf <= or12[3];
1693
                                        im <= or12[4];
1694
                                        hf <= or12[5];
1695
                                        firqim <= or12[6];
1696
                                        ef <= or12[7];
1697
                                end
1698
                        4'b1011:        begin dpr <= or12; nf <= or12[bitsPerByte-1]; zf <= or12[`LOBYTE]=='b0; vf <= 1'b0; end
1699
                        endcase
1700
                end
1701
        `CMPR:
1702
                begin
1703
                        case(ir[bitsPerByte+3:bitsPerByte])
1704
                        4'b0000:        begin nf <= dif12[bitsPerByte*2-1]; zf <= dif12[`DBLBYTE]=='b0; cf <= dif12[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12[bitsPerByte*2-1]); end
1705
                        4'b0001:        begin nf <= dif12[bitsPerByte*2-1]; zf <= dif12[`DBLBYTE]=='b0; cf <= dif12[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12[bitsPerByte*2-1]); end
1706
                        4'b0010:        begin nf <= dif12[bitsPerByte*2-1]; zf <= dif12[`DBLBYTE]=='b0; cf <= dif12[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12[bitsPerByte*2-1]); end
1707
                        4'b0011:        begin nf <= dif12[bitsPerByte*2-1]; zf <= dif12[`DBLBYTE]=='b0; cf <= dif12[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12[bitsPerByte*2-1]); end
1708
                        4'b0100:        begin nf <= dif12[bitsPerByte*2-1]; zf <= dif12[`DBLBYTE]=='b0; cf <= dif12[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12[bitsPerByte*2-1]); end
1709
                        4'b0101:        begin nf <= dif12[bitsPerByte*2-1]; zf <= dif12[`DBLBYTE]=='b0; cf <= dif12[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12[bitsPerByte*2-1]); end
1710
                        4'b1000:        begin nf <= dif12[bitsPerByte-1]; zf <= dif12[`LOBYTE]=='b0; cf <= dif12[bitsPerByte]; vf <= fnSubOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],dif12[bitsPerByte-1]); end
1711
                        4'b1001:        begin nf <= dif12[bitsPerByte-1]; zf <= dif12[`LOBYTE]=='b0; cf <= dif12[bitsPerByte]; vf <= fnSubOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],dif12[bitsPerByte-1]); end
1712
                        4'b1010:        ;
1713
                        4'b1011:        begin nf <= dif12[bitsPerByte-1]; zf <= dif12[`LOBYTE]=='b0; cf <= dif12[bitsPerByte]; vf <= fnSubOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],dif12[bitsPerByte-1]); end
1714
                        endcase
1715
                end
1716
        `SBCR:
1717
                begin
1718
                        case(ir[bitsPerByte+3:bitsPerByte])
1719
                        4'b0000:        begin {acca,accb} <= dif12c; nf <= dif12c[bitsPerByte*2-1]; zf <= dif12c[`DBLBYTE]=='b0; cf <= dif12c[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12c[bitsPerByte*2-1]); end
1720
                        4'b0001:        begin xr <= dif12c; nf <= dif12c[bitsPerByte*2-1]; zf <= dif12c[`DBLBYTE]=='b0; cf <= dif12c[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12c[bitsPerByte*2-1]); end
1721
                        4'b0010:        begin yr <= dif12c; nf <= dif12c[bitsPerByte*2-1]; zf <= dif12c[`DBLBYTE]=='b0; cf <= dif12c[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12c[bitsPerByte*2-1]); end
1722
                        4'b0011:        begin usp <= dif12c; nf <= dif12c[bitsPerByte*2-1]; zf <= dif12c[`DBLBYTE]=='b0; cf <= dif12c[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12c[bitsPerByte*2-1]); end
1723
                        4'b0100:        begin ssp <= dif12c; nf <= dif12c[bitsPerByte*2-1]; zf <= dif12c[`DBLBYTE]=='b0; cf <= dif12c[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12c[bitsPerByte*2-1]); end
1724
                        4'b0101:        begin pc <= dif12c; nf <= dif12c[bitsPerByte*2-1]; zf <= dif12c[`DBLBYTE]=='b0; cf <= dif12c[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12c[bitsPerByte*2-1]); end
1725
                        4'b1000:        begin acca <= dif12c; nf <= dif12c[bitsPerByte-1]; zf <= dif12c[`LOBYTE]=='b0; cf <= dif12c[bitsPerByte]; vf <= fnSubOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],dif12c[bitsPerByte-1]); end
1726
                        4'b1001:        begin accb <= dif12c; nf <= dif12c[bitsPerByte-1]; zf <= dif12c[`LOBYTE]=='b0; cf <= dif12c[bitsPerByte]; vf <= fnSubOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],dif12c[bitsPerByte-1]); end
1727
                        4'b1010:
1728
                                begin
1729
                                        cf <= dif12c[0];
1730
                                        vf <= dif12c[1];
1731
                                        zf <= dif12c[2];
1732
                                        nf <= dif12c[3];
1733
                                        im <= dif12c[4];
1734
                                        hf <= dif12c[5];
1735
                                        firqim <= dif12c[6];
1736
                                        ef <= dif12c[7];
1737
                                end
1738
                        4'b1011:        begin dpr <= dif12c; nf <= dif12c[bitsPerByte-1]; zf <= dif12c[`LOBYTE]=='b0; cf <= dif12c[bitsPerByte]; vf <= fnSubOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],dif12c[bitsPerByte-1]); end
1739
                        endcase
1740
                end
1741
        `SUBR:
1742
                begin
1743
                        case(ir[bitsPerByte+3:bitsPerByte])
1744
                        4'b0000:        begin {acca,accb} <= dif12; nf <= dif12[bitsPerByte*2-1]; zf <= dif12[`DBLBYTE]=='b0; cf <= dif12[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12[bitsPerByte*2-1]); end
1745
                        4'b0001:        begin xr <= dif12; nf <= dif12[bitsPerByte*2-1]; zf <= dif12[`DBLBYTE]=='b0; cf <= dif12[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12[bitsPerByte*2-1]); end
1746
                        4'b0010:        begin yr <= dif12; nf <= dif12[bitsPerByte*2-1]; zf <= dif12[`DBLBYTE]=='b0; cf <= dif12[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12[bitsPerByte*2-1]); end
1747
                        4'b0011:        begin usp <= dif12; nf <= dif12[bitsPerByte*2-1]; zf <= dif12[`DBLBYTE]=='b0; cf <= dif12[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12[bitsPerByte*2-1]); end
1748
                        4'b0100:        begin ssp <= dif12; nf <= dif12[bitsPerByte*2-1]; zf <= dif12[`DBLBYTE]=='b0; cf <= dif12[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12[bitsPerByte*2-1]); end
1749
                        4'b0101:        begin pc <= dif12; nf <= dif12[bitsPerByte*2-1]; zf <= dif12[`DBLBYTE]=='b0; cf <= dif12[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12[bitsPerByte*2-1]); end
1750
                        4'b1000:        begin acca <= dif12; nf <= dif12[bitsPerByte-1]; zf <= dif12[`LOBYTE]=='b0; cf <= dif12[bitsPerByte]; vf <= fnSubOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],dif12[bitsPerByte-1]); end
1751
                        4'b1001:        begin accb <= dif12; nf <= dif12[bitsPerByte-1]; zf <= dif12[`LOBYTE]=='b0; cf <= dif12[bitsPerByte]; vf <= fnSubOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],dif12[bitsPerByte-1]); end
1752
                        4'b1010:
1753
                                begin
1754
                                        cf <= dif12[0];
1755
                                        vf <= dif12[1];
1756
                                        zf <= dif12[2];
1757
                                        nf <= dif12[3];
1758
                                        im <= dif12[4];
1759
                                        hf <= dif12[5];
1760
                                        firqim <= dif12[6];
1761
                                        ef <= dif12[7];
1762
                                end
1763
                        4'b1011:        begin dpr <= dif12; nf <= dif12[bitsPerByte-1]; zf <= dif12[`LOBYTE]=='b0; cf <= dif12[bitsPerByte]; vf <= fnSubOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],dif12[bitsPerByte-1]); end
1764
                        endcase
1765
                end
1766
`endif
1767
`ifdef SUPPORT_6309
1768
        `CMPE_IMM,`CMPF_IMM,`SUBE_IMM,`SUBF_IMM:
1769
                begin res12 <= acc[`LOBYTE] - ir[`HIBYTE]; pc <= pc + 4'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; end
1770
        `LDE_IMM,`LDF_IMM:
1771
                begin res12 <= ir[`HIBYTE]; pc <= pc + 2'd2; end
1772
`endif
1773 2 robfinch
        // Immediate mode instructions
1774
        `SUBA_IMM,`SUBB_IMM,`CMPA_IMM,`CMPB_IMM:
1775
                begin res12 <= acc[`LOBYTE] - ir[`HIBYTE]; pc <= pc + 4'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; end
1776
        `SBCA_IMM,`SBCB_IMM:
1777
                begin res12 <= acc[`LOBYTE] - ir[`HIBYTE] - cf; pc <= pc + 2'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; end
1778
        `ANDA_IMM,`ANDB_IMM,`BITA_IMM,`BITB_IMM:
1779
                begin res12 <= acc[`LOBYTE] & ir[`HIBYTE]; pc <= pc + 2'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; end
1780
        `LDA_IMM,`LDB_IMM:
1781
                begin res12 <= ir[`HIBYTE]; pc <= pc + 2'd2; end
1782
        `EORA_IMM,`EORB_IMM:
1783
                begin res12 <= acc[`LOBYTE] ^ ir[`HIBYTE]; pc <= pc + 2'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; end
1784
        `ADCA_IMM,`ADCB_IMM:
1785
                begin res12 <= acc[`LOBYTE] + ir[`HIBYTE] + cf; pc <= pc + 2'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; end
1786
        `ORA_IMM,`ORB_IMM:
1787
                begin res12 <= acc[`LOBYTE] | ir[`HIBYTE]; pc <= pc + 2'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; end
1788
        `ADDA_IMM,`ADDB_IMM:
1789
                begin res12 <= acc[`LOBYTE] + ir[`HIBYTE]; pc <= pc + 2'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; end
1790 21 robfinch
`ifdef SUPPORT_6309
1791
        `BITD_IMM,
1792
        `ANDD_IMM:
1793
                        begin
1794
                                res <= {acca[`LOBYTE],accb[`LOBYTE]} & {ir[`BYTE2],ir[`BYTE3]};
1795
                                pc <= pc + 32'd3;
1796
                        end
1797
        `EORD_IMM:
1798
                        begin
1799
                                res <= {acca[`LOBYTE],accb[`LOBYTE]} ^ {ir[`BYTE2],ir[`BYTE3]};
1800
                                pc <= pc + 32'd3;
1801
                        end
1802
        `ORD_IMM:
1803
                        begin
1804
                                res <= {acca[`LOBYTE],accb[`LOBYTE]} | {ir[`BYTE2],ir[`BYTE3]};
1805
                                pc <= pc + 32'd3;
1806
                        end
1807
`endif
1808 2 robfinch
        `ADDD_IMM:
1809 21 robfinch
                        begin
1810
                                res <= {acca[`LOBYTE],accb[`LOBYTE]} + {ir[`HIBYTE],ir[`BYTE3]};
1811
                                pc <= pc + 2'd3;
1812
                        end
1813
`ifdef SUPPORT_6309
1814
        `ADDW_IMM:
1815
                        begin
1816
                                res <= {acce[`LOBYTE],accf[`LOBYTE]} + {ir[`HIBYTE],ir[`BYTE3]};
1817
                                pc <= pc + 2'd3;
1818
                        end
1819
        `ADCD_IMM:
1820
                        begin
1821
                                res <= {acca[`LOBYTE],accb[`LOBYTE]} + {ir[`BYTE2],ir[`BYTE3]} + {23'b0,cf};
1822
                                pc <= pc + 32'd3;
1823
                        end
1824
`endif
1825
        `SUBD_IMM:
1826 2 robfinch
                                begin
1827 21 robfinch
                                        res <= {acca[`LOBYTE],accb[`LOBYTE]} - {ir[`HIBYTE],ir[`BYTE3]};
1828 2 robfinch
                                        pc <= pc + 2'd3;
1829
                                end
1830 21 robfinch
`ifdef SUPPORT_6309
1831
        `SUBW_IMM:
1832 2 robfinch
                                begin
1833 21 robfinch
                                        res <= {acce[`LOBYTE],accf[`LOBYTE]} - {ir[`HIBYTE],ir[`BYTE3]};
1834 2 robfinch
                                        pc <= pc + 2'd3;
1835
                                end
1836 21 robfinch
        `SBCD_IMM:
1837
                begin
1838
                        res <= {acca[`LOBYTE],accb[`LOBYTE]} - {ir[`BYTE2],ir[`BYTE3]} - {23'b0,cf};
1839
                        pc <= pc + 32'd3;
1840
                end
1841
        `LDW_IMM:
1842
                begin
1843
                        res <= {ir[`HIBYTE],ir[`BYTE3]};
1844
                        pc <= pc + 2'd3;
1845
                end
1846
`endif
1847 2 robfinch
        `LDD_IMM:
1848
                                begin
1849
                                        res <= {ir[`HIBYTE],ir[`BYTE3]};
1850
                                        pc <= pc + 2'd3;
1851
                                end
1852
        `LDX_IMM,`LDY_IMM,`LDU_IMM,`LDS_IMM:
1853
                                begin
1854
                                        res <= {ir[`HIBYTE],ir[`BYTE3]};
1855
                                        pc <= pc + 2'd3;
1856
                                end
1857
 
1858
        `CMPD_IMM:
1859
                                begin
1860
                                        res <= {acca[`LOBYTE],accb[`LOBYTE]} - {ir[`HIBYTE],ir[`BYTE3]};
1861
                                        pc <= pc + 2'd3;
1862
                                        a <= {acca[`LOBYTE],accb[`LOBYTE]};
1863
                                        b <= {ir[`HIBYTE],ir[`BYTE3]};
1864
                                end
1865 21 robfinch
`ifdef SUPPORT_6309
1866
        `CMPW_IMM:
1867
                                begin
1868
                                        res <= {acce[`LOBYTE],accf[`LOBYTE]} - {ir[`HIBYTE],ir[`BYTE3]};
1869
                                        pc <= pc + 2'd3;
1870
                                        a <= {acce[`LOBYTE],accf[`LOBYTE]};
1871
                                        b <= {ir[`HIBYTE],ir[`BYTE3]};
1872
                                end
1873
`endif
1874 2 robfinch
        `CMPX_IMM:
1875
                                begin
1876
                                        res <= xr[`DBLBYTE] - {ir[`HIBYTE],ir[`BYTE3]};
1877
                                        pc <= pc + 2'd3;
1878
                                        a <= xr[`DBLBYTE];
1879
                                        b <= {ir[`HIBYTE],ir[`BYTE3]};
1880
                                end
1881
        `CMPY_IMM:
1882
                                begin
1883
                                        res <= yr[`DBLBYTE] - {ir[`HIBYTE],ir[`BYTE3]};
1884
                                        pc <= pc + 2'd3;
1885
                                        a <= yr[`DBLBYTE];
1886
                                        b <= {ir[`HIBYTE],ir[`BYTE3]};
1887
                                end
1888
        `CMPU_IMM:
1889
                                begin
1890
                                        res <= usp[`DBLBYTE] - {ir[`HIBYTE],ir[`BYTE3]};
1891
                                        pc <= pc + 2'd3;
1892
                                        a <= usp[`DBLBYTE];
1893
                                        b <= {ir[`HIBYTE],ir[`BYTE3]};
1894
                                end
1895
        `CMPS_IMM:
1896
                                begin
1897
                                        res <= ssp[`DBLBYTE] - {ir[`HIBYTE],ir[`BYTE3]};
1898
                                        pc <= pc + 2'd3;
1899
                                        a <= ssp[`DBLBYTE];
1900
                                        b <= {ir[`HIBYTE],ir[`BYTE3]};
1901
                                end
1902
 
1903
        // Direct mode instructions
1904
        `NEG_DP,`COM_DP,`LSR_DP,`ROR_DP,`ASR_DP,`ASL_DP,`ROL_DP,`DEC_DP,`INC_DP,`TST_DP:
1905
                begin
1906
                        load_what <= `LW_BL;
1907
                        radr <= dp_address;
1908
                        pc <= pc + 2'd2;
1909
                        next_state(LOAD1);
1910
                end
1911 21 robfinch
`ifdef SUPPORT_6309
1912
        `CMPE_DP,`CMPF_DP,
1913
        `LDE_DP,`LDF_DP,
1914
        `SUBE_DP,`SUBF_DP,
1915
`endif
1916 2 robfinch
        `SUBA_DP,`CMPA_DP,`SBCA_DP,`ANDA_DP,`BITA_DP,`LDA_DP,`EORA_DP,`ADCA_DP,`ORA_DP,`ADDA_DP,
1917
        `SUBB_DP,`CMPB_DP,`SBCB_DP,`ANDB_DP,`BITB_DP,`LDB_DP,`EORB_DP,`ADCB_DP,`ORB_DP,`ADDB_DP:
1918
                begin
1919
                        load_what <= `LW_BL;
1920
                        radr <= dp_address;
1921
                        pc <= pc + 2'd2;
1922
                        next_state(LOAD1);
1923
                end
1924 21 robfinch
`ifdef SUPPORT_6309
1925
        `BITD_DP,
1926
        `ANDD_DP,
1927
        `ORD_DP,
1928
        `EORD_DP:
1929
                begin
1930
                        load_what <= `LW_BL;
1931
                        radr <= dp_address;
1932
                        pc <= pc + 2'd2;
1933
                        next_state(LOAD1);
1934
                end
1935
`endif
1936
`ifdef SUPPORT_6309
1937
        `ADDW_DP,`CMPW_DP,`LDW_DP,`SUBW_DP,
1938
`endif
1939 2 robfinch
        `SUBD_DP,`ADDD_DP,`LDD_DP,`CMPD_DP,`ADCD_DP,`SBCD_DP:
1940
                begin
1941
                        load_what <= `LW_BH;
1942
                        pc <= pc + 2'd2;
1943
                        radr <= dp_address;
1944
                        next_state(LOAD1);
1945
                end
1946
        `CMPX_DP,`LDX_DP,`LDU_DP,`LDS_DP,
1947
        `CMPY_DP,`CMPS_DP,`CMPU_DP,`LDY_DP:
1948
                begin
1949
                        load_what <= `LW_BH;
1950
                        pc <= pc + 2'd2;
1951
                        radr <= dp_address;
1952
                        next_state(LOAD1);
1953
                end
1954
        `CLR_DP:
1955
                begin
1956
                        dp_store(`SW_RES8);
1957
                        res12 <= 13'h000;
1958
                end
1959
        `STA_DP:        dp_store(`SW_ACCA);
1960
        `STB_DP:        dp_store(`SW_ACCB);
1961
        `STD_DP:        dp_store(`SW_ACCDH);
1962
        `STU_DP:        dp_store(`SW_USPH);
1963
        `STS_DP:        dp_store(`SW_SSPH);
1964
        `STX_DP:        dp_store(`SW_XH);
1965
        `STY_DP:        dp_store(`SW_YH);
1966 21 robfinch
`ifdef SUPPORT_6309
1967
        `STW_DP:        dp_store(`SW_ACCWH);
1968
        `STE_DP:        dp_store(`SW_ACCE);
1969
        `STF_DP:        dp_store(`SW_ACCF);
1970
`endif
1971 2 robfinch
        // Indexed mode instructions
1972
        `NEG_NDX,`COM_NDX,`LSR_NDX,`ROR_NDX,`ASR_NDX,`ASL_NDX,`ROL_NDX,`DEC_NDX,`INC_NDX,`TST_NDX:
1973
                begin
1974
                        pc <= pc + insnsz;
1975
                        if (isIndirect) begin
1976
                                load_what <= isFar ? `LW_IA2316 : `LW_IAH;
1977
                                load_what2 <= `LW_BL;
1978
                                radr <= NdxAddr;
1979
                                next_state(LOAD1);
1980
                        end
1981
                        else begin
1982
                                b <= 24'd0;
1983
                                load_what <= `LW_BL;
1984
                                radr <= NdxAddr;
1985
                                next_state(LOAD1);
1986
                        end
1987
                end
1988 21 robfinch
`ifdef SUPPORT_6309
1989
        `CMPE_NDX,`CMPF_NDX,
1990
        `LDE_NDX,`LDF_NDX,
1991
        `SUBE_NDX,`SUBF_NDX,
1992
`endif
1993 2 robfinch
        `SUBA_NDX,`CMPA_NDX,`SBCA_NDX,`ANDA_NDX,`BITA_NDX,`LDA_NDX,`EORA_NDX,`ADCA_NDX,`ORA_NDX,`ADDA_NDX,
1994
        `SUBB_NDX,`CMPB_NDX,`SBCB_NDX,`ANDB_NDX,`BITB_NDX,`LDB_NDX,`EORB_NDX,`ADCB_NDX,`ORB_NDX,`ADDB_NDX:
1995
                begin
1996
                        pc <= pc + insnsz;
1997
                        if (isIndirect) begin
1998
                                load_what <= isFar ? `LW_IA2316 : `LW_IAH;
1999
                                load_what2 <= `LW_BL;
2000
                                radr <= NdxAddr;
2001
                                next_state(LOAD1);
2002
                        end
2003
                        else begin
2004
                                b <= 24'd0;
2005
                                load_what <= `LW_BL;
2006
                                radr <= NdxAddr;
2007
                                next_state(LOAD1);
2008
                        end
2009
                end
2010 21 robfinch
`ifdef SUPPORT_6309
2011
        `BITD_NDX,
2012
        `ANDD_NDX,
2013
        `ORD_NDX,
2014
        `EORD_NDX:
2015
                begin
2016
                        pc <= pc + insnsz;
2017
                        if (isIndirect) begin
2018
                                load_what <= isFar ? `LW_IA2316 : `LW_IAH;
2019
                                load_what2 <= `LW_BL;
2020
                                radr <= NdxAddr;
2021
                                next_state(LOAD1);
2022
                        end
2023
                        else begin
2024
                                b <= 24'd0;
2025
                                load_what <= `LW_BL;
2026
                                radr <= NdxAddr;
2027
                                next_state(LOAD1);
2028
                        end
2029
                end
2030
`endif
2031
`ifdef SUPPORT_6309
2032
        `ADDW_NDX,`CMPW_NDX,`LDW_NDX,`SUBW_NDX,
2033
`endif
2034 2 robfinch
        `SUBD_NDX,`ADDD_NDX,`LDD_NDX,`CMPD_NDX,`ADCD_NDX,`SBCD_NDX:
2035
                begin
2036
                        pc <= pc + insnsz;
2037
                        if (isIndirect) begin
2038
                                load_what <= isFar ? `LW_IA2316 : `LW_IAH;
2039
                                load_what2 <= `LW_BH;
2040
                                radr <= NdxAddr;
2041
                                next_state(LOAD1);
2042
                        end
2043
                        else begin
2044
                                load_what <= `LW_BH;
2045
                                radr <= NdxAddr;
2046
                                next_state(LOAD1);
2047
                        end
2048
                end
2049
        `CMPX_NDX,`LDX_NDX,`LDU_NDX,`LDS_NDX,
2050
        `CMPY_NDX,`CMPS_NDX,`CMPU_NDX,`LDY_NDX:
2051
                begin
2052
                        pc <= pc + insnsz;
2053
                        if (isIndirect) begin
2054
                                load_what <= isFar ? `LW_IA2316 : `LW_IAH;
2055
                                load_what2 <= `LW_BH;
2056
                                radr <= NdxAddr;
2057
                                next_state(LOAD1);
2058
                        end
2059
                        else begin
2060
                                load_what <= `LW_BH;
2061
                                radr <= NdxAddr;
2062
                                next_state(LOAD1);
2063
                        end
2064
                end
2065
        `CLR_NDX:
2066
                begin
2067
                        res12 <= 13'h000;
2068
                        indexed_store(`SW_RES8);
2069
                end
2070
        `STA_NDX:       indexed_store(`SW_ACCA);
2071
        `STB_NDX:       indexed_store(`SW_ACCB);
2072
        `STD_NDX:       indexed_store(`SW_ACCDH);
2073
        `STU_NDX:       indexed_store(`SW_USPH);
2074
        `STS_NDX:       indexed_store(`SW_SSPH);
2075
        `STX_NDX:       indexed_store(`SW_XH);
2076
        `STY_NDX:       indexed_store(`SW_YH);
2077 21 robfinch
`ifdef SUPPORT_6309
2078
        `STW_NDX:       indexed_store(`SW_ACCWH);
2079
        `STE_NDX:       indexed_store(`SW_ACCE);
2080
        `STF_NDX:       indexed_store(`SW_ACCF);
2081
                `AIM_DP,`EIM_DP,`OIM_DP,`TIM_DP:
2082
                        begin
2083
                                load_what <= `LW_BL;
2084
                                pc <= pc + 4'd3;
2085
                                radr <= dp_address;
2086
                                next_state(LOAD1);
2087
                        end
2088
                `AIM_NDX,`EIM_NDX,`OIM_NDX,`TIM_NDX:
2089
                        begin
2090
                                pc <= pc + insnsz + 4'd1;
2091
                                if (isIndirect) begin
2092
                                        load_what <= isFar ? `LW_IA2316 : `LW_IAH;
2093
                                        load_what2 <= `LW_BL;
2094
                                        radr <= NdxAddr;
2095
                                        next_state(LOAD1);
2096
                                end
2097
                                else begin
2098
                                        b <= 'd0;
2099
                                        load_what <= `LW_BL;
2100
                                        radr <= NdxAddr;
2101
                                        next_state(LOAD1);
2102
                                end
2103
                        end
2104
`endif
2105
`ifdef SUPPORT_6309
2106
        `AIM_EXT,`OIM_EXT,`EIM_EXT,`TIM_EXT,
2107
`endif
2108 2 robfinch
        // Extended mode instructions
2109
        `NEG_EXT,`COM_EXT,`LSR_EXT,`ROR_EXT,`ASR_EXT,`ASL_EXT,`ROL_EXT,`DEC_EXT,`INC_EXT,`TST_EXT:
2110
                begin
2111
                        load_what <= `LW_BL;
2112
                        radr <= ex_address;
2113
                        pc <= pc + (isFar ? 32'd4 : 32'd3);
2114
                        next_state(LOAD1);
2115
                end
2116 21 robfinch
`ifdef SUPPORT_6309
2117
        `CMPE_EXT,`CMPF_EXT,
2118
        `LDE_EXT,`LDF_EXT,
2119
        `SUBE_EXT,`SUBF_EXT,
2120
`endif
2121 2 robfinch
        `SUBA_EXT,`CMPA_EXT,`SBCA_EXT,`ANDA_EXT,`BITA_EXT,`LDA_EXT,`EORA_EXT,`ADCA_EXT,`ORA_EXT,`ADDA_EXT,
2122
        `SUBB_EXT,`CMPB_EXT,`SBCB_EXT,`ANDB_EXT,`BITB_EXT,`LDB_EXT,`EORB_EXT,`ADCB_EXT,`ORB_EXT,`ADDB_EXT:
2123
                begin
2124
                        load_what <= `LW_BL;
2125
                        radr <= ex_address;
2126
                        pc <= pc + (isFar ? 32'd4 : 32'd3);
2127
                        next_state(LOAD1);
2128
                end
2129 21 robfinch
`ifdef SUPPORT_6309
2130
        `BITD_EXT,
2131
        `ANDD_EXT,
2132
        `ORD_EXT,
2133
        `EORD_EXT:
2134
                begin
2135
                        load_what <= `LW_BL;
2136
                        radr <= ex_address;
2137
                        pc <= pc + (isFar ? 32'd4 : 32'd3);
2138
                        next_state(LOAD1);
2139
                end
2140
`endif
2141
`ifdef SUPPORT_6309
2142
        `ADDW_EXT,`CMPW_EXT,`LDW_EXT,`SUBW_EXT,
2143
`endif
2144 2 robfinch
        `SUBD_EXT,`ADDD_EXT,`LDD_EXT,`CMPD_EXT,`ADCD_EXT,`SBCD_EXT:
2145
                begin
2146
                        load_what <= `LW_BH;
2147
                        radr <= ex_address;
2148
                        pc <= pc + (isFar ? 32'd4 : 32'd3);
2149
                        next_state(LOAD1);
2150
                end
2151
        `CMPX_EXT,`LDX_EXT,`LDU_EXT,`LDS_EXT,
2152
        `CMPY_EXT,`CMPS_EXT,`CMPU_EXT,`LDY_EXT:
2153
                begin
2154
                        load_what <= `LW_BH;
2155
                        radr <= ex_address;
2156
                        pc <= pc + (isFar ? 32'd4 : 32'd3);
2157
                        next_state(LOAD1);
2158
                end
2159
        `CLR_EXT:
2160
                begin
2161
                        ex_store(`SW_RES8);
2162
                        res12 <= 13'h000;
2163
                end
2164
        `STA_EXT:       ex_store(`SW_ACCA);
2165
        `STB_EXT:       ex_store(`SW_ACCB);
2166
        `STD_EXT:       ex_store(`SW_ACCDH);
2167
        `STU_EXT:       ex_store(`SW_USPH);
2168
        `STS_EXT:       ex_store(`SW_SSPH);
2169
        `STX_EXT:       ex_store(`SW_XH);
2170
        `STY_EXT:       ex_store(`SW_YH);
2171 21 robfinch
`ifdef SUPPORT_6309
2172
        `STW_EXT:       ex_store(`SW_ACCWH);
2173
        `STE_EXT:       ex_store(`SW_ACCE);
2174
        `STF_EXT:       ex_store(`SW_ACCF);
2175
`endif
2176 2 robfinch
        `BSR:
2177
                begin
2178
                        store_what <= `SW_PCH;
2179
                        wadr <= ssp - 2'd2;
2180
                        ssp <= ssp - 2'd2;
2181
                        pc <= pc + 2'd2;
2182
                        next_state(STORE1);
2183
                end
2184
        `LBSR:
2185
                begin
2186
                        store_what <= `SW_PCH;
2187
                        wadr <= ssp - 2'd2;
2188
                        ssp <= ssp - 2'd2;
2189
                        pc <= pc + 2'd3;
2190
                        next_state(STORE1);
2191
                end
2192
        `JSR_DP:
2193
                begin
2194
                        store_what <= `SW_PCH;
2195
                        wadr <= ssp - 2'd2;
2196
                        ssp <= ssp - 2'd2;
2197
                        pc <= pc + 2'd2;
2198
                        next_state(STORE1);
2199
                end
2200
        `JSR_NDX:
2201
                begin
2202 15 robfinch
            if (isFar) begin
2203
                                store_what <= `SW_PC2316;
2204
                    wadr <= ssp - 16'd3;
2205
                    ssp <= ssp - 16'd3;
2206
            end
2207
                        begin
2208
                                store_what <= `SW_PCH;
2209
                                wadr <= ssp - 2'd2;
2210
                                ssp <= ssp - 2'd2;
2211 2 robfinch
                        end
2212
                        pc <= pc + insnsz;
2213
                        next_state(STORE1);
2214
                end
2215
        `JSR_EXT:
2216
                begin
2217
                        begin
2218
                                store_what <= `SW_PCH;
2219
                                wadr <= ssp - 2'd2;
2220
                                ssp <= ssp - 2'd2;
2221
                        end
2222
                        pc <= pc + 2'd3;
2223
                        next_state(STORE1);
2224
                end
2225
        `JSR_FAR:
2226
                begin
2227
                        store_what <= `SW_PC2316;
2228
                        wadr <= ssp - 16'd4;
2229
                        ssp <= ssp - 16'd4;
2230
                        pc <= pc + 32'd4;
2231
                        next_state(STORE1);
2232
                end
2233
        `RTS:
2234
                begin
2235
                        load_what <= `LW_PCH;
2236
                        radr <= ssp;
2237
                        next_state(LOAD1);
2238
                end
2239
        `RTF:
2240
                begin
2241
                        load_what <= `LW_PC2316;
2242
                        radr <= ssp;
2243
                        next_state(LOAD1);
2244
                end
2245
        `JMP_DP:        pc <= dp_address;
2246
        `JMP_EXT:       pc <= address;
2247
        `JMP_FAR:       pc <= far_address;
2248
        `JMP_NDX:
2249
                begin
2250
                        if (isIndirect) begin
2251
                        radr <= NdxAddr;
2252
                            if (isFar)
2253
                                   load_what <= `LW_PC2316;
2254
                            else
2255
                                   load_what <= `LW_PCH;
2256
                                next_state(LOAD1);
2257
                        end
2258
                        else
2259
                                pc <= isFar ? NdxAddr : {pc[`BYTE3],NdxAddr[`DBLBYTE]};
2260
                end
2261
        `LEAX_NDX,`LEAY_NDX,`LEAS_NDX,`LEAU_NDX:
2262
                begin
2263
                        pc <= pc + insnsz;
2264
                        if (isIndirect) begin
2265
                                load_what <= `LW_IAH;
2266
                                radr <= NdxAddr;
2267
                                state <= LOAD1;
2268
                        end
2269
                        else
2270
                                res <= NdxAddr[`DBLBYTE];
2271
                end
2272
        `PSHU,`PSHS:
2273
                begin
2274
                        next_state(PUSH1);
2275
                        pc <= pc + 2'd2;
2276
                end
2277
        `PULS:
2278
                begin
2279
                        radr <= ssp;
2280
                        next_state(PULL1);
2281
                        pc <= pc + 2'd2;
2282
                end
2283
        `PULU:
2284
                begin
2285
                        radr <= {usppg,8'h00} + usp;
2286
                        next_state(PULL1);
2287
                        pc <= pc + 2'd2;
2288
                end
2289
        `BEQ,`BNE,`BMI,`BPL,`BVS,`BVC,`BHI,`BLS,`BHS,`BLO,`BGT,`BGE,`BLT,`BLE,`BRA,`BRN:
2290
                if (takb)
2291
                        pc <= pc + {{24{ir[BPBX2M1]}},ir[`HIBYTE]} + 2'd2;
2292
                else
2293
                        pc <= pc + 2'd2;
2294
        // PC is already incremented by one due to the PG10 prefix.
2295
        `LBEQ,`LBNE,`LBMI,`LBPL,`LBVS,`LBVC,`LBHI,`LBLS,`LBHS,`LBLO,`LBGT,`LBGE,`LBLT,`LBLE,`LBRN:
2296
                if (takb)
2297
                        pc <= pc + {{12{ir[BPB*3-1]}},ir[`HIBYTE],ir[`BYTE3]} + 2'd3;
2298
                else
2299
                        pc <= pc + 2'd3;
2300
        `LBRA:  pc <= pc + {{12{ir[BPB*3-1]}},ir[`HIBYTE],ir[`BYTE3]} + 2'd3;
2301
        `RTI:
2302
                begin
2303
                        load_what <= `LW_CCR;
2304
                        radr <= ssp;
2305
                        isFar <= `TRUE;
2306
                        next_state(LOAD1);
2307
                end
2308
        `SWI:
2309
                begin
2310
                        im <= 1'b1;
2311
                        firqim <= 1'b1;
2312
                        ir[`LOBYTE] <= `INT;
2313
                        ipg <= 2'b11;
2314
                        vect <= `SWI_VECT;
2315
                        next_state(DECODE);
2316
                end
2317
        `SWI2:
2318
                begin
2319
                        ir[`LOBYTE] <= `INT;
2320
                        ipg <= 2'b11;
2321
                        vect <= `SWI2_VECT;
2322
                        next_state(DECODE);
2323
                end
2324
        `SWI3:
2325
                begin
2326
                        ir[`LOBYTE] <= `INT;
2327
                        ipg <= 2'b11;
2328
                        vect <= `SWI3_VECT;
2329
                        next_state(DECODE);
2330
                end
2331
        // If the processor was in the wait state before the interrupt occurred
2332
        // the registers will have already been pushed. All that needs to be
2333
        // done is to vector to the interrupt routine.
2334
        `INT:
2335
                begin
2336
                        if (wait_state) begin
2337
                                wait_state <= `FALSE;
2338
                                if (vec_i != 24'h0) begin
2339
                                    pc <= vec_i;
2340
                                    next_state(IFETCH);
2341
                                end
2342
                                else begin
2343
                                    radr <= vect;
2344
                      load_what <= `LW_PCH;
2345
                                    pc <= 32'hFFFFFFFE;
2346
                                    next_state(LOAD1);
2347
                                end
2348
                        end
2349
                        else begin
2350
                                if (isNMI | isIRQ | isSWI | isSWI2 | isSWI3) begin
2351
                                        ir[`HIBYTE] <= 16'hFFFF;
2352
                                        ef <= 1'b1;
2353
                                end
2354
                                else if (isFIRQ) begin
2355
                                        if (natMd) begin
2356
                                                ef <= firqMd;
2357
                                                ir[`HIBYTE] <= firqMd ? 16'hFFFF : 12'h81;
2358
                                        end
2359
                                        else begin
2360
                                                ir[`HIBYTE] <= 12'h81;
2361
                                                ef <= 1'b0;
2362
                                        end
2363
                                end
2364
                                pc <= pc;
2365
                                isFar <= `TRUE;
2366
                                next_state(PUSH1);
2367
                        end
2368
                end
2369
        default:        ;
2370
        endcase
2371
end
2372
endtask
2373
 
2374
// ============================================================================
2375
// MEMORY LOAD
2376
// ============================================================================
2377
task tLoad1;
2378
begin
2379
`ifdef SUPPORT_DCACHE
2380
        if (unCachedData)
2381
`endif
2382
        case(radr)
2383
        {{BPB*3-8{1'b1}},8'hE0}:        load_tsk({2'b0,id});
2384
        {{BPB*3-8{1'b1}},8'hE1}:        load_tsk(chkpoint);
2385
        {{BPB*3-8{1'b1}},8'hE4}:        load_tsk(12'h0);
2386
        {{BPB*3-8{1'b1}},8'hE5}:        load_tsk(ms_count[35:24]);
2387
        {{BPB*3-8{1'b1}},8'hE6}:        load_tsk(ms_count[23:12]);
2388
        {{BPB*3-8{1'b1}},8'hE7}:        load_tsk(ms_count[11: 0]);
2389
        default:
2390
        if (~ack_i) begin
2391
                lock_o <= lock_bus;
2392
                wb_read(radr);
2393
                if (!tsc)
2394
                        next_state(LOAD2);
2395
        end
2396
`ifdef SUPPORT_DCACHE
2397
        else if (dhit)
2398
                load_tsk(rdat);
2399
        else begin
2400
                retstate <= LOAD1;
2401
                state <= DCACHE1;
2402
        end
2403
`endif
2404
        endcase
2405
end
2406
endtask
2407
 
2408
task tLoad2;
2409
begin
2410
        // On a tri-state condition abort the bus cycle and retry the load.
2411
        if (tsc|rty_i|bto) begin
2412
                wb_nack();
2413
                next_state(LOAD1);
2414
        end
2415
        else if (ack_i) begin
2416
                wb_nack();
2417
                load_tsk(dati);
2418
        end
2419
`ifdef SUPPORT_BERR
2420
        else if (err_i) begin
2421
                lock_o <= 1'b0;
2422
                wb_nack();
2423
                derr_address <= adr_o;
2424
//              intno <= 9'd508;
2425
                state <= BUS_ERROR;
2426
        end
2427
`endif
2428
end
2429
endtask
2430
 
2431
// ============================================================================
2432
// EXECUTE
2433
//
2434
// Perform calculations
2435
// ============================================================================
2436
task tExecute;
2437
begin
2438
        next_state(IFETCH);
2439
        case(ir12)
2440
        `SUBD_DP,`SUBD_NDX,`SUBD_EXT,
2441
        `CMPD_DP,`CMPD_NDX,`CMPD_EXT:
2442
                begin
2443
                    a <= {acca[`LOBYTE],accb[`LOBYTE]};
2444
                        res <= {acca[`LOBYTE],accb[`LOBYTE]} - b[`DBLBYTE];
2445
                end
2446
        `SBCD_DP,`SBCD_NDX,`SBCD_EXT:
2447
                begin
2448
                    a <= {acca[`LOBYTE],accb[`LOBYTE]};
2449
                        res <= {acca[`LOBYTE],accb[`LOBYTE]} - b[`DBLBYTE] - {23'b0,cf};
2450
                end
2451
        `ADDD_DP,`ADDD_NDX,`ADDD_EXT:
2452
                begin
2453
                    a <= {acca[`LOBYTE],accb[`LOBYTE]};
2454
                        res <= {acca[`LOBYTE],accb[`LOBYTE]} + b[`DBLBYTE];
2455
                end
2456 21 robfinch
`ifdef SUPPORT_6309
2457
        `SUBW_DP,`SUBW_NDX,`SUBW_EXT,
2458
        `CMPW_DP,`CMPW_NDX,`CMPW_EXT:
2459
                begin
2460
                    a <= {acce[`LOBYTE],accf[`LOBYTE]};
2461
                        res <= {acce[`LOBYTE],accf[`LOBYTE]} - b[`DBLBYTE];
2462
                end
2463
        `ADDW_DP,`ADDW_NDX,`ADDW_EXT:
2464
                begin
2465
                    a <= {acce[`LOBYTE],accf[`LOBYTE]};
2466
                        res <= {acce[`LOBYTE],accf[`LOBYTE]} + b[`DBLBYTE];
2467
                end
2468
        `LDW_DP,`LDW_NDX,`LDW_EXT:
2469
                res <= b[`DBLBYTE];
2470
`endif
2471 2 robfinch
        `ADCD_DP,`ADCD_NDX,`ADCD_EXT:
2472
                begin
2473
                    a <= {acca[`LOBYTE],accb[`LOBYTE]};
2474
                        res <= {acca[`LOBYTE],accb[`LOBYTE]} + b[`DBLBYTE] + {23'b0,cf};
2475
                end
2476
        `LDD_DP,`LDD_NDX,`LDD_EXT:
2477
                res <= b[`DBLBYTE];
2478 21 robfinch
`ifdef SUPPORT_6309
2479
        `CMPE_DP,`CMPE_NDX,`CMPE_EXT,
2480
        `CMPF_DP,`CMPF_NDX,`CMPF_EXT,
2481
        `SUBE_DP,`SUBE_NDX,`SUBE_EXT,
2482
        `SUBF_DP,`SUBF_NDX,`SUBF_EXT,
2483
`endif
2484 2 robfinch
        `CMPA_DP,`CMPA_NDX,`CMPA_EXT,
2485
        `SUBA_DP,`SUBA_NDX,`SUBA_EXT,
2486
        `CMPB_DP,`CMPB_NDX,`CMPB_EXT,
2487
        `SUBB_DP,`SUBB_NDX,`SUBB_EXT:
2488
                begin
2489
                        a <= acc;
2490
           res12 <= acc[`LOBYTE] - b12;
2491
                        end
2492
 
2493
        `SBCA_DP,`SBCA_NDX,`SBCA_EXT,
2494
        `SBCB_DP,`SBCB_NDX,`SBCB_EXT:
2495
                begin
2496
                    a <= acc;
2497
          res12 <= acc[`LOBYTE] - b12 - cf;
2498
              end
2499
        `BITA_DP,`BITA_NDX,`BITA_EXT,
2500
        `ANDA_DP,`ANDA_NDX,`ANDA_EXT,
2501
        `BITB_DP,`BITB_NDX,`BITB_EXT,
2502
        `ANDB_DP,`ANDB_NDX,`ANDB_EXT:
2503
                                res12 <= acc[`LOBYTE] & b12;
2504 21 robfinch
`ifdef SUPPORT_6309
2505
        `BITD_DP,`BITD_NDX,`BITD_EXT,
2506
        `ANDD_DP,`ANDD_NDX,`ANDD_EXT:
2507
                res <= {acca[`LOBYTE],accb[`LOBYTE]} & b[`DBLBYTE];
2508
        `EORD_DP,`EORD_NDX,`EORD_EXT:
2509
                res <= {acca[`LOBYTE],accb[`LOBYTE]} ^ b[`DBLBYTE];
2510
        `ORD_DP,`ORD_NDX,`ORD_EXT:
2511
                res <= {acca[`LOBYTE],accb[`LOBYTE]} | b[`DBLBYTE];
2512
        `LDE_DP,`LDE_NDX,`LDE_EXT,
2513
        `LDF_DP,`LDF_NDX,`LDF_EXT:
2514
                res12 <= b12;
2515
`endif
2516 2 robfinch
        `LDA_DP,`LDA_NDX,`LDA_EXT,
2517
        `LDB_DP,`LDB_NDX,`LDB_EXT:
2518
                        res12 <= b12;
2519
        `EORA_DP,`EORA_NDX,`EORA_EXT,
2520
        `EORB_DP,`EORB_NDX,`EORB_EXT:
2521
                                res12 <= acc[`LOBYTE] ^ b12;
2522
        `ADCA_DP,`ADCA_NDX,`ADCA_EXT,
2523
        `ADCB_DP,`ADCB_NDX,`ADCB_EXT:
2524
                        begin
2525
                            a <= acc;
2526
                                res12 <= acc[`LOBYTE] + b12 + cf;
2527
                        end
2528
        `ORA_DP,`ORA_NDX,`ORA_EXT,
2529
        `ORB_DP,`ORB_NDX,`ORB_EXT:
2530
                                res12 <= acc[`LOBYTE] | b12;
2531 21 robfinch
`ifdef SUPPORT_6309
2532
        `ADDE_DP,`ADDE_NDX,`ADDE_EXT,
2533
        `ADDF_DP,`ADDF_NDX,`ADDF_EXT,
2534
`endif
2535 2 robfinch
        `ADDA_DP,`ADDA_NDX,`ADDA_EXT,
2536
        `ADDB_DP,`ADDB_NDX,`ADDB_EXT:
2537
                begin
2538
                    a <= acc;
2539
                      res12 <= acc[`LOBYTE] + b12;
2540
              end
2541
 
2542
        `LDU_DP,`LDS_DP,`LDX_DP,`LDY_DP,
2543
        `LDU_NDX,`LDS_NDX,`LDX_NDX,`LDY_NDX,
2544
        `LDU_EXT,`LDS_EXT,`LDX_EXT,`LDY_EXT:    res <= b[`DBLBYTE];
2545
        `CMPX_DP,`CMPX_NDX,`CMPX_EXT:   begin a <= xr; res <= xr[`DBLBYTE] - b[`DBLBYTE]; end
2546
        `CMPY_DP,`CMPY_NDX,`CMPY_EXT:   begin a <= yr; res <= yr[`DBLBYTE] - b[`DBLBYTE]; end
2547
        `CMPS_DP,`CMPS_NDX,`CMPS_EXT:   begin a <= ssp; res <= ssp[`DBLBYTE] - b[`DBLBYTE]; end
2548
        `CMPU_DP,`CMPU_NDX,`CMPU_EXT:   begin a <= usp; res <= usp[`DBLBYTE] - b[`DBLBYTE]; end
2549
 
2550
        `NEG_DP,`NEG_NDX,`NEG_EXT:      begin res12 <= -b12; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
2551
        `COM_DP,`COM_NDX,`COM_EXT:      begin res12 <= ~b12; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
2552
        `LSR_DP,`LSR_NDX,`LSR_EXT:      begin res12 <= {b[0],1'b0,b[BPBM1:1]}; store_what <= `SW_RES8; wadr <= radr; next_state(STORE1); end
2553
        `ROR_DP,`ROR_NDX,`ROR_EXT:      begin res12 <= {b[0],cf,b[BPBM1:1]}; store_what <= `SW_RES8; wadr <= radr; next_state(STORE1); end
2554
        `ASR_DP,`ASR_NDX,`ASR_EXT:      begin res12 <= {b[0],b[BPBM1],b[BPBM1:1]}; store_what <= `SW_RES8; wadr <= radr; next_state(STORE1); end
2555
        `ASL_DP,`ASL_NDX,`ASL_EXT:      begin res12 <= {b12,1'b0}; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
2556
        `ROL_DP,`ROL_NDX,`ROL_EXT:      begin res12 <= {b12,cf}; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
2557
        `DEC_DP,`DEC_NDX,`DEC_EXT:      begin res12 <= b12 - 2'd1; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
2558
        `INC_DP,`INC_NDX,`INC_EXT:      begin res12 <= b12 + 2'd1; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
2559
        `TST_DP,`TST_NDX,`TST_EXT:      res12 <= b12;
2560 21 robfinch
`ifdef SUPPORT_6309
2561 2 robfinch
        `AIM_DP,`AIM_NDX,`AIM_EXT:      begin res12 <= ir[`HIBYTE] & b12; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
2562
        `OIM_DP,`OIM_NDX,`OIM_EXT:      begin res12 <= ir[`HIBYTE] | b12; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
2563
        `EIM_DP,`EIM_NDX,`OIM_EXT:  begin res12 <= ir[`HIBYTE] ^ b12; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
2564
        `TIM_DP,`TIM_NDX,`TIM_EXT:      begin res12 <= ir[`HIBYTE] & b12; end
2565 21 robfinch
`endif
2566 2 robfinch
        default:        ;
2567
        endcase
2568
end
2569
endtask
2570
 
2571
// ============================================================================
2572
// MEMORY STORE
2573
// ============================================================================
2574
 
2575
task tStore1;
2576
begin
2577
        if (!ack_i) begin
2578
                lock_o <= lock_bus;
2579
`ifdef SUPPORT_CHECKPOINT
2580
                if (wadr=={{BPB*3-8{1'b1}},8'hE1})
2581
                        next_state(IFETCH);
2582
                else
2583
`endif
2584
                begin
2585
                        case(store_what)
2586
                        `SW_ACCDH:      wb_write(wadr,acca[`LOBYTE]);
2587
                        `SW_ACCDL:      wb_write(wadr,accb[`LOBYTE]);
2588
                        `SW_ACCA:       wb_write(wadr,acca[`LOBYTE]);
2589
                        `SW_ACCB:       wb_write(wadr,accb[`LOBYTE]);
2590 21 robfinch
`ifdef SUPPORT_6309
2591
                        `SW_ACCWH:      wb_write(wadr,acce[`LOBYTE]);
2592
                        `SW_ACCWL:      wb_write(wadr,accf[`LOBYTE]);
2593
                        `SW_ACCE:       wb_write(wadr,acce[`LOBYTE]);
2594
                        `SW_ACCF:       wb_write(wadr,accf[`LOBYTE]);
2595
`endif
2596 2 robfinch
                        `SW_DPR:        wb_write(wadr,dpr);
2597
                        `SW_XL: wb_write(wadr,xr[`LOBYTE]);
2598
                        `SW_XH: wb_write(wadr,xr[`HIBYTE]);
2599
                        `SW_YL: wb_write(wadr,yr[`LOBYTE]);
2600
                        `SW_YH: wb_write(wadr,yr[`HIBYTE]);
2601
                        `SW_USPL:       wb_write(wadr,usp[`LOBYTE]);
2602
                        `SW_USPH:       wb_write(wadr,usp[`HIBYTE]);
2603
                        `SW_SSPL:       wb_write(wadr,ssp[`LOBYTE]);
2604
                        `SW_SSPH:       wb_write(wadr,ssp[`HIBYTE]);
2605
                        `SW_PC2316:     wb_write(wadr,pc[`BYTE3]);
2606
                        `SW_PCH:        wb_write(wadr,pc[`HIBYTE]);
2607
                        `SW_PCL:        wb_write(wadr,pc[`LOBYTE]);
2608
                        `SW_CCR:        wb_write(wadr,ccr);
2609
                        `SW_RES8:       wb_write(wadr,res12[`LOBYTE]);
2610
                        `SW_RES16H:     wb_write(wadr,res[`HIBYTE]);
2611
                        `SW_RES16L:     wb_write(wadr,res[`LOBYTE]);
2612
                        `SW_DEF8:       wb_write(wadr,wdat);
2613
                        default:        wb_write(wadr,wdat);
2614
                        endcase
2615
`ifdef SUPPORT_DCACHE
2616
                        radr <= wadr;           // Do a cache read to test the hit
2617
`endif
2618
                        if (!tsc)
2619 21 robfinch
                                next_state(STORE1a);
2620 2 robfinch
                end
2621
        end
2622
end
2623
endtask
2624
 
2625 21 robfinch
task tStore1a;
2626
begin
2627
        if (!tsc) begin
2628
                cyc_o <= 1'b1;
2629
                stb_o <= 1'b1;
2630
                next_state(STORE2);
2631
        end
2632
end
2633
endtask
2634
 
2635 2 robfinch
// Terminal state for stores. Update the data cache if there was a cache hit.
2636
// Clear any previously set lock status
2637
task tStore2;
2638
begin
2639
        // On a tri-state condition abort the bus cycle and retry the store.
2640
        if (tsc|rty_i|bto) begin
2641
                wb_nack();
2642
                next_state(STORE1);
2643
        end
2644
        else if (ack_i) begin
2645
                wb_nack();
2646
                wdat <= dat_o;
2647
                wadr <= wadr + 2'd1;
2648
                next_state(IFETCH);
2649
                case(store_what)
2650
                `SW_CCR:
2651
                        begin
2652
                                if (isINT) begin
2653
                                        im <= 1'b1;
2654
                                        firqim <= 1'b1;
2655
                                end
2656
                                next_state(PUSH2);
2657
                        end
2658
                `SW_ACCA:
2659
                        if (isINT | isPSHS | isPSHU)
2660
                                next_state(PUSH2);
2661
                        else    // STA
2662
                                next_state(IFETCH);
2663
                `SW_ACCB:
2664
                        if (isINT | isPSHS | isPSHU)
2665
                                next_state(PUSH2);
2666
                        else    // STB
2667
                                next_state(IFETCH);
2668 21 robfinch
`ifdef SUPPORT_6309
2669
                `SW_ACCE:
2670
                        if (isINT | isPSHS | isPSHU)
2671
                                next_state(PUSH2);
2672
                        else    // STE
2673
                                next_state(IFETCH);
2674
                `SW_ACCF:
2675
                        if (isINT | isPSHS | isPSHU)
2676
                                next_state(PUSH2);
2677
                        else    // STF
2678
                                next_state(IFETCH);
2679
                `SW_ACCWH:
2680
                        begin
2681
                                store_what <= `SW_ACCWL;
2682
                                next_state(STORE1);
2683
                        end
2684
                `SW_ACCWL:      next_state(IFETCH);
2685
`endif
2686 2 robfinch
                `SW_ACCDH:
2687
                        begin
2688
                                store_what <= `SW_ACCDL;
2689
                                next_state(STORE1);
2690
                        end
2691
                `SW_ACCDL:      next_state(IFETCH);
2692
                `SW_DPR:        next_state(PUSH2);
2693
                `SW_XH:
2694
                        begin
2695
                                store_what <= `SW_XL;
2696
                                next_state(STORE1);
2697
                        end
2698
                `SW_XL:
2699
                        if (isINT | isPSHS | isPSHU)
2700
                                next_state(PUSH2);
2701
                        else    // STX
2702
                                next_state(IFETCH);
2703
                `SW_YH:
2704
                        begin
2705
                                store_what <= `SW_YL;
2706
                                next_state(STORE1);
2707
                        end
2708
                `SW_YL:
2709
                        if (isINT | isPSHS | isPSHU)
2710
                                next_state(PUSH2);
2711
                        else    // STY
2712
                                next_state(IFETCH);
2713
                `SW_USPH:
2714
                        begin
2715
                                store_what <= `SW_USPL;
2716
                                next_state(STORE1);
2717
                        end
2718
                `SW_USPL:
2719
                        if (isINT | isPSHS | isPSHU)
2720
                                next_state(PUSH2);
2721
                        else    // STU
2722
                                next_state(IFETCH);
2723
                `SW_SSPH:
2724
                        begin
2725
                                store_what <= `SW_SSPL;
2726
                                next_state(STORE1);
2727
                        end
2728
                `SW_SSPL:
2729
                        if (isINT | isPSHS | isPSHU)
2730
                                next_state(PUSH2);
2731
                        else    // STS
2732
                                next_state(IFETCH);
2733
                `SW_PC2316:
2734
                        begin
2735
                                store_what <= `SW_PCH;
2736
                                next_state(STORE1);
2737
                        end
2738
                `SW_PCH:
2739
                        begin
2740
                                store_what <= `SW_PCL;
2741
                                next_state(STORE1);
2742
                        end
2743
                `SW_PCL:
2744
                        if (isINT | isPSHS | isPSHU)
2745
                                next_state(PUSH2);
2746
                        else begin      // JSR
2747
                                next_state(IFETCH);
2748
                                case(ir12)
2749
                                `BSR:           pc <= pc + {{24{ir[BPBX2M1]}},ir[`HIBYTE]};
2750
                                `LBSR:  pc <= pc + {{12{ir[BPB*3-1]}},ir[`HIBYTE],ir[`BYTE3]};
2751
                                `JSR_DP:        pc <= {dpr,ir[`HIBYTE]};
2752
                                `JSR_EXT:       pc <= {pc[`BYTE3],address[`DBLBYTE]};
2753
                                `JSR_FAR:
2754
                                        begin
2755
                                                pc <= far_address;
2756
                                                $display("Loading PC with %h", far_address);
2757
                                        end
2758
                                `JSR_NDX:
2759
                                        begin
2760
                                                if (isIndirect) begin
2761
                                                        radr <= NdxAddr;
2762
                                                        load_what <= isFar ? `LW_PC2316 : `LW_PCH;
2763
                                                        next_state(LOAD1);
2764
                                                end
2765
                                                else
2766
                                                        pc <= isFar ? NdxAddr : {pc[`BYTE3],NdxAddr[`DBLBYTE]};
2767
                                        end
2768
                                endcase
2769
                        end
2770
                endcase
2771
`ifdef SUPPORT_DCACHE
2772
                if (!dhit && write_allocate) begin
2773
                        state <= DCACHE1;
2774
                end
2775
`endif
2776
        end
2777
`ifdef SUPPORT_BERR
2778
        else if (err_i) begin
2779
                lock_o <= 1'b0;
2780
                wb_nack();
2781
                state <= BUS_ERROR;
2782
        end
2783
`endif
2784
end
2785
endtask
2786
 
2787
// ============================================================================
2788
// WRITEBACK
2789
//
2790
// Write results back to the register file and status flags.
2791
// Which registers and flags get updated depend on the instruction.
2792
// ============================================================================
2793
 
2794
task tWriteback;
2795
begin
2796
        if (first_ifetch) begin
2797
                first_ifetch <= `FALSE;
2798
                case(ir12)
2799
                `ABX:   xr <= res;
2800
                `ADDA_IMM,`ADDA_DP,`ADDA_NDX,`ADDA_EXT,
2801
                `ADCA_IMM,`ADCA_DP,`ADCA_NDX,`ADCA_EXT:
2802
                        begin
2803
                                cf <= (a[BPBM1]&b[BPBM1])|(a[BPBM1]&~res12[BPBM1])|(b[BPBM1]&~res12[BPBM1]);
2804
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
2805
                                vf <= (res12[BPBM1] ^ b[BPBM1]) & (1'b1 ^ a[BPBM1] ^ b[BPBM1]);
2806
                                nf <= res12[BPBM1];
2807
                                zf <= res12[`LOBYTE]==12'h000;
2808
                                acca <= res12[`LOBYTE];
2809
                        end
2810
                `ADDB_IMM,`ADDB_DP,`ADDB_NDX,`ADDB_EXT,
2811
                `ADCB_IMM,`ADCB_DP,`ADCB_NDX,`ADCB_EXT:
2812
                        begin
2813
                                cf <= (a[BPBM1]&b[BPBM1])|(a[BPBM1]&~res12[BPBM1])|(b[BPBM1]&~res12[BPBM1]);
2814
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
2815
                                vf <= (res12[BPBM1] ^ b[BPBM1]) & (1'b1 ^ a[BPBM1] ^ b[BPBM1]);
2816
                                nf <= res12[BPBM1];
2817
                                zf <= res12[`LOBYTE]==12'h000;
2818
                                accb <= res12[`LOBYTE];
2819
                        end
2820 21 robfinch
`ifdef SUPPORT_6309
2821
                `ADDE_IMM,`ADDE_DP,`ADDE_NDX,`ADDE_EXT:
2822
                        begin
2823
                                cf <= (a[BPBM1]&b[BPBM1])|(a[BPBM1]&~res12[BPBM1])|(b[BPBM1]&~res12[BPBM1]);
2824
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
2825
                                vf <= (res12[BPBM1] ^ b[BPBM1]) & (1'b1 ^ a[BPBM1] ^ b[BPBM1]);
2826
                                nf <= res12[BPBM1];
2827
                                zf <= res12[`LOBYTE]==12'h000;
2828
                                accf <= res12[`LOBYTE];
2829
                        end
2830
                `ADDF_IMM,`ADDF_DP,`ADDF_NDX,`ADDF_EXT:
2831
                        begin
2832
                                cf <= (a[BPBM1]&b[BPBM1])|(a[BPBM1]&~res12[BPBM1])|(b[BPBM1]&~res12[BPBM1]);
2833
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
2834
                                vf <= (res12[BPBM1] ^ b[BPBM1]) & (1'b1 ^ a[BPBM1] ^ b[BPBM1]);
2835
                                nf <= res12[BPBM1];
2836
                                zf <= res12[`LOBYTE]==12'h000;
2837
                                acce <= res12[`LOBYTE];
2838
                        end
2839
`endif
2840 2 robfinch
                `ADDD_IMM,`ADDD_DP,`ADDD_NDX,`ADDD_EXT:
2841
                        begin
2842
                                cf <= (a[BPBX2M1]&b[BPBX2M1])|(a[BPBX2M1]&~res[BPBX2M1])|(b[BPBX2M1]&~res[BPBX2M1]);
2843
                                vf <= (res[BPBX2M1] ^ b[BPBX2M1]) & (1'b1 ^ a[BPBX2M1] ^ b[BPBX2M1]);
2844
                                nf <= res[BPBX2M1];
2845
                                zf <= res[`DBLBYTE]==24'h000000;
2846
                                acca <= res[`HIBYTE];
2847
                                accb <= res[`LOBYTE];
2848
                        end
2849 21 robfinch
`ifdef SUPPORT_6309
2850
                `ADDW_IMM,`ADDW_DP,`ADDW_NDX,`ADDW_EXT:
2851
                        begin
2852
                                cf <= (a[BPBX2M1]&b[BPBX2M1])|(a[BPBX2M1]&~res[BPBX2M1])|(b[BPBX2M1]&~res[BPBX2M1]);
2853
                                vf <= (res[BPBX2M1] ^ b[BPBX2M1]) & (1'b1 ^ a[BPBX2M1] ^ b[BPBX2M1]);
2854
                                nf <= res[BPBX2M1];
2855
                                zf <= res[`DBLBYTE]==24'h000000;
2856
                                acce <= res[`HIBYTE];
2857
                                accf <= res[`LOBYTE];
2858
                        end
2859
                `ADCD_IMM,`ADCD_DP,`ADCD_NDX,`ADCD_EXT:
2860
                        begin
2861
                                cf <= (a[BPBX2M1]&b[BPBX2M1])|(a[BPBX2M1]&~res[BPBX2M1])|(b[BPBX2M1]&~res[BPBX2M1]);
2862
                                vf <= (res[BPBX2M1] ^ b[BPBX2M1]) & (1'b1 ^ a[BPBX2M1] ^ b[BPBX2M1]);
2863
                                nf <= res[BPBX2M1];
2864
                                zf <= res[`DBLBYTE]==24'h0000;
2865
                                acca <= res[`HIBYTE];
2866
                                accb <= res[`LOBYTE];
2867
                        end
2868
                `OIM_DP,`OIM_NDX,`OIM_EXT,
2869
                `EIM_DP,`EIM_NDX,`EIM_EXT,
2870
                `TIM_DP,`TIM_NDX,`TIM_EXT,
2871
                `AIM_DP,`AIM_NDX,`AIM_EXT:
2872
                        begin
2873
                                vf <= 1'b0;
2874
                                nf <= res12n;
2875
                                zf <= res12z;
2876
                        end
2877
`endif
2878 2 robfinch
                `ANDA_IMM,`ANDA_DP,`ANDA_NDX,`ANDA_EXT:
2879
                        begin
2880
                                nf <= res12n;
2881
                                zf <= res12z;
2882
                                vf <= 1'b0;
2883
                                acca <= res12[`LOBYTE];
2884
                        end
2885
                `ANDB_IMM,`ANDB_DP,`ANDB_NDX,`ANDB_EXT:
2886
                        begin
2887
                                nf <= res12n;
2888
                                zf <= res12z;
2889
                                vf <= 1'b0;
2890
                                accb <= res12[`LOBYTE];
2891
                        end
2892 21 robfinch
`ifdef SUPPORT_6309
2893
                `ORD_IMM,`ORD_DP,`ORD_NDX,`ORD_EXT,
2894
                `EORD_IMM,`EORD_DP,`EORD_NDX,`EORD_EXT,
2895
                `ANDD_IMM,`ANDD_DP,`ANDD_NDX,`ANDD_EXT:
2896
                        begin
2897
                                nf <= res24n;
2898
                                zf <= res24z;
2899
                                vf <= 1'b0;
2900
                                acca <= res[`HIBYTE];
2901
                                accb <= res[`LOBYTE];
2902
                        end
2903
                `BITD_IMM,`BITD_DP,`BITD_NDX,`BITD_EXT:
2904
                        begin
2905
                                nf <= res24n;
2906
                                zf <= res24z;
2907
                                vf <= 1'b0;
2908
                        end
2909
`endif
2910 2 robfinch
                `ASLA:
2911
                        begin
2912
                                cf <= res12c;
2913
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
2914
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
2915
                                nf <= res12[BPBM1];
2916
                                zf <= res12[`LOBYTE]==12'h000;
2917
                                acca <= res12[`LOBYTE];
2918
                        end
2919
                `ASLB:
2920
                        begin
2921
                                cf <= res12c;
2922
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
2923
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
2924
                                nf <= res12[BPBM1];
2925
                                zf <= res12[`LOBYTE]==12'h000;
2926
                                accb <= res12[`LOBYTE];
2927
                        end
2928 21 robfinch
`ifdef H6309
2929
                `ASLD,`ROLD:
2930
                        begin
2931
                                cf <= resc;
2932
                                nf <= resn;
2933
                                zf <= resz;
2934
                                vf <= acca[bitsPerByte-1]^acca[bitsPerByte-2];
2935
                                acca <= res[`HIBYTE];
2936
                                accb <= res[`LOBYTE];
2937
                        end
2938
                `ASRD:
2939
                        begin
2940
                                cf <= resc;
2941
                                nf <= resn;
2942
                                zf <= resz;
2943
                                vf <= acca[bitsPerByte-1]^acca[bitsPerByte-2];
2944
                                acca <= res[`HIBYTE];
2945
                                accb <= res[`LOBYTE];
2946
                        end
2947
                `LSRD,`RORD:
2948
                        begin
2949
                                cf <= resc;
2950
                                nf <= resn;
2951
                                zf <= resz;
2952
                                vf <= acca[bitsPerByte-1]^acca[bitsPerByte-2];
2953
                                acca <= res[`HIBYTE];
2954
                                accb <= res[`LOBYTE];
2955
                        end
2956
                `LSRW:
2957
                        begin
2958
                                cf <= resc;
2959
                                nf <= resn;
2960
                                zf <= resz;
2961
                                vf <= acce[bitsPerByte-1]^acce[bitsPerByte-2];
2962
                                acce <= res[`HIBYTE];
2963
                                accf <= res[`LOBYTE];
2964
                        end
2965
                `ROLW,`RORW:
2966
                        begin
2967
                                cf <= resc;
2968
                                nf <= resn;
2969
                                zf <= resz;
2970
                                vf <= acce[bitsPerByte-1]^acce[bitsPerByte-2];
2971
                                acce <= res[`HIBYTE];
2972
                                accf <= res[`LOBYTE];
2973
                        end
2974
`endif
2975 2 robfinch
                `ASL_DP,`ASL_NDX,`ASL_EXT:
2976
                        begin
2977
                                cf <= res12c;
2978
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
2979
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
2980
                                nf <= res12[BPBM1];
2981
                                zf <= res12[`LOBYTE]==12'h000;
2982
                        end
2983
                `ASRA:
2984
                        begin
2985
                                cf <= res12c;
2986
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
2987
                                nf <= res12[BPBM1];
2988
                                zf <= res12[`LOBYTE]==12'h000;
2989
                                acca <= res12[`LOBYTE];
2990
                        end
2991
                `ASRB:
2992
                        begin
2993
                                cf <= res12c;
2994
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
2995
                                nf <= res12[BPBM1];
2996
                                zf <= res12[`LOBYTE]==12'h000;
2997
                                accb <= res12[`LOBYTE];
2998
                        end
2999
                `ASR_DP,`ASR_NDX,`ASR_EXT:
3000
                        begin
3001
                                cf <= res12c;
3002
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
3003
                                nf <= res12[BPBM1];
3004
                                zf <= res12[`LOBYTE]==12'h000;
3005
                        end
3006
                `BITA_IMM,`BITA_DP,`BITA_NDX,`BITA_EXT,
3007
                `BITB_IMM,`BITB_DP,`BITB_NDX,`BITB_EXT:
3008
                        begin
3009
                                vf <= 1'b0;
3010
                                nf <= res12[BPBM1];
3011
                                zf <= res12[`LOBYTE]==12'h000;
3012
                        end
3013
                `CLRA:
3014
                        begin
3015
                                vf <= 1'b0;
3016
                                cf <= 1'b0;
3017
                                nf <= 1'b0;
3018
                                zf <= 1'b1;
3019
                                acca <= 12'h000;
3020
                        end
3021
                `CLRB:
3022
                        begin
3023
                                vf <= 1'b0;
3024
                                cf <= 1'b0;
3025
                                nf <= 1'b0;
3026
                                zf <= 1'b1;
3027
                                accb <= 12'h000;
3028
                        end
3029 21 robfinch
`ifdef SUPPORT_6309
3030
                `CLRD:
3031
                        begin
3032
                                vf <= 1'b0;
3033
                                cf <= 1'b0;
3034
                                nf <= 1'b0;
3035
                                zf <= 1'b1;
3036
                                acca <= 12'h000;
3037
                                accb <= 12'h000;
3038
                        end
3039
                `CLRW:
3040
                        begin
3041
                                vf <= 1'b0;
3042
                                cf <= 1'b0;
3043
                                nf <= 1'b0;
3044
                                zf <= 1'b1;
3045
                                acce <= 12'h000;
3046
                                accf <= 12'h000;
3047
                        end
3048
                `CLRE:
3049
                        begin
3050
                                vf <= 1'b0;
3051
                                cf <= 1'b0;
3052
                                nf <= 1'b0;
3053
                                zf <= 1'b1;
3054
                                acce <= 12'h000;
3055
                        end
3056
                `CLRF:
3057
                        begin
3058
                                vf <= 1'b0;
3059
                                cf <= 1'b0;
3060
                                nf <= 1'b0;
3061
                                zf <= 1'b1;
3062
                                accf <= 12'h000;
3063
                        end
3064
`endif
3065 2 robfinch
                `CLR_DP,`CLR_NDX,`CLR_EXT:
3066
                        begin
3067
                                vf <= 1'b0;
3068
                                cf <= 1'b0;
3069
                                nf <= 1'b0;
3070
                                zf <= 1'b1;
3071
                        end
3072 21 robfinch
`ifdef SUPPORT_6309
3073
                `CMPE_IMM,`CMPE_DP,`CMPE_NDX,`CMPE_EXT,
3074
                `CMPF_IMM,`CMPF_DP,`CMPF_NDX,`CMPF_EXT,
3075
`endif
3076 2 robfinch
                `CMPA_IMM,`CMPA_DP,`CMPA_NDX,`CMPA_EXT,
3077
                `CMPB_IMM,`CMPB_DP,`CMPB_NDX,`CMPB_EXT:
3078
                        begin
3079
                                cf <= (~a[BPBM1]&b[BPBM1])|(res12[BPBM1]&~a[BPBM1])|(res12[BPBM1]&b[BPBM1]);
3080
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
3081
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
3082
                                nf <= res12[BPBM1];
3083
                                zf <= res12[`LOBYTE]==12'h000;
3084
                        end
3085 21 robfinch
`ifdef SUPPORT_6309
3086
                `CMPW_IMM,`CMPW_DP,`CMPW_NDX,`CMPW_EXT,
3087
`endif
3088 2 robfinch
                `CMPD_IMM,`CMPD_DP,`CMPD_NDX,`CMPD_EXT:
3089
                        begin
3090
                                cf <= (~a[BPBX2M1]&b[BPBX2M1])|(res[BPBX2M1]&~a[BPBX2M1])|(res[BPBX2M1]&b[BPBX2M1]);
3091
                                vf <= (1'b1 ^ res[BPBX2M1] ^ b[BPBX2M1]) & (a[BPBX2M1] ^ b[BPBX2M1]);
3092
                                nf <= res[BPBX2M1];
3093
                                zf <= res[`DBLBYTE]==24'h000000;
3094
                        end
3095
                `CMPS_IMM,`CMPS_DP,`CMPS_NDX,`CMPS_EXT,
3096
                `CMPU_IMM,`CMPU_DP,`CMPU_NDX,`CMPU_EXT,
3097
                `CMPX_IMM,`CMPX_DP,`CMPX_NDX,`CMPX_EXT,
3098
                `CMPY_IMM,`CMPY_DP,`CMPY_NDX,`CMPY_EXT:
3099
                        begin
3100
                                cf <= (~a[BPBX2M1]&b[BPBX2M1])|(res[BPBX2M1]&~a[BPBX2M1])|(res[BPBX2M1]&b[BPBX2M1]);
3101
                                vf <= (1'b1 ^ res[BPBX2M1] ^ b[BPBX2M1]) & (a[BPBX2M1] ^ b[BPBX2M1]);
3102
                                nf <= res[BPBX2M1];
3103
                                zf <= res[`DBLBYTE]==24'h000000;
3104
                        end
3105
                `COMA:
3106
                        begin
3107
                                cf <= 1'b1;
3108
                                vf <= 1'b0;
3109
                                nf <= res12n;
3110
                                zf <= res12z;
3111
                                acca <= res12[`LOBYTE];
3112
                        end
3113
                `COMB:
3114
                        begin
3115
                                cf <= 1'b1;
3116
                                vf <= 1'b0;
3117
                                nf <= res12n;
3118
                                zf <= res12z;
3119
                                accb <= res12[`LOBYTE];
3120
                        end
3121 21 robfinch
`ifdef SUPPORT_6309
3122
                `COME:
3123
                        begin
3124
                                cf <= 1'b1;
3125
                                vf <= 1'b0;
3126
                                nf <= res12n;
3127
                                zf <= res12z;
3128
                                acce <= res12[`LOBYTE];
3129
                        end
3130
                `COMF:
3131
                        begin
3132
                                cf <= 1'b1;
3133
                                vf <= 1'b0;
3134
                                nf <= res12n;
3135
                                zf <= res12z;
3136
                                accf <= res12[`LOBYTE];
3137
                        end
3138
                `COMD:
3139
                        begin
3140
                                cf <= 1'b1;
3141
                                vf <= 1'b0;
3142
                                nf <= res24n;
3143
                                zf <= res24z;
3144
                                {acca,accb} <= res;
3145
                        end
3146
                `COMW:
3147
                        begin
3148
                                cf <= 1'b1;
3149
                                vf <= 1'b0;
3150
                                nf <= res24n;
3151
                                zf <= res24z;
3152
                                {acce,accf} <= res;
3153
                        end
3154
`endif
3155 2 robfinch
                `COM_DP,`COM_NDX,`COM_EXT:
3156
                        begin
3157
                                cf <= 1'b1;
3158
                                vf <= 1'b0;
3159
                                nf <= res12n;
3160
                                zf <= res12z;
3161
                        end
3162
                `DAA:
3163
                        begin
3164
                                cf <= res12c;
3165
                                zf <= res12z;
3166
                                nf <= res12n;
3167
                                vf <= (res12[BPBM1] ^ b[BPBM1]) & (1'b1 ^ a[BPBM1] ^ b[BPBM1]);
3168
                                acca <= res12[`LOBYTE];
3169
                        end
3170
                `DECA:
3171
                        begin
3172
                                nf <= res12n;
3173
                                zf <= res12z;
3174
                                vf <= res12[BPBM1] != acca[BPBM1];
3175
                                acca <= res12[`LOBYTE];
3176
                        end
3177
                `DECB:
3178
                        begin
3179
                                nf <= res12n;
3180
                                zf <= res12z;
3181
                                vf <= res12[BPBM1] != accb[BPBM1];
3182
                                accb <= res12[`LOBYTE];
3183
                        end
3184 21 robfinch
`ifdef SUPPORT_6309
3185
                `DECE:
3186
                        begin
3187
                                nf <= res12n;
3188
                                zf <= res12z;
3189
                                vf <= res12[BPBM1] != acce[BPBM1];
3190
                                acce <= res12[`LOBYTE];
3191
                        end
3192
                `DECF:
3193
                        begin
3194
                                nf <= res12n;
3195
                                zf <= res12z;
3196
                                vf <= res12[BPBM1] != accf[BPBM1];
3197
                                accf <= res12[`LOBYTE];
3198
                        end
3199
                `DECD:
3200
                        begin
3201
                                nf <= res24n;
3202
                                zf <= res24z;
3203
                                vf <= res[bitsPerByte*2-1] != acca[bitsPerByte-1];
3204
                                {acca,accb} <= res;
3205
                        end
3206
                `DECW:
3207
                        begin
3208
                                nf <= res24n;
3209
                                zf <= res24z;
3210
                                vf <= res[bitsPerByte*2-1] != acce[bitsPerByte-1];
3211
                                {acce,accf} <= res;
3212
                        end
3213
`endif
3214 2 robfinch
                `DEC_DP,`DEC_NDX,`DEC_EXT:
3215
                        begin
3216
                                nf <= res12n;
3217
                                zf <= res12z;
3218
                                vf <= res12[BPBM1] != b[BPBM1];
3219
                        end
3220
                `EORA_IMM,`EORA_DP,`EORA_NDX,`EORA_EXT,
3221
                `ORA_IMM,`ORA_DP,`ORA_NDX,`ORA_EXT:
3222
                        begin
3223
                                nf <= res12n;
3224
                                zf <= res12z;
3225
                                vf <= 1'b0;
3226
                                acca <= res12[`LOBYTE];
3227
                        end
3228
                `EORB_IMM,`EORB_DP,`EORB_NDX,`EORB_EXT,
3229
                `ORB_IMM,`ORB_DP,`ORB_NDX,`ORB_EXT:
3230
                        begin
3231
                                nf <= res12n;
3232
                                zf <= res12z;
3233
                                vf <= 1'b0;
3234
                                accb <= res12[`LOBYTE];
3235
                        end
3236
                `EXG:
3237
                        begin
3238
                                case(ir[bitsPerByte+3:bitsPerByte])
3239
                                4'b0000:
3240
                                                        begin
3241
                                                                acca <= src1[`HIBYTE];
3242
                                                                accb <= src1[`LOBYTE];
3243
                                                        end
3244
                                4'b0001:        xr <= src1;
3245
                                4'b0010:        yr <= src1;
3246
                                4'b0011:        usp <= src1;
3247
                                4'b0100:        begin ssp <= src1; nmi_armed <= `TRUE; end
3248
                                4'b0101:        pc <= src1[`DBLBYTE];
3249
                                4'b1000:        acca <= src1[`LOBYTE];
3250
                                4'b1001:        accb <= src1[`LOBYTE];
3251
                                4'b1010:
3252
                                        begin
3253
                                                cf <= src1[0];
3254
                                                vf <= src1[1];
3255
                                                zf <= src1[2];
3256
                                                nf <= src1[3];
3257
                                                im <= src1[4];
3258
                                                hf <= src1[5];
3259
                                                firqim <= src1[6];
3260
                                                ef <= src1[7];
3261
                                        end
3262
                                4'b1011:        dpr <= src1[`LOBYTE];
3263 21 robfinch
                                4'b1100:        usppg <= src1[`DBLBYTE];
3264
`ifdef SUPPORT_6309
3265
                                4'b0110:        {acce,accf} <= src1[`DBLBYTE];
3266
                                4'b1110:        acce <= src1[`LOBYTE];
3267
                                4'b1111:        accf <= src1[`LOBYTE];
3268
`else
3269
                                4'b1110:        ;
3270 2 robfinch
                                4'b1111:        ;
3271 21 robfinch
`endif
3272 2 robfinch
                                default:        ;
3273
                                endcase
3274
                                case(ir[bitsPerByte+7:bitsPerByte+4])
3275
                                4'b0000:
3276
                                                        begin
3277
                                                                acca <= src2[`HIBYTE];
3278
                                                                accb <= src2[`LOBYTE];
3279
                                                        end
3280
                                4'b0001:        xr <= src2;
3281
                                4'b0010:        yr <= src2;
3282
                                4'b0011:        usp <= src2;
3283
                                4'b0100:        begin ssp <= src2; nmi_armed <= `TRUE; end
3284
                                4'b0101:        pc <= src2[`DBLBYTE];
3285
                                4'b1000:        acca <= src2[`LOBYTE];
3286
                                4'b1001:        accb <= src2[`LOBYTE];
3287
                                4'b1010:
3288
                                        begin
3289
                                                cf <= src2[0];
3290
                                                vf <= src2[1];
3291
                                                zf <= src2[2];
3292
                                                nf <= src2[3];
3293
                                                im <= src2[4];
3294
                                                hf <= src2[5];
3295
                                                firqim <= src2[6];
3296
                                                ef <= src2[7];
3297
                                        end
3298
                                4'b1011:        dpr <= src2[`LOBYTE];
3299 21 robfinch
                                4'b1100:        usppg <= src2[`DBLBYTE];
3300
`ifdef SUPPORT_6309
3301
                                4'b1110:        acce <= src2[`LOBYTE];
3302
                                4'b1111:        accf <= src2[`LOBYTE];
3303
`else
3304
                                4'b1110:        ;
3305 2 robfinch
                                4'b1111:        ;
3306 21 robfinch
`endif
3307 2 robfinch
                                default:        ;
3308
                                endcase
3309
                        end
3310
                `INCA:
3311
                        begin
3312
                                nf <= res12n;
3313
                                zf <= res12z;
3314
                                vf <= res12[BPBM1] != acca[BPBM1];
3315
                                acca <= res12[`LOBYTE];
3316
                        end
3317
                `INCB:
3318
                        begin
3319
                                nf <= res12n;
3320
                                zf <= res12z;
3321
                                vf <= res12[BPBM1] != accb[BPBM1];
3322
                                accb <= res12[`LOBYTE];
3323
                        end
3324 21 robfinch
`ifdef SUPPORT_6309
3325
                `INCE:
3326
                        begin
3327
                                nf <= res12n;
3328
                                zf <= res12z;
3329
                                vf <= res12[BPBM1] != acce[BPBM1];
3330
                                acce <= res12[`LOBYTE];
3331
                        end
3332
                `INCF:
3333
                        begin
3334
                                nf <= res12n;
3335
                                zf <= res12z;
3336
                                vf <= res12[BPBM1] != accf[BPBM1];
3337
                                accf <= res12[`LOBYTE];
3338
                        end
3339
                `INCD:
3340
                        begin
3341
                                nf <= res24n;
3342
                                zf <= res24z;
3343
                                vf <= res[bitsPerByte*2-1] != acca[bitsPerByte-1];
3344
                                {acca,accb} <= res[`LOBYTE];
3345
                        end
3346
                `INCW:
3347
                        begin
3348
                                nf <= res24n;
3349
                                zf <= res24z;
3350
                                vf <= res[bitsPerByte*2-1] != acce[bitsPerByte-1];
3351
                                {acce,accf} <= res[`LOBYTE];
3352
                        end
3353
                `LDE_IMM,`LDE_DP,`LDE_NDX,`LDE_EXT:
3354
                        begin
3355
                                vf <= 1'b0;
3356
                                zf <= res12z;
3357
                                nf <= res12n;
3358
                                acce <= res12[`LOBYTE];
3359
                        end
3360
                `LDF_IMM,`LDF_DP,`LDF_NDX,`LDF_EXT:
3361
                        begin
3362
                                vf <= 1'b0;
3363
                                zf <= res12z;
3364
                                nf <= res12n;
3365
                                accf <= res12[`LOBYTE];
3366
                        end
3367
`endif
3368 2 robfinch
                `INC_DP,`INC_NDX,`INC_EXT:
3369
                        begin
3370
                                nf <= res12n;
3371
                                zf <= res12z;
3372
                                vf <= res12[BPBM1] != b[BPBM1];
3373
                        end
3374
                `LDA_IMM,`LDA_DP,`LDA_NDX,`LDA_EXT:
3375
                        begin
3376
                                vf <= 1'b0;
3377
                                zf <= res12z;
3378
                                nf <= res12n;
3379
                                acca <= res12[`LOBYTE];
3380
                        end
3381
                `LDB_IMM,`LDB_DP,`LDB_NDX,`LDB_EXT:
3382
                        begin
3383
                                vf <= 1'b0;
3384
                                zf <= res12z;
3385
                                nf <= res12n;
3386
                                accb <= res12[`LOBYTE];
3387
                        end
3388
                `LDD_IMM,`LDD_DP,`LDD_NDX,`LDD_EXT:
3389
                        begin
3390
                                vf <= 1'b0;
3391
                                zf <= res24z;
3392
                                nf <= res24n;
3393
                                acca <= res[`HIBYTE];
3394
                                accb <= res[`LOBYTE];
3395
                        end
3396 21 robfinch
`ifdef SUPPORT_6309
3397
                `LDW_IMM,`LDW_DP,`LDW_NDX,`LDW_EXT:
3398
                        begin
3399
                                vf <= 1'b0;
3400
                                zf <= res24z;
3401
                                nf <= res24n;
3402
                                acce <= res[`HIBYTE];
3403
                                accf <= res[`LOBYTE];
3404
                        end
3405
`endif
3406 2 robfinch
                `LDU_IMM,`LDU_DP,`LDU_NDX,`LDU_EXT:
3407
                        begin
3408
                                vf <= 1'b0;
3409
                                zf <= res24z;
3410
                                nf <= res24n;
3411
                                usp <= res[`DBLBYTE];
3412
                        end
3413
                `LDS_IMM,`LDS_DP,`LDS_NDX,`LDS_EXT:
3414
                        begin
3415
                                vf <= 1'b0;
3416
                                zf <= res24z;
3417
                                nf <= res24n;
3418
                                ssp <= res[`DBLBYTE];
3419
                                nmi_armed <= 1'b1;
3420
                        end
3421
                `LDX_IMM,`LDX_DP,`LDX_NDX,`LDX_EXT:
3422
                        begin
3423
                                vf <= 1'b0;
3424
                                zf <= res24z;
3425
                                nf <= res24n;
3426
                                xr <= res[`DBLBYTE];
3427
                        end
3428
                `LDY_IMM,`LDY_DP,`LDY_NDX,`LDY_EXT:
3429
                        begin
3430
                                vf <= 1'b0;
3431
                                zf <= res24z;
3432
                                nf <= res24n;
3433
                                yr <= res[`DBLBYTE];
3434
                        end
3435
                `LEAS_NDX:
3436
                        begin ssp <= res[`DBLBYTE]; nmi_armed <= 1'b1; end
3437
                `LEAU_NDX:
3438
                        usp <= res[`DBLBYTE];
3439
                `LEAX_NDX:
3440
                        begin
3441
                                zf <= res24z;
3442
                                xr <= res[`DBLBYTE];
3443
                        end
3444
                `LEAY_NDX:
3445
                        begin
3446
                                zf <= res24z;
3447
                                yr <= res[`DBLBYTE];
3448
                        end
3449
                `LSRA:
3450
                        begin
3451
                                cf <= res12c;
3452
                                nf <= res12[BPBM1];
3453
                                zf <= res12[`LOBYTE]==12'h000;
3454
                                acca <= res12[`LOBYTE];
3455
                        end
3456
                `LSRB:
3457
                        begin
3458
                                cf <= res12c;
3459
                                nf <= res12[BPBM1];
3460
                                zf <= res12[`LOBYTE]==12'h000;
3461
                                accb <= res12[`LOBYTE];
3462
                        end
3463
                `LSR_DP,`LSR_NDX,`LSR_EXT:
3464
                        begin
3465
                                cf <= res12c;
3466
                                nf <= res12[BPBM1];
3467
                                zf <= res12[`LOBYTE]==12'h000;
3468
                        end
3469
                `MUL:
3470
                        begin
3471
                                cf <= prod[BPBM1];
3472
                                zf <= res24z;
3473
                                acca <= prod[`HIBYTE];
3474
                                accb <= prod[`LOBYTE];
3475
                        end
3476
                `NEGA:
3477
                        begin
3478
                                cf <= (~a[BPBM1]&b[BPBM1])|(res12[BPBM1]&~a[BPBM1])|(res12[BPBM1]&b[BPBM1]);
3479
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
3480
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
3481
                                nf <= res12[BPBM1];
3482
                                zf <= res12[`LOBYTE]==12'h000;
3483
                                acca <= res12[`LOBYTE];
3484
                        end
3485
                `NEGB:
3486
                        begin
3487
                                cf <= (~a[BPBM1]&b[BPBM1])|(res12[BPBM1]&~a[BPBM1])|(res12[BPBM1]&b[BPBM1]);
3488
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
3489
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
3490
                                nf <= res12[BPBM1];
3491
                                zf <= res12[`LOBYTE]==12'h000;
3492
                                accb <= res12[`LOBYTE];
3493
                        end
3494 21 robfinch
`ifdef SUPPORT_6309
3495
                `NEGD:
3496
                        begin
3497
                                cf <= (~a[bitsPerByte*2-1]&b[bitsPerByte*2-1])|(res[bitsPerByte*2-1]&~a[bitsPerByte*2-1])|(res[bitsPerByte*2-1]&b[bitsPerByte*2-1]);
3498
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res[`HCBIT]&~a[`HCBIT])|(res[`HCBIT]&b[`HCBIT]);
3499
                                vf <= (1'b1 ^ res[bitsPerByte*2-1] ^ b[bitsPerByte*2-1]) & (a[bitsPerByte*2-1] ^ b[bitsPerByte*2-1]);
3500
                                nf <= res[bitsPerByte*2-1];
3501
                                zf <= res[`DBLBYTE]=='h0;
3502
                                {acca,accb} <= res;
3503
                        end
3504
`endif
3505 2 robfinch
                `NEG_DP,`NEG_NDX,`NEG_EXT:
3506
                        begin
3507
                                cf <= (~a[BPBM1]&b[BPBM1])|(res12[BPBM1]&~a[BPBM1])|(res12[BPBM1]&b[BPBM1]);
3508
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
3509
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
3510
                                nf <= res12[BPBM1];
3511
                                zf <= res12[`LOBYTE]==12'h000;
3512
                        end
3513
                `ROLA:
3514
                        begin
3515
                                cf <= res12c;
3516
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
3517
                                nf <= res12[BPBM1];
3518
                                zf <= res12[`LOBYTE]==12'h000;
3519
                                acca <= res12[`LOBYTE];
3520
                        end
3521
                `ROLB:
3522
                        begin
3523
                                cf <= res12c;
3524
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
3525
                                nf <= res12[BPBM1];
3526
                                zf <= res12[`LOBYTE]==12'h000;
3527
                                accb <= res12[`LOBYTE];
3528
                        end
3529
                `ROL_DP,`ROL_NDX,`ROL_EXT:
3530
                        begin
3531
                                cf <= res12c;
3532
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
3533
                                nf <= res12[BPBM1];
3534
                                zf <= res12[`LOBYTE]==12'h000;
3535
                        end
3536
                `RORA:
3537
                        begin
3538
                                cf <= res12c;
3539
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
3540
                                nf <= res12[BPBM1];
3541
                                zf <= res12[`LOBYTE]==12'h000;
3542
                                acca <= res12[`LOBYTE];
3543
                        end
3544
                `RORB:
3545
                        begin
3546
                                cf <= res12c;
3547
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
3548
                                nf <= res12[BPBM1];
3549
                                zf <= res12[`LOBYTE]==12'h000;
3550
                                accb <= res12[`LOBYTE];
3551
                        end
3552
                `ROR_DP,`ROR_NDX,`ROR_EXT:
3553
                        begin
3554
                                cf <= res12c;
3555
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
3556
                                nf <= res12[BPBM1];
3557
                                zf <= res12[`LOBYTE]==12'h000;
3558
                        end
3559
                `SBCA_IMM,`SBCA_DP,`SBCA_NDX,`SBCA_EXT:
3560
                        begin
3561
                                cf <= (~a[BPBM1]&b[BPBM1])|(res12[BPBM1]&~a[BPBM1])|(res12[BPBM1]&b[BPBM1]);
3562
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
3563
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
3564
                                nf <= res12[BPBM1];
3565
                                zf <= res12[`LOBYTE]==12'h000;
3566
                                acca <= res12[`LOBYTE];
3567
                        end
3568
                `SBCB_IMM,`SBCB_DP,`SBCB_NDX,`SBCB_EXT:
3569
                        begin
3570
                                cf <= (~a[BPBM1]&b[BPBM1])|(res12[BPBM1]&~a[BPBM1])|(res12[BPBM1]&b[BPBM1]);
3571
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
3572
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
3573
                                nf <= res12[BPBM1];
3574
                                zf <= res12[`LOBYTE]==12'h000;
3575
                                accb <= res12[`LOBYTE];
3576
                        end
3577
                `SEX:
3578
                        begin
3579
                                vf <= 1'b0;
3580
                                nf <= res12n;
3581
                                zf <= res12z;
3582
                                acca <= res12[`LOBYTE];
3583
                        end
3584 21 robfinch
`ifdef SUPPORT_6309
3585
                `STE_DP,`STE_NDX,`STE_EXT,
3586
                `STF_DP,`STF_NDX,`STF_EXT,
3587
`endif
3588 2 robfinch
                `STA_DP,`STA_NDX,`STA_EXT,
3589
                `STB_DP,`STB_NDX,`STB_EXT:
3590
                        begin
3591
                                vf <= 1'b0;
3592
                                zf <= res12z;
3593
                                nf <= res12n;
3594
                        end
3595 21 robfinch
`ifdef SUPPORT_6309
3596
                `STW_DP,`STW_NDX,`STW_EXT,
3597
`endif
3598 2 robfinch
                `STD_DP,`STD_NDX,`STD_EXT,
3599
                `STU_DP,`STU_NDX,`STU_EXT,
3600
                `STX_DP,`STX_NDX,`STX_EXT,
3601
                `STY_DP,`STY_NDX,`STY_EXT:
3602
                        begin
3603
                                vf <= 1'b0;
3604
                                zf <= res24z;
3605
                                nf <= res24n;
3606
                        end
3607
                `TFR:
3608
                        begin
3609
                                case(ir[bitsPerByte+3:bitsPerByte])
3610
                                4'b0000:
3611
                                                        begin
3612
                                                                acca <= src1[`HIBYTE];
3613
                                                                accb <= src1[`LOBYTE];
3614
                                                        end
3615
                                4'b0001:        xr <= src1;
3616
                                4'b0010:        yr <= src1;
3617
                                4'b0011:        usp <= src1;
3618
                                4'b0100:        begin ssp <= src1; nmi_armed <= `TRUE; end
3619
                                4'b0101:        pc <= src1[`DBLBYTE];
3620
                                4'b1000:        acca <= src1[`LOBYTE];
3621
                                4'b1001:        accb <= src1[`LOBYTE];
3622
                                4'b1010:
3623
                                        begin
3624
                                                cf <= src1[0];
3625
                                                vf <= src1[1];
3626
                                                zf <= src1[2];
3627
                                                nf <= src1[3];
3628
                                                im <= src1[4];
3629
                                                hf <= src1[5];
3630
                                                firqim <= src1[6];
3631
                                                ef <= src1[7];
3632
                                        end
3633
                                4'b1011:        dpr <= src1[`LOBYTE];
3634 21 robfinch
                                4'b1100:        usppg <= src1[`DBLBYTE];
3635
`ifdef SUPPORT_6309
3636
                                4'b0110:        {acce,accf} <= src1[`DBLBYTE];
3637
                                4'b1110:        acce <= src1[`LOBYTE];
3638
                                4'b1111:        accf <= src1[`LOBYTE];
3639
`else
3640
                                4'b1110:        ;
3641 2 robfinch
                                4'b1111:        ;
3642 21 robfinch
`endif
3643 2 robfinch
                                default:        ;
3644
                                endcase
3645
                        end
3646 21 robfinch
                `TSTE,`TSTF,
3647 2 robfinch
                `TSTA,`TSTB:
3648
                        begin
3649
                                vf <= 1'b0;
3650
                                nf <= res12n;
3651
                                zf <= res12z;
3652
                        end
3653
                `TSTD:
3654
                        begin
3655
                                vf <= 1'b0;
3656
                                nf <= res24n;
3657
                                zf <= res24z;
3658
                        end
3659 21 robfinch
`ifdef SUPPORT_6309
3660
                `TSTW:
3661
                        begin
3662
                                vf <= 1'b0;
3663
                                nf <= res24n;
3664
                                zf <= res24z;
3665
                        end
3666
`endif
3667 2 robfinch
                `TST_DP,`TST_NDX,`TST_EXT:
3668
                        begin
3669
                                vf <= 1'b0;
3670
                                nf <= res12n;
3671
                                zf <= res12z;
3672
                        end
3673
                `SUBA_IMM,`SUBA_DP,`SUBA_NDX,`SUBA_EXT:
3674
                        begin
3675
                                acca <= res12[`LOBYTE];
3676
                                nf <= res12n;
3677
                                zf <= res12z;
3678
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
3679
                                cf <= res12c;
3680
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
3681
                        end
3682
                `SUBB_IMM,`SUBB_DP,`SUBB_NDX,`SUBB_EXT:
3683
                        begin
3684
                                accb <= res12[`LOBYTE];
3685
                                nf <= res12n;
3686
                                zf <= res12z;
3687
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
3688
                                cf <= res12c;
3689
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
3690
                        end
3691 21 robfinch
`ifdef SUPPORT_6309
3692
                `SUBE_IMM,`SUBE_DP,`SUBE_NDX,`SUBE_EXT:
3693
                        begin
3694
                                acce <= res12[`LOBYTE];
3695
                                nf <= res12n;
3696
                                zf <= res12z;
3697
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
3698
                                cf <= res12c;
3699
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
3700
                        end
3701
                `SUBF_IMM,`SUBF_DP,`SUBF_NDX,`SUBF_EXT:
3702
                        begin
3703
                                accf <= res12[`LOBYTE];
3704
                                nf <= res12n;
3705
                                zf <= res12z;
3706
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
3707
                                cf <= res12c;
3708
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
3709
                        end
3710
                `SUBW_IMM,`SUBW_DP,`SUBW_NDX,`SUBW_EXT:
3711
                        begin
3712
                                cf <= res24c;
3713
                                vf <= (1'b1 ^ res[BPBX2M1] ^ b[BPBX2M1]) & (a[BPBX2M1] ^ b[BPBX2M1]);
3714
                                nf <= res[BPBX2M1];
3715
                                zf <= res[`DBLBYTE]==24'h000000;
3716
                                acce <= res[`HIBYTE];
3717
                                accf <= res[`LOBYTE];
3718
                        end
3719
                `SBCD_IMM,`SBCD_DP,`SBCD_NDX,`SBCD_EXT,
3720
`endif
3721 2 robfinch
                `SUBD_IMM,`SUBD_DP,`SUBD_NDX,`SUBD_EXT:
3722
                        begin
3723
                                cf <= res24c;
3724
                                vf <= (1'b1 ^ res[BPBX2M1] ^ b[BPBX2M1]) & (a[BPBX2M1] ^ b[BPBX2M1]);
3725
                                nf <= res[BPBX2M1];
3726
                                zf <= res[`DBLBYTE]==24'h000000;
3727
                                acca <= res[`HIBYTE];
3728
                                accb <= res[`LOBYTE];
3729
                        end
3730
                endcase
3731
        end
3732
end
3733
endtask
3734
 
3735
task dp_store;
3736
input [5:0] stw;
3737
begin
3738
        store_what <= stw;
3739
        wadr <= dp_address;
3740
        pc <= pc + 2'd2;
3741
        next_state(STORE1);
3742
end
3743
endtask
3744
 
3745
task indexed_store;
3746
input [5:0] stw;
3747
begin
3748
        store_what <= stw;
3749
        pc <= pc + insnsz;
3750
        if (isIndirect) begin
3751
                load_what <= isFar ? `LW_IA2316 : `LW_IAH;
3752
                radr <= NdxAddr;
3753
                next_state(LOAD1);
3754
        end
3755
        else begin
3756
                wadr <= NdxAddr;
3757
                next_state(STORE1);
3758
        end
3759
end
3760
endtask
3761
 
3762
task ex_store;
3763
input [5:0] stw;
3764
begin
3765
        pc <= pc + (isFar ? 3'd4 : 3'd3);
3766
        store_what <= stw;
3767
        wadr <= ex_address;
3768
        next_state(STORE1);
3769
end
3770
endtask
3771
 
3772
task next_state;
3773
input [5:0] st;
3774
begin
3775
        state <= st;
3776
end
3777
endtask
3778
 
3779
task wb_burst;
3780
input [5:0] len;
3781
input [bitsPerByte*2-1:0] adr;
3782
begin
3783
        if (!tsc) begin
3784
                bte_o <= 2'b00;
3785
                cti_o <= 3'b001;
3786
                bl_o <= len;
3787
                cyc_o <= 1'b1;
3788
                stb_o <= 1'b1;
3789 12 robfinch
                we_o <= 1'b0;
3790 2 robfinch
                adr_o <= adr;
3791
        end
3792
end
3793
endtask
3794
 
3795
task wb_read;
3796
input [`TRPBYTE] adr;
3797
begin
3798
        if (!tsc) begin
3799
                cyc_o <= 1'b1;
3800
                stb_o <= 1'b1;
3801 12 robfinch
                we_o <= 1'b0;
3802 2 robfinch
                adr_o <= adr;
3803
        end
3804
end
3805
endtask
3806
 
3807
task wb_write;
3808
input [`TRPBYTE] adr;
3809
input [`LOBYTE] dat;
3810
begin
3811
        if (!tsc) begin
3812
                we_o <= 1'b1;
3813
                adr_o <= adr;
3814
                dat_o <= dat;
3815
        end
3816
end
3817
endtask
3818
 
3819
task wb_nack;
3820
begin
3821
        cti_o <= 3'b000;
3822
        bl_o <= 6'd0;
3823
        cyc_o <= 1'b0;
3824
        stb_o <= 1'b0;
3825
        we_o <= 1'b0;
3826
        adr_o <= 24'd0;
3827
        dat_o <= 12'd0;
3828
end
3829
endtask
3830
 
3831
task load_tsk;
3832
input [`LOBYTE] dat;
3833
begin
3834
        case(load_what)
3835
        `LW_BH:
3836
                        begin
3837
                                radr <= radr + 2'd1;
3838
                                b[`HIBYTE] <= dat;
3839
                                load_what <= `LW_BL;
3840
                                next_state(LOAD1);
3841
                        end
3842
        `LW_BL:
3843
                        begin
3844
                                // Don't increment address here for the benefit of the memory
3845
                                // operate instructions which set wadr=radr in CALC.
3846
                                b[`LOBYTE] <= dat;
3847
                                next_state(CALC);
3848
                        end
3849
        `LW_CCR:        begin
3850
                                next_state(PULL1);
3851
                                radr <= radr + 2'd1;
3852
                                cf <= dat[0];
3853
                                vf <= dat[1];
3854
                                zf <= dat[2];
3855
                                nf <= dat[3];
3856
                                im <= dat[4];
3857
                                hf <= dat[5];
3858
                                firqim <= dat[6];
3859
                                ef <= dat[7];
3860
                                if (isRTI) begin
3861
                                        $display("loaded ccr=%b", dat);
3862
                                        ir[`HIBYTE] <= dat[7] ? 12'hFE : 12'h80;
3863
                                        ssp <= ssp + 2'd1;
3864
                                end
3865
                                else if (isPULS)
3866
                                        ssp <= ssp + 2'd1;
3867
                                else if (isPULU)
3868
                                        usp <= usp + 2'd1;
3869
                        end
3870
        `LW_ACCA:       begin
3871
                                acca <= dat;
3872
                                radr <= radr + 2'd1;
3873
                                if (isRTI) begin
3874
                                        $display("loaded acca=%h from %h", dat, radr);
3875
                                        ssp <= ssp + 2'd1;
3876
                                        next_state(PULL1);
3877
                                end
3878
                                else if (isPULU) begin
3879
                                        usp <= usp + 2'd1;
3880
                                        next_state(PULL1);
3881
                                end
3882
                                else if (isPULS) begin
3883
                                        ssp <= ssp + 2'd1;
3884
                                        next_state(PULL1);
3885
                                end
3886
                                else
3887
                                        next_state(IFETCH);
3888
                        end
3889
        `LW_ACCB:       begin
3890
                                accb <= dat;
3891
                                radr <= radr + 2'd1;
3892
                                if (isRTI) begin
3893 11 robfinch
                                        $display("loaded accb=%h from %h", dat, radr);
3894 2 robfinch
                                        ssp <= ssp + 2'd1;
3895
                                        next_state(PULL1);
3896
                                end
3897
                                else if (isPULU) begin
3898
                                        usp <= usp + 2'd1;
3899
                                        next_state(PULL1);
3900
                                end
3901
                                else if (isPULS) begin
3902
                                        ssp <= ssp + 2'd1;
3903
                                        next_state(PULL1);
3904
                                end
3905
                                else
3906
                                        next_state(IFETCH);
3907
                        end
3908 21 robfinch
`ifdef SUPPORT_6309
3909
        `LW_ACCE:       begin
3910
                                acce <= dat;
3911
                                radr <= radr + 2'd1;
3912
                                if (isRTI) begin
3913
                                        $display("loaded acce=%h from %h", dat, radr);
3914
                                        ssp <= ssp + 2'd1;
3915
                                        next_state(PULL1);
3916
                                end
3917
                                else if (isPULU) begin
3918
                                        usp <= usp + 2'd1;
3919
                                        next_state(PULL1);
3920
                                end
3921
                                else if (isPULS) begin
3922
                                        ssp <= ssp + 2'd1;
3923
                                        next_state(PULL1);
3924
                                end
3925
                                else
3926
                                        next_state(IFETCH);
3927
                        end
3928
        `LW_ACCF:       begin
3929
                                accf <= dat;
3930
                                radr <= radr + 2'd1;
3931
                                if (isRTI) begin
3932
                                        $display("loaded accf=%h from %h", dat, radr);
3933
                                        ssp <= ssp + 2'd1;
3934
                                        next_state(PULL1);
3935
                                end
3936
                                else if (isPULU) begin
3937
                                        usp <= usp + 2'd1;
3938
                                        next_state(PULL1);
3939
                                end
3940
                                else if (isPULS) begin
3941
                                        ssp <= ssp + 2'd1;
3942
                                        next_state(PULL1);
3943
                                end
3944
                                else
3945
                                        next_state(IFETCH);
3946
                        end
3947
`endif
3948 2 robfinch
        `LW_DPR:        begin
3949
                                dpr <= dat;
3950
                                radr <= radr + 2'd1;
3951
                                if (isRTI) begin
3952
                                        $display("loaded dpr=%h from %h", dat, radr);
3953
                                        ssp <= ssp + 2'd1;
3954
                                        next_state(PULL1);
3955
                                end
3956
                                else if (isPULU) begin
3957
                                        usp <= usp + 2'd1;
3958
                                        next_state(PULL1);
3959
                                end
3960
                                else if (isPULS) begin
3961
                                        ssp <= ssp + 2'd1;
3962
                                        next_state(PULL1);
3963
                                end
3964
                                else
3965
                                        next_state(IFETCH);
3966
                        end
3967
        `LW_XH: begin
3968
                                load_what <= `LW_XL;
3969
                                next_state(LOAD1);
3970
                                xr[`HIBYTE] <= dat;
3971
                                radr <= radr + 2'd1;
3972
                                if (isRTI) begin
3973
                                        $display("loaded XH=%h from %h", dat, radr);
3974
                                        ssp <= ssp + 2'd1;
3975
                                end
3976
                                else if (isPULU) begin
3977
                                        usp <= usp + 2'd1;
3978
                                end
3979
                                else if (isPULS) begin
3980
                                        ssp <= ssp + 2'd1;
3981
                                end
3982
                        end
3983
        `LW_XL: begin
3984
                                xr[`LOBYTE] <= dat;
3985
                                radr <= radr + 2'd1;
3986
                                if (isRTI) begin
3987
                                        $display("loaded XL=%h from %h", dat, radr);
3988
                                        ssp <= ssp + 2'd1;
3989
                                        next_state(PULL1);
3990
                                end
3991
                                else if (isPULU) begin
3992
                                        usp <= usp + 2'd1;
3993
                                        next_state(PULL1);
3994
                                end
3995
                                else if (isPULS) begin
3996
                                        ssp <= ssp + 2'd1;
3997
                                        next_state(PULL1);
3998
                                end
3999
                                else
4000
                                        next_state(IFETCH);
4001
                        end
4002
        `LW_YH:
4003
                        begin
4004
                                load_what <= `LW_YL;
4005
                                next_state(LOAD1);
4006
                                yr[`HIBYTE] <= dat;
4007
                                radr <= radr + 2'd1;
4008
                                if (isRTI) begin
4009
                                        $display("loadded YH=%h", dat);
4010
                                        ssp <= ssp + 2'd1;
4011
                                end
4012
                                else if (isPULU) begin
4013
                                        usp <= usp + 2'd1;
4014
                                end
4015
                                else if (isPULS) begin
4016
                                        ssp <= ssp + 2'd1;
4017
                                end
4018
                        end
4019
        `LW_YL: begin
4020
                                yr[`LOBYTE] <= dat;
4021
                                radr <= radr + 2'd1;
4022
                                if (isRTI) begin
4023
                                        $display("loadded YL=%h", dat);
4024
                                        ssp <= ssp + 2'd1;
4025
                                        next_state(PULL1);
4026
                                end
4027
                                else if (isPULU) begin
4028
                                        usp <= usp + 2'd1;
4029
                                        next_state(PULL1);
4030
                                end
4031
                                else if (isPULS) begin
4032
                                        ssp <= ssp + 2'd1;
4033
                                        next_state(PULL1);
4034
                                end
4035
                                else
4036
                                        next_state(IFETCH);
4037
                        end
4038
        `LW_USPH:       begin
4039
                                load_what <= `LW_USPL;
4040
                                next_state(LOAD1);
4041
                                usp[`HIBYTE] <= dat;
4042
                                radr <= radr + 2'd1;
4043
                                if (isRTI) begin
4044
                                        $display("loadded USPH=%h", dat);
4045
                                        ssp <= ssp + 2'd1;
4046
                                end
4047
                                else if (isPULS) begin
4048
                                        ssp <= ssp + 2'd1;
4049
                                end
4050
                        end
4051
        `LW_USPL:       begin
4052
                                usp[`LOBYTE] <= dat;
4053
                                radr <= radr + 2'd1;
4054
                                if (isRTI) begin
4055
                                        $display("loadded USPL=%h", dat);
4056
                                        ssp <= ssp + 2'd1;
4057
                                        next_state(PULL1);
4058
                                end
4059
                                else if (isPULS) begin
4060
                                        ssp <= ssp + 2'd1;
4061
                                        next_state(PULL1);
4062
                                end
4063
                                else
4064
                                        next_state(IFETCH);
4065
                        end
4066
        `LW_SSPH:       begin
4067
                                load_what <= `LW_SSPL;
4068
                                next_state(LOAD1);
4069
                                ssp[`HIBYTE] <= dat;
4070
                                radr <= radr + 2'd1;
4071
                                if (isRTI) begin
4072
                                        ssp <= ssp + 2'd1;
4073
                                end
4074
                                else if (isPULU) begin
4075
                                        usp <= usp + 2'd1;
4076
                                end
4077
                        end
4078
        `LW_SSPL:       begin
4079
                                ssp[`LOBYTE] <= dat;
4080
                                radr <= radr + 2'd1;
4081
                                if (isRTI) begin
4082
                                        ssp <= ssp + 2'd1;
4083
                                        next_state(PULL1);
4084
                                end
4085
                                else if (isPULU) begin
4086
                                        usp <= usp + 2'd1;
4087
                                        next_state(PULL1);
4088
                                end
4089
                                else
4090
                                        next_state(IFETCH);
4091
                        end
4092
        `LW_PCL:        begin
4093
                                pc[`LOBYTE] <= dat;
4094
                                radr <= radr + 2'd1;
4095 11 robfinch
                                // If loading from the vector table in bank zero, force pc[23:16]=0
4096
                                if (radr[`BYTE3]=={BPB{1'b0}} && radr[`BYTE2]=={BPB{1'b1}} && radr[7:4]==4'hF)
4097
                                        pc[`BYTE3] <= {BPB{1'b0}};
4098 15 robfinch
                                if (isRTI|isRTS|isRTF|isPULS) begin
4099 2 robfinch
                                        $display("loadded PCL=%h", dat);
4100
                                        ssp <= ssp + 2'd1;
4101
                                end
4102
                                else if (isPULU)
4103
                                        usp <= usp + 2'd1;
4104
                                next_state(IFETCH);
4105
                        end
4106
        `LW_PCH:        begin
4107
                                pc[`HIBYTE] <= dat;
4108
                                load_what <= `LW_PCL;
4109
                                radr <= radr + 2'd1;
4110 15 robfinch
                                if (isRTI|isRTS|isRTF|isPULS) begin
4111 2 robfinch
                                        $display("loadded PCH=%h", dat);
4112
                                        ssp <= ssp + 2'd1;
4113
                                end
4114
                                else if (isPULU)
4115
                                        usp <= usp + 2'd1;
4116
                                next_state(LOAD1);
4117
                        end
4118
        `LW_PC2316:     begin
4119
                                pc[`BYTE3] <= dat;
4120
                                load_what <= `LW_PCH;
4121
                                radr <= radr + 16'd1;
4122
                                if (isRTI|isRTF|isPULS)
4123
                                        ssp <= ssp + 16'd1;
4124
                                else if (isPULU)
4125
                                        usp <= usp + 16'd1;
4126
                                next_state(LOAD1);
4127
                        end
4128
        `LW_IAL:
4129
                        begin
4130
                                ia[`LOBYTE] <= dat;
4131
                                res[`LOBYTE] <= dat;
4132
                                radr <= {ia[`BYTE3],ia[`HIBYTE],dat};
4133
                                wadr <= {ia[`BYTE3],ia[`HIBYTE],dat};
4134
`ifdef SUPPORT_DBL_IND
4135
                                if (isDblIndirect) begin
4136
          load_what <= `LW_IAH;
4137
          next_state(LOAD1);
4138
          isDblIndirect <= `FALSE;
4139
                                end
4140
                                else
4141
`endif
4142
                                begin
4143
          load_what <= load_what2;
4144
          if (isOuterIndexed)
4145
              next_state(OUTER_INDEXING);
4146
          else begin
4147
              if (isLEA)
4148
                  next_state(IFETCH);
4149
              else if (isStore)
4150
                  next_state(STORE1);
4151
              else
4152
                  next_state(LOAD1);
4153
          end
4154
                                end
4155
                        end
4156
        `LW_IAH:
4157
                        begin
4158
                                ia[`HIBYTE] <= dat;
4159
                                res[`HIBYTE] <= dat;
4160
                                load_what <= `LW_IAL;
4161
                                radr <= radr + 2'd1;
4162
                                next_state(LOAD1);
4163
                        end
4164
        `LW_IA2316:
4165
                        begin
4166
                                ia[`BYTE3] <= dat;
4167
                                load_what <= `LW_IAH;
4168
                                radr <= radr + 32'd1;
4169
                                next_state(LOAD1);
4170
                        end
4171
        endcase
4172
end
4173
endtask
4174
 
4175
endmodule
4176
 
4177
// ============================================================================
4178
// Cache Memories
4179
// ============================================================================
4180
module rf6809_icachemem(wclk, wce, wr, wa, i, rclk, rce, pc, insn);
4181
input wclk;
4182
input wce;
4183
input wr;
4184
input [11:0] wa;
4185
input [BPB*16-1:0] i;
4186
input rclk;
4187
input rce;
4188
input [11:0] pc;
4189
output [`HEXBYTE] insn;
4190
reg [`HEXBYTE] insn;
4191
 
4192
integer n;
4193
reg [BPB*16-1:0] mem [0:255];
4194
reg [11:0] rpc,rpcp16;
4195
initial begin
4196
        for (n = 0; n < 256; n = n + 1)
4197
                mem[n] = {16{`NOP}};
4198
end
4199
 
4200
always_ff @(posedge wclk)
4201
        if (wce & wr) mem[wa[11:4]] <= i;
4202
 
4203
always_ff @(posedge rclk)
4204
        if (rce) rpc <= pc;
4205
always_ff @(posedge rclk)
4206
        if (rce) rpcp16 <= pc + 5'd16;
4207
wire [BPB*16-1:0] insn0 = mem[rpc[11:4]];
4208
wire [BPB*16-1:0] insn1 = mem[rpcp16[11:4]];
4209
always_comb
4210
        insn = {insn1,insn0} >> ({4'h0,rpc[3:0]} * BPB);
4211
 
4212
endmodule
4213
 
4214
module rf6809_itagmem(wclk, wce, wr, wa, invalidate, rclk, rce, pc, hit0, hit1);
4215
input wclk;
4216
input wce;
4217
input wr;
4218
input [`TRPBYTE] wa;
4219
input invalidate;
4220
input rclk;
4221
input rce;
4222
input [`TRPBYTE] pc;
4223
output hit0;
4224
output hit1;
4225
 
4226
integer n;
4227
reg [BPB*3-1:12] mem [0:255];
4228
reg [0:255] tvalid = 256'd0;
4229
reg [`TRPBYTE] rpc,rpcp16;
4230
wire [BPB*3-1:11] tag0,tag1;
4231
initial begin
4232
        for (n = 0; n < 256; n = n + 1)
4233
                mem[n] = {BPB*2{1'b0}};
4234
end
4235
 
4236
always_ff @(posedge wclk)
4237
        if (wce & wr) mem[wa[11:4]] <= wa[BPB*3-1:12];
4238
always_ff @(posedge wclk)
4239
        if (invalidate) tvalid <= 256'd0;
4240
        else if (wce & wr) tvalid[wa[11:4]] <= 1'b1;
4241
always_ff @(posedge rclk)
4242
        if (rce) rpc <= pc;
4243
always_ff @(posedge rclk)
4244
        if (rce) rpcp16 <= pc + 5'd16;
4245
assign tag0 = {mem[rpc[11:4]],tvalid[rpc[11:4]]};
4246
assign tag1 = {mem[rpcp16[11:4]],tvalid[rpcp16[11:4]]};
4247
 
4248
assign hit0 = tag0 == {rpc[BPB*3-1:12],1'b1};
4249 12 robfinch
// Consider a hit on port 1 if the instruction will not span onto it.
4250
assign hit1 = tag1 == {rpcp16[BPB*3-1:12],1'b1} || rpc[3:0] < 4'h9;
4251 2 robfinch
 
4252
endmodule
4253
 

powered by: WebSVN 2.1.0

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