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

Subversion Repositories rf6809

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

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
parameter STORE2 = 6'd10;
55
parameter OUTER_INDEXING = 6'd11;
56
parameter OUTER_INDEXING2 = 6'd12;
57
parameter ICACHE1 = 6'd31;
58
parameter ICACHE2 = 6'd32;
59
parameter ICACHE3 = 6'd33;
60
parameter ICACHE4 = 6'd34;
61
parameter ICACHE5 = 6'd35;
62
parameter ICACHE6 = 6'd36;
63
parameter ICACHE7 = 6'd37;
64
parameter ICACHE8 = 6'd38;
65
parameter ICACHE9 = 6'd39;
66
parameter IBUF1 = 6'd40;
67
parameter IBUF2 = 6'd41;
68
parameter IBUF3 = 6'd42;
69
parameter IBUF4 = 6'd43;
70
parameter IBUF5 = 6'd44;
71
parameter IBUF6 = 6'd45;
72
input [5:0] id;
73
input rst_i;
74
input clk_i;
75
input halt_i;
76
input nmi_i;
77
input irq_i;
78
input firq_i;
79
input [`TRPBYTE] vec_i;
80
output reg ba_o;
81
output reg bs_o;
82
output lic_o;
83
input tsc_i;
84
input rty_i;
85
output reg [1:0] bte_o;
86
output reg [2:0] cti_o;
87
output reg [5:0] bl_o;
88
output reg cyc_o;
89
output reg stb_o;
90
output reg we_o;
91
output reg lock_o;
92
input ack_i;
93
input aack_i;
94
input [3:0] atag_i;
95
output reg [`TRPBYTE] adr_o;
96
input [`LOBYTE] dat_i;
97
output reg [`LOBYTE] dat_o;
98
output [5:0] state;
99
 
100
reg [5:0] state;
101
reg [5:0] load_what,store_what,load_what2;
102
reg [`TRPBYTE] pc;
103
wire [`TRPBYTE] pcp2 = pc + 4'd2;
104
wire [`TRPBYTE] pcp16 = pc + 5'd16;
105
wire [`HEXBYTE] insn;
106
wire icacheOn = 1'b1;
107
reg [`TRPBYTE] ibufadr, icwa;
108
reg [191:0] ibuf;
109
wire ibufhit = ibufadr==pc;
110
reg natMd,firqMd;
111
reg md32;
112
wire [`DBLBYTE] mask = 24'hFFFFFF;
113
reg [1:0] ipg;
114
reg isFar;
115
reg isOuterIndexed;
116
reg [`HEXBYTE] ir;
117
`ifdef EIGHTBIT
118
wire [9:0] ir12 = {ipg,ir[`LOBYTE]};
119
`endif
120
`ifdef TWELVEBIT
121
wire [`LOBYTE] ir12 = ir[`LOBYTE];
122
`endif
123
reg [`LOBYTE] dpr;              // direct page register
124
reg [`DBLBYTE] usppg;   // user stack pointer page
125
wire [`LOBYTE] ndxbyte;
126
reg cf,vf,zf,nf,hf,ef;
127
wire [`LOBYTE] cfx8 = cf;
128
wire [`DBLBYTE] cfx24 = {23'b0,cf};
129
reg im,firqim;
130
reg sync_state,wait_state;
131
wire [`LOBYTE] ccr = {ef,firqim,hf,im,nf,zf,vf,cf};
132
reg [`LOBYTE] acca,accb;
133
reg [`DBLBYTE] accd;
134
reg [`DBLBYTE] xr,yr,usp,ssp;
135
wire [`DBLBYTE] prod = acca * accb;
136
reg [`DBLBYTE] vect;
137
reg [`DBLBYTEP1] res;
138
reg [`LOBYTEP1] res12;
139
wire res12n = res12[BPBM1];
140
wire res12z = res12[`LOBYTE]==12'h000;
141
wire res12c = res12[bitsPerByte];
142
wire res24n = res[BPBX2M1];
143
wire res24z = res[`DBLBYTE]==24'h000000;
144
wire res24c = res[BPB*2];
145
reg [`TRPBYTE] ia;
146
reg ic_invalidate;
147
reg first_ifetch;
148
reg tsc_latched;
149
wire tsc = tsc_i|tsc_latched;
150
reg [`LOBYTE] chkpoint;
151
reg [15:0] icgot;
152
reg [23:0] btocnt;
153
reg bto;                                                        // bus timed out
154
 
155
reg [`DBLBYTE] a,b;
156
wire [`LOBYTE] b12 = b[`LOBYTE];
157
reg [`TRPBYTE] radr,wadr;
158
reg [`DBLBYTE] wdat;
159
 
160
reg nmi1,nmi_edge;
161
reg nmi_armed;
162
 
163
reg isStore;
164
reg isPULU,isPULS;
165
reg isPSHS,isPSHU;
166
reg isRTS,isRTI,isRTF;
167
reg isLEA;
168
reg isRMW;
169
 
170
// Data input path multiplexing
171 12 robfinch
reg [bitsPerByte-1:0] dati;
172 2 robfinch
always_comb
173
        dati = dat_i;
174
 
175
// Evaluate the branch conditional
176
reg takb;
177
always_comb
178
        case(ir12)
179
        `BRA,`LBRA:             takb <= 1'b1;
180
        `BRN,`LBRN:             takb <= 1'b0;
181
        `BHI,`LBHI:             takb <= !cf & !zf;
182
        `BLS,`LBLS:             takb <=  cf | zf;
183
        `BLO,`LBLO:             takb <=  cf;
184
        `BHS,`LBHS:             takb <= !cf;
185
        `BNE,`LBNE:             takb <= !zf;
186
        `BEQ,`LBEQ:             takb <=  zf;
187
        `BMI,`LBMI:             takb <=  nf;
188
        `BPL,`LBPL:             takb <= !nf;
189
        `BVS,`LBVS:             takb <=  vf;
190
        `BVC,`LBVC:             takb <= !vf;
191
        `BGT,`LBGT:             takb <= (nf & vf & !zf) | (!nf & !vf & !zf);
192
        `BGE,`LBGE:             takb <= (nf & vf) | (!nf & !vf);
193
        `BLE,`LBLE:             takb <= zf | (nf & !vf) | (!nf & vf);
194
        `BLT,`LBLT:             takb <= (nf & !vf) | (!nf & vf);
195
        default:        takb <= 1'b1;
196
        endcase
197
 
198
// This chunk of code takes care of calculating the number of bytes stacked
199
// by a push or pull operation.
200
//
201
reg [4:0] cnt;
202
always_comb
203
begin
204
        cnt =   (ir[bitsPerByte] ? 5'd1 : 5'd0) +
205
                        (ir[bitsPerByte+1] ? 5'd1 : 5'd0) +
206
                        (ir[bitsPerByte+2] ? 5'd1 : 5'd0) +
207
                        (ir[bitsPerByte+3] ? 5'd1 : 5'd0) +
208
                        (ir[bitsPerByte+4] ? 5'd2 : 5'd0) +
209
                        (ir[bitsPerByte+5] ? 5'd2 : 5'd0) +
210
                        (ir[bitsPerByte+6] ? 5'd2 : 5'd0) +
211 11 robfinch
                        (ir[bitsPerByte+7] ? (isFar ? 5'd3 : 5'd2) : 5'd0)
212 2 robfinch
                        ;
213
//  cnt = 0;
214
//      if (ir[8]) cnt = cnt + 5'd1;    // CC
215
//      if (ir[9]) cnt = cnt + md32 ? 5'd4 : 5'd1;      // A
216
//      if (ir[10]) cnt = cnt + md32 ? 5'd4 : 5'd1;     // B
217
//      if (ir[BPBM1]) cnt = cnt + 5'd1;        // DP
218
//      if (ir[12]) cnt = cnt + md32 ? 5'd4 : 5'd2;     // X
219
//      if (ir[bitsPerByte+1]) cnt = cnt + md32 ? 5'd4 : 5'd2;  // Y
220
//      if (ir[bitsPerByte+2]) cnt = cnt + md32 ? 5'd4 : 5'd2;  // U/S
221
//      if (ir[bitsPerByte+3]) cnt = cnt + 5'd4;        // PC
222
end
223
 
224
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 ||
225
                                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 ||
226
                                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
227
                                ;
228
 
229
wire isIndexed =
230
        ir12[7:4]==4'h6 || ir12[7:4]==4'hA || ir12[7:4]==4'hE ||
231
        ir12==`LEAX_NDX || ir12==`LEAY_NDX || ir12==`LEAS_NDX || ir12==`LEAU_NDX
232
        ;
233
reg isDblIndirect;
234 12 robfinch
wire isIndirect = ndxbyte[bitsPerByte-4] & ndxbyte[bitsPerByte-1];
235 13 robfinch
`ifdef TWELVEBIT
236
always_comb
237
        isOuterIndexed = ndxbyte[bitsPerByte-5] & ndxbyte[bitsPerByte-1];
238
`endif
239
 
240 2 robfinch
assign ndxbyte = ir[`HIBYTE];
241
 
242
// Detect type of interrupt
243
wire isINT = ir12==`INT;
244
wire isRST = vect[3:0]==4'hE;
245
wire isNMI = vect[3:0]==4'hC;
246
wire isSWI = vect[3:0]==4'hA;
247
wire isIRQ = vect[3:0]==4'h8;
248
wire isFIRQ = vect[3:0]==4'h6;
249
wire isSWI2 = vect[3:0]==4'h4;
250
wire isSWI3 = vect[3:0]==4'h2;
251
 
252
wire [`TRPBYTE] far_address = {ir[`HIBYTE],ir[`BYTE3],ir[`BYTE4]};
253
wire [`TRPBYTE] address = {ir[`HIBYTE],ir[`BYTE3]};
254
wire [`TRPBYTE] dp_address = {dpr,ir[`HIBYTE]};
255
wire [`TRPBYTE] ex_address = isFar ? far_address : address;
256
wire [`TRPBYTE] offset12 = {{bitsPerByte{ir[bitsPerByte*3-1]}},ir[`BYTE3]};
257
wire [`TRPBYTE] offset24 = {ir[`BYTE3],ir[`BYTE4]};
258
wire [`TRPBYTE] offset36 = {ir[`BYTE3],ir[`BYTE4],ir[`BYTE5]};
259
 
260
// Choose the indexing register
261
reg [`TRPBYTE] ndxreg;
262
always_comb
263
        if (bitsPerByte==8)
264
                case(ndxbyte[6:5])
265
                2'b00:  ndxreg <= xr;
266
                2'b01:  ndxreg <= yr;
267
                2'b10:  ndxreg <= {usppg,8'h00} + usp;
268
                2'b11:  ndxreg <= ssp;
269
                endcase
270
        else if (bitsPerByte==12)
271
                case(ndxbyte[10:9])
272
                2'b00:  ndxreg <= xr;
273
                2'b01:  ndxreg <= yr;
274
                2'b10:  ndxreg <= {usppg,8'h00} + usp;
275
                2'b11:  ndxreg <= ssp;
276
                endcase
277
 
278
reg [`TRPBYTE] NdxAddr;
279
always_comb
280
        if (bitsPerByte==8)
281
                casez({isOuterIndexed,ndxbyte})
282
                9'b00???????:   NdxAddr <= ndxreg + {{19{ndxbyte[BPB-4]}},ndxbyte[BPB-4:0]};
283
                9'b01???0000:   NdxAddr <= ndxreg;
284
                9'b01???0001:   NdxAddr <= ndxreg;
285
                9'b01???0010:   NdxAddr <= ndxreg - 2'd1;
286
                9'b01???0011:   NdxAddr <= ndxreg - 2'd2;
287
                9'b01???0100:   NdxAddr <= ndxreg;
288
                9'b01???0101:   NdxAddr <= ndxreg + {{BPB*2{accb[BPBM1]}},accb};
289
                9'b01???0110:   NdxAddr <= ndxreg + {{BPB*2{acca[BPBM1]}},acca};
290
                9'b01???1000:   NdxAddr <= ndxreg + offset12;
291
                9'b01???1001:   NdxAddr <= ndxreg + offset24;
292
                9'b01???1010:   NdxAddr <= ndxreg + offset36;
293
                9'b01???1011:   NdxAddr <= ndxreg + {acca,accb};
294
                9'b01???1100:   NdxAddr <= pc + offset12 + 3'd3;
295
                9'b01???1101:   NdxAddr <= pc + offset24 + 3'd4;
296
                9'b01???1110:   NdxAddr <= pc + offset36 + 3'd5;
297
                9'b01??01111:   NdxAddr <= isFar ? offset36 : offset24;
298
                9'b01??11111:   NdxAddr <= offset24;
299
                9'b10???????:   NdxAddr <= {{11{ndxbyte[BPB-4]}},ndxbyte[BPB-4:0]};
300
                9'b11???0000:   NdxAddr <= 24'd0;
301
                9'b11???0001:   NdxAddr <= 24'd0;
302
                9'b11???0010:   NdxAddr <= 24'd0;
303
                9'b11???0011:   NdxAddr <= 24'd0;
304
                9'b11???0100:   NdxAddr <= 24'd0;
305
                9'b11???0101:   NdxAddr <= {{BPB*2{accb[BPBM1]}},accb};
306
                9'b11???0110:   NdxAddr <= {{BPB*2{acca[BPBM1]}},acca};
307
                9'b11???1000:   NdxAddr <= offset12;
308
                9'b11???1001:   NdxAddr <= offset24;
309
                9'b11???1010:   NdxAddr <= offset36;
310
                9'b11???1011:   NdxAddr <= {acca,accb};
311
                9'b11???1100:   NdxAddr <= pc + offset12 + 3'd3;
312
                9'b11???1101:   NdxAddr <= pc + offset24 + 3'd4;
313
                9'b11???1110:   NdxAddr <= pc + offset36 + 3'd5;
314
                9'b11??01111:   NdxAddr <= isFar ? offset36 : offset24;
315
                9'b11??11111:   NdxAddr <= offset24;
316
                default:                NdxAddr <= 24'hFFFFFF;
317
                endcase
318
        else if (bitsPerByte==12)
319
                casez({isOuterIndexed,ndxbyte})
320
                13'b00???????????:      NdxAddr <= ndxreg + {{27{ndxbyte[BPB-4]}},ndxbyte[BPB-4:0]};
321
                13'b01???00000000:      NdxAddr <= ndxreg;
322
                13'b01???00000001:      NdxAddr <= ndxreg;
323
                13'b01???00000010:      NdxAddr <= ndxreg - 2'd1;
324
                13'b01???00010010:      NdxAddr <= ndxreg - 2'd2;
325
                13'b01???00100010:      NdxAddr <= ndxreg - 2'd3;
326
                13'b01???00000011:      NdxAddr <= ndxreg - 2'd2;
327
                13'b01???00000100:      NdxAddr <= ndxreg;
328
                13'b01???00000101:      NdxAddr <= ndxreg + {{BPB*2{accb[BPBM1]}},accb};
329
                13'b01???00000110:      NdxAddr <= ndxreg + {{BPB*2{acca[BPBM1]}},acca};
330
                13'b01???00001000:      NdxAddr <= ndxreg + offset12;
331
                13'b01???00001001:      NdxAddr <= ndxreg + offset24;
332
                13'b01???00001010:      NdxAddr <= ndxreg + offset36;
333
                13'b01???00001011:      NdxAddr <= ndxreg + {acca,accb};
334
                13'b01???00001100:      NdxAddr <= pc + offset12 + 3'd3;
335
                13'b01???00001101:      NdxAddr <= pc + offset24 + 3'd4;
336
                13'b01???00001110:      NdxAddr <= pc + offset36 + 3'd5;
337
                13'b01??000001111:      NdxAddr <= isFar ? offset36 : offset24;
338
                13'b01??100001111:      NdxAddr <= offset24;
339
                13'b01???10000000:      NdxAddr <= 24'd0;
340
                13'b01???10000001:      NdxAddr <= 24'd0;
341
                13'b01???10000010:      NdxAddr <= 24'd0;
342
                13'b01???10000011:      NdxAddr <= 24'd0;
343
                13'b01???10000100:      NdxAddr <= 24'd0;
344
                13'b01???10000101:      NdxAddr <= {{BPB*2{accb[BPBM1]}},accb};
345
                13'b01???10000110:      NdxAddr <= {{BPB*2{acca[BPBM1]}},acca};
346
                13'b01???10001000:      NdxAddr <= offset12;
347
                13'b01???10001001:      NdxAddr <= offset24;
348 15 robfinch
                13'b01???10001010:      NdxAddr <= offset36;
349 2 robfinch
                13'b01???10001011:      NdxAddr <= {acca,accb};
350
                13'b01???10001100:      NdxAddr <= pc + offset12 + 3'd3;
351
                13'b01???10001101:      NdxAddr <= pc + offset24 + 3'd4;
352
                13'b01???10001110:      NdxAddr <= pc + offset36 + 3'd5;
353
                13'b01??010001111:      NdxAddr <= isFar ? offset36 : offset24;
354
                13'b01??110001111:      NdxAddr <= offset24;
355
                13'b10???????????:      NdxAddr <= {{15{ndxbyte[BPB-4]}},ndxbyte[BPB-4:0]};
356
                13'b11???00000000:      NdxAddr <= 24'd0;
357
                13'b11???00000001:      NdxAddr <= 24'd0;
358
                13'b11???00000010:      NdxAddr <= 24'd0;
359
                13'b11???00000011:      NdxAddr <= 24'd0;
360
                13'b11???00000100:      NdxAddr <= 24'd0;
361
                13'b11???00000101:      NdxAddr <= {{BPB*2{accb[BPBM1]}},accb};
362
                13'b11???00000110:      NdxAddr <= {{BPB*2{acca[BPBM1]}},acca};
363
                13'b11???00001000:      NdxAddr <= offset12;
364
                13'b11???00001001:      NdxAddr <= offset24;
365
                13'b11???00001010:      NdxAddr <= offset36;
366
                13'b11???00001011:      NdxAddr <= {acca,accb};
367
                13'b11???00001100:      NdxAddr <= pc + offset12 + 3'd3;
368
                13'b11???00001101:      NdxAddr <= pc + offset24 + 3'd4;
369
                13'b11???00001110:      NdxAddr <= pc + offset36 + 3'd5;
370
                13'b11??000001111:      NdxAddr <= isFar ? offset36 : offset24;
371
                13'b11??000011111:      NdxAddr <= offset24;
372
                default:                NdxAddr <= 24'hFFFFFF;
373
                endcase
374
 
375
// Compute instruction length depending on indexing byte
376
reg [2:0] insnsz;
377
always_comb
378
        if (bitsPerByte==8)
379
                casez(ndxbyte)
380
                8'b0???????:    insnsz <= 4'h2;
381
                8'b1??00000:    insnsz <= 4'h2;
382
                8'b1??00001:    insnsz <= 4'h2;
383
                8'b1??00010:    insnsz <= 4'h2;
384
                8'b1??00011:    insnsz <= 4'h2;
385
                8'b1??00100:    insnsz <= 4'h2;
386
                8'b1??00101:    insnsz <= 4'h2;
387
                8'b1??00110:    insnsz <= 4'h2;
388
                8'b1??01000:    insnsz <= 4'h3;
389
                8'b1??01001:    insnsz <= 4'h4;
390
                8'b1??01010:    insnsz <= 4'h5;
391
                8'b1??01011:    insnsz <= 4'h2;
392
                8'b1??01100:    insnsz <= 4'h3;
393
                8'b1??01101:    insnsz <= 4'h4;
394
                8'b1??01110:    insnsz <= 4'h5;
395
                8'b1??01111:    insnsz <= isFar ? 4'h5 : 4'h4;
396
                8'b1??11111:    insnsz <= 4'h4;
397
                default:        insnsz <= 4'h2;
398
                endcase
399
        else if (bitsPerByte==12)
400
                casez(ndxbyte)
401
                12'b0???????????:       insnsz <= 4'h2;
402
                12'b1???00000000:       insnsz <= 4'h2;
403
                12'b1???00000001:       insnsz <= 4'h2;
404
                12'b1???00000010:       insnsz <= 4'h2;
405
                12'b1???00000011:       insnsz <= 4'h2;
406
                12'b1???00000100:       insnsz <= 4'h2;
407
                12'b1???00000101:       insnsz <= 4'h2;
408
                12'b1???00000110:       insnsz <= 4'h2;
409
                12'b1???00001000:       insnsz <= 4'h3;
410
                12'b1???00001001:       insnsz <= 4'h4;
411
                12'b1???00001010:       insnsz <= 4'h5;
412
                12'b1???00001011:       insnsz <= 4'h2;
413
                12'b1???00001100:       insnsz <= 4'h3;
414
                12'b1???00001101:       insnsz <= 4'h4;
415
                12'b1???00001110:       insnsz <= 4'h5;
416
                12'b1??000001111:       insnsz <= isFar ? 4'h5 : 4'h4;
417 15 robfinch
                12'b1??100001111:       insnsz <= 4'h4;
418 2 robfinch
                default:        insnsz <= 4'h2;
419
                endcase
420
 
421
// Source registers for transfer or exchange instructions.
422
reg [`DBLBYTE] src1,src2;
423
always_comb
424
        case(ir[bitsPerByte+7:bitsPerByte+4])
425
        4'b0000:        src1 <= {acca[`LOBYTE],accb[`LOBYTE]};
426
        4'b0001:        src1 <= xr;
427
        4'b0010:        src1 <= yr;
428
        4'b0011:        src1 <= usp;
429
        4'b0100:        src1 <= ssp;
430
        4'b0101:        src1 <= pcp2;
431
        4'b1000:        src1 <= acca[`LOBYTE];
432
        4'b1001:        src1 <= accb[`LOBYTE];
433
        4'b1010:        src1 <= ccr;
434
        4'b1011:        src1 <= dpr;
435
        4'b1100:        src1 <= usppg;
436
        4'b1101:        src1 <= 24'h0000;
437
        4'b1110:        src1 <= 24'h0000;
438
        4'b1111:        src1 <= 24'h0000;
439
        default:        src1 <= 24'h0000;
440
        endcase
441
always_comb
442
        case(ir[bitsPerByte+3:bitsPerByte])
443
        4'b0000:        src2 <= {acca[`LOBYTE],accb[`LOBYTE]};
444
        4'b0001:        src2 <= xr;
445
        4'b0010:        src2 <= yr;
446
        4'b0011:        src2 <= usp;
447
        4'b0100:        src2 <= ssp;
448
        4'b0101:        src2 <= pcp2;
449
        4'b1000:        src2 <= acca[`LOBYTE];
450
        4'b1001:        src2 <= accb[`LOBYTE];
451
        4'b1010:        src2 <= ccr;
452
        4'b1011:        src2 <= dpr;
453
        4'b1100:        src2 <= usppg;
454
        4'b1101:        src2 <= 24'h0000;
455
        4'b1110:        src2 <= 24'h0000;
456
        4'b1111:        src2 <= 24'h0000;
457
        default:        src2 <= 24'h0000;
458
        endcase
459
 
460
wire isAcca     =       ir12==`NEGA || ir12==`COMA || ir12==`LSRA || ir12==`RORA || ir12==`ASRA || ir12==`ASLA ||
461
                                ir12==`ROLA || ir12==`DECA || ir12==`INCA || ir12==`TSTA || ir12==`CLRA ||
462
                                ir12==`SUBA_IMM || ir12==`CMPA_IMM || ir12==`SBCA_IMM || ir12==`ANDA_IMM || ir12==`BITA_IMM ||
463
                                ir12==`LDA_IMM || ir12==`EORA_IMM || ir12==`ADCA_IMM || ir12==`ORA_IMM || ir12==`ADDA_IMM ||
464
                                ir12==`SUBA_DP || ir12==`CMPA_DP || ir12==`SBCA_DP || ir12==`ANDA_DP || ir12==`BITA_DP ||
465
                                ir12==`LDA_DP || ir12==`EORA_DP || ir12==`ADCA_DP || ir12==`ORA_DP || ir12==`ADDA_DP ||
466
                                ir12==`SUBA_NDX || ir12==`CMPA_NDX || ir12==`SBCA_NDX || ir12==`ANDA_NDX || ir12==`BITA_NDX ||
467
                                ir12==`LDA_NDX || ir12==`EORA_NDX || ir12==`ADCA_NDX || ir12==`ORA_NDX || ir12==`ADDA_NDX ||
468
                                ir12==`SUBA_EXT || ir12==`CMPA_EXT || ir12==`SBCA_EXT || ir12==`ANDA_EXT || ir12==`BITA_EXT ||
469
                                ir12==`LDA_EXT || ir12==`EORA_EXT || ir12==`ADCA_EXT || ir12==`ORA_EXT || ir12==`ADDA_EXT
470
                                ;
471
 
472
wire [`DBLBYTE] acc = isAcca ? acca : accb;
473
 
474
wire [`DBLBYTE] sum12 = src1 + src2;
475
 
476
always_ff @(posedge clk_i)
477
if (state==DECODE) begin
478
        isStore <=      ir12==`STA_DP || ir12==`STB_DP || ir12==`STD_DP || ir12==`STX_DP || ir12==`STY_DP || ir12==`STU_DP || ir12==`STS_DP ||
479
                                ir12==`STA_NDX || ir12==`STB_NDX || ir12==`STD_NDX || ir12==`STX_NDX || ir12==`STY_NDX || ir12==`STU_NDX || ir12==`STS_NDX ||
480
                                ir12==`STA_EXT || ir12==`STB_EXT || ir12==`STD_EXT || ir12==`STX_EXT || ir12==`STY_EXT || ir12==`STU_EXT || ir12==`STS_EXT
481
                                ;
482
        isPULU <= ir12==`PULU;
483
        isPULS <= ir12==`PULS;
484
        isPSHS <= ir12==`PSHS;
485
        isPSHU <= ir12==`PSHU;
486
        isRTI <= ir12==`RTI;
487
        isRTS <= ir12==`RTS;
488
        isRTF <= ir12==`RTF;
489
        isLEA <= ir12==`LEAX_NDX || ir12==`LEAY_NDX || ir12==`LEAU_NDX || ir12==`LEAS_NDX;
490
        isRMW <= isRMW1;
491
end
492
 
493
wire hit0, hit1;
494
wire ihit = hit0 & hit1;
495
reg rhit0;
496
 
497
assign lic_o =  (state==CALC && !isRMW) ||
498
                                (state==DECODE && (
499
                                        ir12==`NOP || ir12==`ORCC || ir12==`ANDCC || ir12==`DAA || ir12==`LDMD || ir12==`TFR || ir12==`EXG ||
500
                                        ir12==`NEGA || ir12==`COMA || ir12==`LSRA || ir12==`RORA || ir12==`ASRA || ir12==`ROLA || ir12==`DECA || ir12==`INCA || ir12==`TSTA || ir12==`CLRA ||
501
                                        ir12==`NEGB || ir12==`COMB || ir12==`LSRB || ir12==`RORB || ir12==`ASRB || ir12==`ROLB || ir12==`DECB || ir12==`INCB || ir12==`TSTB || ir12==`CLRB ||
502
                                        ir12==`ASLD || ir12==`TSTD || //ir12==`ADDR ||
503
                                        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 ||
504
                                        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 ||
505
                                        ir12==`ANDD_IMM || ir12==`ADDD_IMM || ir12==`ADCD_IMM || ir12==`SUBD_IMM || ir12==`SBCD_IMM || ir12==`LDD_IMM ||
506
                                        ir12==`LDQ_IMM || ir12==`CMPD_IMM || ir12==`CMPX_IMM || ir12==`CMPY_IMM || ir12==`CMPU_IMM || ir12==`CMPS_IMM ||
507
                                        ir12==`BEQ || ir12==`BNE || ir12==`BMI || ir12==`BPL || ir12==`BVS || ir12==`BVC || ir12==`BRA || ir12==`BRN ||
508
                                        ir12==`BHI || ir12==`BLS || ir12==`BHS || ir12==`BLO ||
509
                                        ir12==`BGT || ir12==`BGE || ir12==`BLT || ir12==`BLE ||
510
                                        ir12==`LBEQ || ir12==`LBNE || ir12==`LBMI || ir12==`LBPL || ir12==`LBVS || ir12==`LBVC || ir12==`LBRA || ir12==`LBRN ||
511
                                        ir12==`LBHI || ir12==`LBLS || ir12==`LBHS || ir12==`LBLO ||
512
                                        ir12==`LBGT || ir12==`LBGE || ir12==`LBLT || ir12==`LBLE
513
                                        )
514
                                ) ||
515
                                (state==STORE2 && (
516
                                        (store_what==`SW_ACCQ3124 && wadr[1:0]==2'b00) ||
517
                                        (store_what==`SW_ACCQ70) ||
518
                                        (store_what==`SW_ACCA && !(isINT || isPSHS || isPSHU)) ||
519
                                        (store_what==`SW_ACCB && !(isINT || isPSHS || isPSHU)) ||
520
                                        (store_what==`SW_ACCDH && wadr[1:0]!=2'b11) ||
521
                                        (store_what==`SW_ACCDL) ||
522
                                        (store_what==`SW_X3124 && wadr[1:0]==2'b00 && !(isINT || isPSHS || isPSHU)) ||
523
                                        (store_what==`SW_XL && !(isINT || isPSHS || isPSHU)) ||
524
                                        (store_what==`SW_YL && !(isINT || isPSHS || isPSHU)) ||
525
                                        (store_what==`SW_USPL && !(isINT || isPSHS || isPSHU)) ||
526
                                        (store_what==`SW_SSPL && !(isINT || isPSHS || isPSHU)) ||
527
                                        (store_what==`SW_PCL && !(isINT || isPSHS || isPSHU) && !(ir12==`JSR_NDX && isIndirect)) ||
528
                                        (store_what==`SW_ACCA70 && !(isINT || isPSHS || isPSHU)) ||
529
                                        (store_what==`SW_ACCB70 && !(isINT || isPSHS || isPSHU))
530
                                )) ||
531
                                (state==PUSH2 && ir[`HIBYTE]==12'h000 && !isINT) ||
532
                                (state==PULL1 && ir[`HIBYTE]==12'h000) ||
533
                                (state==OUTER_INDEXING2 && isLEA) ||
534
                                (state==LOAD2 &&
535
                                        (load_what==`LW_ACCA && !(isRTI || isPULU || isPULS)) ||
536
                                        (load_what==`LW_ACCB && !(isRTI || isPULU || isPULS)) ||
537
                                        (load_what==`LW_DPR && !(isRTI || isPULU || isPULS)) ||
538
                                        (load_what==`LW_XL && !(isRTI || isPULU || isPULS)) ||
539
                                        (load_what==`LW_YL && !(isRTI || isPULU || isPULS)) ||
540
                                        (load_what==`LW_USPL && !(isRTI || isPULU || isPULS)) ||
541
                                        (load_what==`LW_SSPL && !(isRTI || isPULU || isPULS)) ||
542
                                        (load_what==`LW_PCL) ||
543
                                        (load_what==`LW_IAL && !isOuterIndexed && isLEA) ||
544
                                        (load_what==`LW_IA3124 && radr[1:0]==2'b00 && !isOuterIndexed && isLEA)
545
                                )
546
                                ;
547
 
548
wire lock_bus = load_what==`LW_XH || load_what==`LW_YH || load_what==`LW_USPH || load_what==`LW_SSPH ||
549
                                load_what==`LW_PCH || load_what==`LW_BH || load_what==`LW_IAH || load_what==`LW_PC3124 ||
550
                                load_what==`LW_IA3124 || load_what==`LW_B3124 ||
551
                                load_what==`LW_X3124 || load_what==`LW_Y3124 || load_what==`LW_USP3124 || load_what==`LW_SSP3124 ||
552
                                isRMW ||
553
                                store_what==`SW_ACCDH || store_what==`SW_XH || store_what==`SW_YH || store_what==`SW_USPH || store_what==`SW_SSPH ||
554
                                store_what==`SW_PCH || store_what==`SW_PC3124 || store_what==`SW_ACCQ3124 ||
555
                                store_what==`SW_X3124 || store_what==`SW_Y3124 || store_what==`SW_USP3124 || store_what==`SW_SSP3124
556
                                ;
557
 
558
wire isPrefix = ir12==`PG2 || ir12==`PG3 || ir12==`OUTER;
559
 
560
reg rty;
561
reg [5:0] waitcnt;
562
reg [3:0] iccnt;
563
reg [bitsPerByte-1:0] icbuf [0:15];
564
reg [bitsPerByte*16-1:0] icbuf2;
565
reg [15:0] outstanding; // Outstanding async read cycles.
566
integer n4;
567
 
568
rf6809_icachemem u1
569
(
570
        .wclk(clk_i),
571
        .wce(1'b1),
572
        .wr(state==ICACHE6),
573
        .wa(icwa[11:0]),
574
        .i(icbuf2),
575
        .rclk(~clk_i),
576
        .rce(1'b1),
577
        .pc(pc[11:0]),
578
        .insn(insn)
579
);
580
 
581
rf6809_itagmem u2
582
(
583
        .wclk(clk_i),
584
        .wce(1'b1),
585
        .wr(state==ICACHE6),
586
        .wa(icwa[`TRPBYTE]),
587
        .invalidate(ic_invalidate),
588
        .rclk(~clk_i),
589
        .rce(1'b1),
590
        .pc(pc),
591
        .hit0(hit0),
592
        .hit1(hit1)
593
);
594
 
595
// For asynchronous reads,
596
// The read response might come back in any order (the packets could loop
597
// around in the network.
598
// We need to buffer and reorder the response correctly.
599
 
600
integer n3;
601
always_ff @(posedge clk_i)
602
if (rst_i) begin
603
        icgot <= 16'h0;
604
        for (n3 = 0; n3 < 16; n3 = n3 + 1)
605
                icbuf[n3] <= {bitsPerByte{1'b0}};
606
end
607
else begin
608
        if (state==ICACHE1)
609
                icgot <= 16'h0;
610
`ifdef SUPPORT_AREAD
611
        if (aack_i) begin
612
                icgot[atag_i] <= 1'b1;
613
                icbuf[atag_i] <= dati;
614
        end
615
`else
616
        if (ack_i) begin
617
                icgot[adr_o[3:0]] <= 1'b1;
618
                icbuf[adr_o[3:0]] <= dati;
619
        end
620
`endif
621
end
622
 
623
genvar g;
624
generate begin : gIcin
625
for (g = 0; g < 16; g = g + 1)
626
        always_comb
627
                icbuf2[(g+1)*bitsPerByte-1:g*bitsPerByte] <= icbuf[g];
628
end
629
endgenerate
630
 
631
// Bus timeout counter
632
always_ff @(posedge clk_i)
633
if (rst_i) begin
634
        btocnt <= 24'd0;
635
end
636
else begin
637
        if (cyc_o & stb_o)
638
                btocnt <= btocnt + 2'd1;
639
        else
640
                btocnt <= 24'd0;
641
end
642
always_comb
643
        bto = btocnt >= 24'd10000;
644
 
645
// Count  milliseconds
646
// Based on a count determined by the clock frequency
647
// 40MHz is assumed.
648
reg [23:0] ns_count;    // The counter to get to 1ms
649
reg [35:0] ms_count;    // Count of number of milliseconds
650
 
651
always_ff @(posedge clk_i)
652
if (rst_i) begin
653
        ns_count <= 16'd0;
654
        ms_count <= 36'd0;
655
end
656
else begin
657
        ns_count <= ns_count + 2'd1;
658
        if (ns_count>=24'd40000) begin
659
                ns_count <= 24'h0;
660
                ms_count <= ms_count + 2'd1;
661
        end
662
end
663
 
664
`ifdef SUPPORT_CHECKPOINT
665
always_ff @(posedge clk_i)
666
if (rst_i)
667
        chkpoint <= 12'h000;
668
else begin
669
        if (ns_count==16'd40000) begin
670
                if (ms_count[9:0]==10'h3FF)
671
                        chkpoint <= 12'hFFF;
672
        end
673
        if (state==STORE1 && (wadr=={{BPB*3-8{1'b1}},8'hE1}))
674
                chkpoint <= 12'h000;
675
end
676
`endif
677
 
678
always_ff @(posedge clk_i)
679
        tsc_latched <= tsc_i;
680
 
681
always_ff @(posedge clk_i)
682
        nmi1 <= nmi_i;
683
always_ff @(posedge clk_i)
684
`ifdef SUPPORT_CHECKPOINT
685
        if (ms_count[9:0]==10'h3FF && chkpoint!=12'h000)
686
                nmi_edge <= 1'b1;
687
        else
688
`endif
689
        if (nmi_i & !nmi1)
690
                nmi_edge <= 1'b1;
691
        else if (state==DECODE && ir12==`INT)
692
                nmi_edge <= 1'b0;
693
 
694
reg [9:0] rst_cnt;
695
 
696
always @(posedge clk_i)
697
if (rst_i) begin
698
        wb_nack();
699
        rty <= `FALSE;
700
        rst_cnt <= {id,4'd0};
701
        next_state(RESET);
702
        sync_state <= `FALSE;
703
        wait_state <= `FALSE;
704
        md32 <= `FALSE;
705
        ipg <= 2'b00;
706
        isFar <= `FALSE;
707 13 robfinch
`ifdef EIGHTBIT
708 2 robfinch
        isOuterIndexed <= `FALSE;
709 13 robfinch
`endif
710 2 robfinch
        dpr <= 12'h000;
711
        ibufadr <= {BPB*3{1'b0}};
712
//      pc <= 24'hFFFFFE;
713
        pc <= {{BPB*3-1{1'b1}},1'b0};   // FF...FE
714
        ir <= {4{`NOP}};
715
        ibuf <= {4{`NOP}};
716
        im <= 1'b1;
717
        firqim <= 1'b1;
718
        nmi_armed <= `FALSE;
719
        ic_invalidate <= `TRUE;
720
        first_ifetch <= `TRUE;
721
        acca <= 12'h0;
722
        accb <= 12'h0;
723
        accd <= 24'h0;
724
        xr <= 24'h0;
725
        yr <= 24'h0;
726 15 robfinch
        usppg <= 16'h0;
727 2 robfinch
        usp <= 24'h0;
728
        ssp <= 24'h0;
729
        if (halt_i) begin
730
                ba_o <= 1'b1;
731
                bs_o <= 1'b1;
732
        end
733
        else begin
734
                ba_o <= 1'b0;
735
                bs_o <= 1'b0;
736
        end
737
        outstanding <= 16'h0;
738
        iccnt <= 4'h0;
739
end
740
else begin
741
 
742
// Release any bus lock during the last state of an instruction.
743
if (lic_o && ack_i && (state==STORE2 || state==LOAD2))
744
        lock_o <= 1'b0;
745
 
746
case(state)
747
RESET:
748
        if (rst_cnt==10'd0) begin
749
                ic_invalidate <= `FALSE;
750
                ba_o <= 1'b0;
751
                bs_o <= 1'b0;
752
                vect <= `RST_VECT;
753
                radr <= `RST_VECT;
754
                load_what <= `LW_PCH;
755
                next_state(LOAD1);
756
        end
757
        else
758
                rst_cnt <= rst_cnt - 2'd1;
759
 
760
IFETCH:
761
        begin
762
                tIfetch();
763
                tWriteback();
764
        end
765
DECODE: tDecode();
766
LOAD1:  tLoad1();
767
LOAD2:  tLoad2();
768
CALC:           tExecute();
769
STORE1: tStore1();
770
STORE2: tStore2();
771
 
772
// ============================================================================
773
// ============================================================================
774
PUSH1:
775
        begin
776
                next_state(PUSH2);
777
                if (isINT | isPSHS) begin
778
                        wadr <= (ssp - cnt);
779
                        ssp <= (ssp - cnt);
780
                end
781
                else begin      // PSHU
782 15 robfinch
                        wadr <= ({usppg,{bitsPerByte{1'b0}}} + usp - cnt);
783 2 robfinch
                        usp <= (usp - cnt);
784
                end
785
        end
786
PUSH2:
787
        begin
788
                next_state(STORE1);
789
                if (ir[bitsPerByte]) begin
790
                        store_what <= `SW_CCR;
791
                        ir[bitsPerByte] <= 1'b0;
792
                end
793
                else if (ir[bitsPerByte+1]) begin
794
                        store_what <= `SW_ACCA;
795
                        ir[bitsPerByte+1] <= 1'b0;
796
                end
797
                else if (ir[bitsPerByte+2]) begin
798
                        store_what <= `SW_ACCB;
799
                        ir[bitsPerByte+2] <= 1'b0;
800
                end
801
                else if (ir[bitsPerByte+3]) begin
802
                        store_what <= `SW_DPR;
803
                        ir[bitsPerByte+3] <= 1'b0;
804
                end
805
                else if (ir[bitsPerByte+4]) begin
806
                        store_what <= `SW_XH;
807
                        ir[bitsPerByte+4] <= 1'b0;
808
                end
809
                else if (ir[bitsPerByte+5]) begin
810
                        store_what <= `SW_YH;
811
                        ir[bitsPerByte+5] <= 1'b0;
812
                end
813
                else if (ir[bitsPerByte+6]) begin
814
                        if (isINT | isPSHS)
815
                                store_what <= `SW_USPH;
816
                        else
817
                                store_what <= `SW_SSPH;
818
                        ir[bitsPerByte+6] <= 1'b0;
819
                end
820
                else if (ir[bitsPerByte+7]) begin
821
                        store_what <= isFar ? `SW_PC2316 : `SW_PCH;
822
                        ir[bitsPerByte+7] <= 1'b0;
823
                end
824
                else begin
825
                        if (isINT) begin
826
                                radr <= vect;
827
                                if (vec_i != 24'h0) begin
828
                                        $display("vector: %h", vec_i);
829
                                        pc <= vec_i;
830
                                        next_state(IFETCH);
831
                                end
832
                                else begin
833
                                        pc[`BYTE3] <= 8'h00;
834
                                        load_what <= `LW_PCH;
835
                                        next_state(LOAD1);
836
                                end
837
                        end
838
                        else
839
                                next_state(IFETCH);
840
                end
841
        end
842
PULL1:
843
        begin
844
                next_state(LOAD1);
845
                if (ir[bitsPerByte]) begin
846
                        load_what <= `LW_CCR;
847
                        ir[bitsPerByte] <= 1'b0;
848
                end
849
                else if (ir[bitsPerByte+1]) begin
850
                        load_what <= `LW_ACCA;
851
                        ir[bitsPerByte+1] <= 1'b0;
852
                end
853
                else if (ir[bitsPerByte+2]) begin
854
                        load_what <= `LW_ACCB;
855
                        ir[bitsPerByte+2] <= 1'b0;
856
                end
857
                else if (ir[bitsPerByte+3]) begin
858
                        load_what <= `LW_DPR;
859
                        ir[bitsPerByte+3] <= 1'b0;
860
                end
861
                else if (ir[bitsPerByte+4]) begin
862
                        load_what <= `LW_XH;
863
                        ir[bitsPerByte+4] <= 1'b0;
864
                end
865
                else if (ir[bitsPerByte+5]) begin
866
                        load_what <= `LW_YH;
867
                        ir[bitsPerByte+5] <= 1'b0;
868
                end
869
                else if (ir[bitsPerByte+6]) begin
870
                        if (ir12==`PULU)
871
                                load_what <= `LW_SSPH;
872
                        else
873
                                load_what <= `LW_USPH;
874
                        ir[bitsPerByte+6] <= 1'b0;
875
                end
876
                else if (ir[bitsPerByte+7]) begin
877
                        load_what <= isFar ? `LW_PC2316 : `LW_PCH;
878
                        ir[bitsPerByte+7] <= 1'b0;
879
                end
880
                else
881
                        next_state(IFETCH);
882
        end
883
 
884
// ----------------------------------------------------------------------------
885
// Outer Indexing Support
886
// ----------------------------------------------------------------------------
887
OUTER_INDEXING:
888
        begin
889
                if (bitsPerByte==8) begin
890 12 robfinch
                        casez(ndxbyte)
891
                        8'b0???????:    radr <= radr + ndxreg;
892
                        8'b1???0000:
893 2 robfinch
                                                        begin
894
                                                                radr <= radr + ndxreg;
895
                                                                case(ndxbyte[6:5])
896
                                                                2'b00:  xr <= (xr + 2'd1);
897
                                                                2'b01:  yr <= (yr + 2'd1);
898
                                                                2'b10:  usp <= (usp + 2'd1);
899
                                                                2'b11:  ssp <= (ssp + 2'd1);
900
                                                                endcase
901
                                                        end
902 12 robfinch
                        8'b1???0001:    begin
903 2 robfinch
                                                                radr <= radr + ndxreg;
904
                                                                case(ndxbyte[6:5])
905
                                                                2'b00:  xr <= (xr + 2'd2);
906
                                                                2'b01:  yr <= (yr + 2'd2);
907
                                                                2'b10:  usp <= (usp + 2'd2);
908
                                                                2'b11:  ssp <= (ssp + 2'd2);
909
                                                                endcase
910
                                                        end
911 12 robfinch
                        8'b1???0010:    radr <= radr + ndxreg;
912
                        8'b1???0011:    radr <= radr + ndxreg;
913
                        8'b1???0100:    radr <= radr + ndxreg;
914
                        8'b1???0101:    radr <= radr + ndxreg;
915
                        8'b1???0110:    radr <= radr + ndxreg;
916
                        8'b1???1000:    radr <= radr + ndxreg;
917
                        8'b1???1001:    radr <= radr + ndxreg;
918
                        8'b1???1010:    radr <= radr + ndxreg;
919
                        8'b1???1011:    radr <= radr + ndxreg;
920 2 robfinch
                        default:        radr <= radr;
921
                        endcase
922
                end
923
                else if (bitsPerByte==12) begin
924 12 robfinch
                        casez(ndxbyte)
925
                        12'b0???????????:       radr <= radr + ndxreg;
926
                        12'b1????0000000:
927 2 robfinch
                                                        begin
928
                                                                radr <= radr + ndxreg;
929
                                                                case(ndxbyte[10:9])
930
                                                                2'b00:  xr <= (xr + 2'd1);
931
                                                                2'b01:  yr <= (yr + 2'd1);
932
                                                                2'b10:  usp <= (usp + 2'd1);
933
                                                                2'b11:  ssp <= (ssp + 2'd1);
934
                                                                endcase
935
                                                        end
936 12 robfinch
                        12'b1????0000001:       begin
937 2 robfinch
                                                                radr <= radr + ndxreg;
938
                                                                case(ndxbyte[10:9])
939
                                                                2'b00:  xr <= (xr + 2'd2);
940
                                                                2'b01:  yr <= (yr + 2'd2);
941
                                                                2'b10:  usp <= (usp + 2'd2);
942
                                                                2'b11:  ssp <= (ssp + 2'd2);
943
                                                                endcase
944
                                                        end
945 12 robfinch
                        12'b1????0000010:       radr <= radr + ndxreg;
946
                        12'b1????0000011:       radr <= radr + ndxreg;
947
                        12'b1????0000100:       radr <= radr + ndxreg;
948
                        12'b1????0000101:       radr <= radr + ndxreg;
949
                        12'b1????0000110:       radr <= radr + ndxreg;
950
                        12'b1????0001000:       radr <= radr + ndxreg;
951
                        12'b1????0001001:       radr <= radr + ndxreg;
952
                        12'b1????0001010:       radr <= radr + ndxreg;
953
                        12'b1????0001011:       radr <= radr + ndxreg;
954 2 robfinch
                        default:        radr <= radr;
955
                        endcase
956
                end
957
                next_state(OUTER_INDEXING2);
958
        end
959
OUTER_INDEXING2:
960
        begin
961
                wadr <= radr;
962
                res <= radr[`DBLBYTE];
963
                load_what <= load_what2;
964
                if (isLEA)
965
                        next_state(IFETCH);
966
                else if (isStore)
967
                        next_state(STORE1);
968
                else
969
                        next_state(LOAD1);
970
        end
971
 
972
// ============================================================================
973
// Cache Control
974
// ============================================================================
975
ICACHE1:
976
        begin
977
                iccnt <= 4'h0;
978
                outstanding <= 16'h0;
979
                if (hit0 & hit1)
980
                        next_state(IFETCH);
981
                else if (!tsc && !ack_i) begin
982
                        rhit0 <= hit0;
983
                        bte_o <= 2'b00;
984
                        cti_o <= 3'b001;
985
                        cyc_o <= 1'b1;
986
                        bl_o <= 6'd15;
987
                        stb_o <= 1'b1;
988
                        we_o <= 1'b0;
989
                        adr_o <= !hit0 ? {pc[bitsPerByte*3-1:4],4'b00} : {pcp16[bitsPerByte*3-1:4],4'b0000};
990
                        dat_o <= 12'd0;
991
                        next_state(ICACHE2);
992
                end
993
        end
994
// If tsc is asserted during an instruction cache fetch, then abort the fetch
995
// cycle, and wait until tsc deactivates.
996
// The instruction cache uses asynchronous reading through the network for
997
// better performance. The read request and the read response are two
998
// separate things.
999
ICACHE2:
1000
`ifdef SUPPORT_AREAD
1001
        if (tsc) begin
1002
                wb_nack();
1003
                next_state(ICACHE3);
1004
        end
1005
        else if (ack_i|rty_i|bto) begin
1006
                stb_o <= 1'b0;
1007
                iccnt <= iccnt + 2'd1;
1008
                next_state(ICACHE4);
1009
                if (iccnt==4'b1110)
1010
                        cti_o <= 3'b111;
1011
                if (iccnt==4'b1111) begin
1012
                        icwa <= adr_o;
1013
                        wb_nack();
1014
                        next_state(ICACHE5);
1015
                end
1016
        end
1017
`else
1018
        if (tsc|rty_i) begin
1019
                wb_nack();
1020
                next_state(ICACHE3);
1021
        end
1022
        else if (ack_i) begin
1023
                stb_o <= 1'b0;
1024
                iccnt <= iccnt + 2'd1;
1025
                next_state(ICACHE4);
1026
                if (iccnt==4'b1110)
1027
                        cti_o <= 3'b111;
1028
                if (iccnt==4'b1111) begin
1029
                        icwa <= adr_o;
1030
                        wb_nack();
1031
                        next_state(ICACHE6);
1032
                end
1033
        end
1034
`endif
1035
 
1036
ICACHE4:
1037
        if (!ack_i) begin
1038
                adr_o[3:0] <= iccnt;
1039
                stb_o <= 1'b1;
1040
                next_state(ICACHE2);
1041
        end
1042
 
1043
ICACHE6:
1044
        next_state(ICACHE1);
1045
 
1046
// The following states to handle outstanding transfers.
1047
// The transfer might retry several times if it has not registered.
1048
`ifdef SUPPORT_AREAD
1049
ICACHE5:
1050
        // Line loaded?
1051
        if (icgot == 16'hFFFF)
1052
                next_state(ICACHE6);
1053
        else begin
1054
                waitcnt <= 6'd20;
1055
                next_state(ICACHE7);
1056
        end
1057
ICACHE7:
1058
        if (waitcnt==6'd0) begin
1059 11 robfinch
                next_state(ICACHE5);
1060 2 robfinch
                adr_o <= icwa;
1061
                for (n4 = 15; n4 >= 0; n4 = n4 - 1)
1062 11 robfinch
                        if (~icgot[n4]) begin// & ~outstanding[n4]) begin
1063 2 robfinch
                                cti_o <= 3'b001;
1064 11 robfinch
                                cyc_o <= `TRUE;
1065
                                stb_o <= `TRUE;
1066 2 robfinch
                                adr_o[3:0] <= n4[3:0];
1067
                                outstanding[n4[3:0]] <= 1'b1;
1068
                                next_state(ICACHE9);
1069
                        end
1070
        end
1071
        else
1072
                waitcnt <= waitcnt - 2'd1;
1073
ICACHE9:
1074
        begin
1075
                if (bto)
1076
                        outstanding <= 16'h0;
1077
                if (aack_i)
1078 11 robfinch
                        outstanding[atag_i] <= 1'b0;
1079 2 robfinch
                if (ack_i|rty_i|bto) begin
1080
                        wb_nack();
1081
                        waitcnt <= 6'd20;
1082
                        next_state(ICACHE7);
1083
                end
1084
        end
1085
`endif
1086
 
1087
// Restart a cache load aborted by the TSC signal. A registered version of the
1088
// hit signal must be used as the cache may be partially updated.
1089
ICACHE3:
1090
        if (!tsc) begin
1091
                bte_o <= 2'b00;
1092
                cti_o <= 3'b001;
1093
                cyc_o <= 1'b1;
1094
                bl_o <= 6'd15;
1095
                stb_o <= 1'b1;
1096
                we_o <= 1'b0;
1097
                adr_o <= !rhit0 ? {pc[bitsPerByte*3-1:4],4'b00} : {pcp16[bitsPerByte*3-1:4],4'b0000};
1098
                dat_o <= 12'd0;
1099
                next_state(ICACHE2);
1100
        end
1101
 
1102
`ifdef SUPPORT_IBUF
1103
IBUF1:
1104
        if (!tsc) begin
1105
                bte_o <= 2'b00;
1106
                cti_o <= 3'b001;
1107
                cyc_o <= 1'b1;
1108
                bl_o <= 6'd2;
1109
                stb_o <= 1'b1;
1110
                we_o <= 1'b0;
1111
                adr_o <= pc[`DBLBYTE];
1112
                dat_o <= 12'd0;
1113
                next_state(IBUF2);
1114
        end
1115
IBUF2:
1116
        if (tsc|rty_i) begin
1117
                wb_nack();
1118
                next_state(IBUF1);
1119
        end
1120
        else if (ack_i) begin
1121
                adr_o <= adr_o + 2'd1;
1122
                ibuf <= dat_i;
1123
                next_state(IBUF3);
1124
        end
1125
IBUF3:
1126
        if (tsc|rty_i) begin
1127
                wb_nack();
1128
                next_state(IBUF1);
1129
        end
1130
        else if (ack_i) begin
1131
                cti_o <= 3'b111;
1132
                adr_o <= adr_o + 2'd1;
1133
                ibuf[`HIBYTE] <= dat_i;
1134
                next_state(IBUF4);
1135
        end
1136
IBUF4:
1137
        if (tsc|rty_i) begin
1138
                wb_nack();
1139
                next_state(IBUF1);
1140
        end
1141
        else if (ack_i) begin
1142
                wb_nack();
1143
                ibuf[`BYTE3] <= dat_i;
1144
                next_state(IBUF5);
1145
        end
1146
IBUF5:
1147
        if (tsc|rty_i) begin
1148
                wb_nack();
1149
                next_state(IBUF1);
1150
        end
1151
        else if (ack_i) begin
1152
                wb_nack();
1153
                ibuf[`BYTE4] <= dat_i;
1154
                next_state(IBUF6);
1155
        end
1156
IBUF6:
1157
        if (tsc|rty_i) begin
1158
                wb_nack();
1159
                next_state(IBUF1);
1160
        end
1161
        else if (ack_i) begin
1162
                wb_nack();
1163
                ibuf[`BYTE5] <= dat_i;
1164
                ibufadr <= pc;
1165
                next_state(IFETCH);
1166
        end
1167
`endif
1168
 
1169
endcase
1170
end
1171
 
1172
// ============================================================================
1173
// ============================================================================
1174
// Supporting Tasks
1175
// ============================================================================
1176
// ============================================================================
1177
 
1178
// ============================================================================
1179
// IFETCH
1180
//
1181
// Fetch instructions.
1182
// ============================================================================
1183
 
1184
task tIfetch;
1185
begin
1186
        if (halt_i) begin
1187
                ba_o <= 1'b1;
1188
                bs_o <= 1'b1;
1189
        end
1190
        else begin
1191
                ba_o <= 1'b0;
1192
                bs_o <= 1'b0;
1193
                next_state(DECODE);
1194
                isFar <= `FALSE;
1195 13 robfinch
`ifdef EIGHTBIT
1196 2 robfinch
                isOuterIndexed <= `FALSE;
1197 13 robfinch
`endif
1198 2 robfinch
                ipg <= 2'b00;
1199 12 robfinch
                ia <= {bitsPerByte*3{1'b0}};
1200 2 robfinch
                res <= 24'd0;
1201
                load_what <= `LW_NOTHING;
1202
                store_what <= `SW_NOTHING;
1203
                if (nmi_edge | firq_i | irq_i)
1204
                        sync_state <= `FALSE;
1205
                if (nmi_edge & nmi_armed) begin
1206
                        bs_o <= 1'b1;
1207
                        ir[`LOBYTE] <= `INT;
1208
                        ipg <= 2'b11;
1209
                        vect <= `NMI_VECT;
1210
                end
1211
                else if (firq_i & !firqim & !sync_state) begin
1212
                        bs_o <= 1'b1;
1213
                        ir[`LOBYTE] <= `INT;
1214
                        ipg <= 2'b11;
1215
                        vect <= `FIRQ_VECT;
1216
                end
1217
                else if (irq_i & !im & !sync_state) begin
1218
                        $display("**************************************");
1219
                        $display("****** Interrupt *********************");
1220
                        $display("**************************************");
1221
                        bs_o <= 1'b1;
1222
                        ir[`LOBYTE] <= `INT;
1223
                        ipg <= 2'b11;
1224
                        vect <= `IRQ_VECT;
1225
                end
1226
                else begin
1227
                        if (sync_state) begin
1228
                                ba_o <= 1'b1;
1229
                                next_state(IFETCH);
1230
                        end
1231
                        else if (icacheOn) begin
1232
                                if (ihit) begin
1233
                                        ir <= insn;
1234
                                end
1235
                                else begin
1236
                                        ipg <= ipg;
1237
                                        isFar <= isFar;
1238 13 robfinch
`ifdef EIGHTBIT
1239 2 robfinch
                                        isOuterIndexed <= isOuterIndexed;
1240 13 robfinch
`endif
1241 2 robfinch
                                        next_state(ICACHE1);
1242
                                end
1243
                        end
1244
`ifdef SUPPORT_IBUF
1245
                        else begin
1246
                                if (ibufhit)
1247
                                        ir <= ibuf;
1248
                                else begin
1249
                                        ipg <= ipg;
1250
                                        isFar <= isFar;
1251 13 robfinch
`ifdef EIGHTBIT
1252 2 robfinch
                                        isOuterIndexed <= isOuterIndexed;
1253 13 robfinch
`endif
1254 2 robfinch
                                        next_state(IBUF1);
1255
                                end
1256
                        end
1257
`endif
1258
                end
1259
        end
1260
end
1261
endtask
1262
 
1263
// ============================================================================
1264
// DECODE
1265
//
1266
// Decode instruction and fetch register file values.
1267
// ============================================================================
1268
 
1269
task tDecode;
1270
begin
1271
        first_ifetch <= `TRUE;
1272
        next_state(IFETCH);             // default: move to IFETCH
1273
        pc <= pc + 2'd1;                // default: increment PC by one
1274
        a <= 24'd0;
1275
        b <= 24'd0;
1276 12 robfinch
        ia <= {bitsPerByte * 3{1'b0}};
1277 2 robfinch
        isDblIndirect <= `FALSE;//ndxbyte[11:4]==8'h8F;
1278
        if (isIndexed) begin
1279
                if (bitsPerByte==8) begin
1280
                        casez(ndxbyte)
1281
                        8'b1??00000:
1282
                                if (!isOuterIndexed)
1283
                                        case(ndxbyte[6:5])
1284
                                        2'b00:  xr <= (xr + 4'd1);
1285
                                        2'b01:  yr <= (yr + 4'd1);
1286
                                        2'b10:  usp <= (usp + 4'd1);
1287
                                        2'b11:  ssp <= (ssp + 4'd1);
1288
                                        endcase
1289
                        8'b1??00001:
1290
                                if (!isOuterIndexed)
1291
                                        case(ndxbyte[6:5])
1292
                                        2'b00:  xr <= (xr + 4'd2);
1293
                                        2'b01:  yr <= (yr + 4'd2);
1294
                                        2'b10:  usp <= (usp + 4'd2);
1295
                                        2'b11:  ssp <= (ssp + 4'd2);
1296
                                        endcase
1297
                        8'b1??00010:
1298
                                case(ndxbyte[6:5])
1299
                                2'b00:  xr <= (xr - 2'd1);
1300
                                2'b01:  yr <= (yr - 2'd1);
1301
                                2'b10:  usp <= (usp - 2'd1);
1302
                                2'b11:  ssp <= (ssp - 2'd1);
1303
                                endcase
1304
                        8'b1??00011:
1305
                                case(ndxbyte[6:5])
1306
                                2'b00:  xr <= (xr - 2'd2);
1307
                                2'b01:  yr <= (yr - 2'd2);
1308
                                2'b10:  usp <= (usp - 2'd2);
1309
                                2'b11:  ssp <= (ssp - 2'd2);
1310
                                endcase
1311
                        endcase
1312
                end
1313
                else if (bitsPerByte==12) begin
1314
                        casez(ndxbyte)
1315
                        12'b1??000000000:
1316 13 robfinch
                                if (!isOuterIndexed && ndxbyte[bitsPerByte-5]==1'b0)
1317 2 robfinch
                                        case(ndxbyte[10:9])
1318
                                        2'b00:  xr <= (xr + 4'd1);
1319
                                        2'b01:  yr <= (yr + 4'd1);
1320
                                        2'b10:  usp <= (usp + 4'd1);
1321
                                        2'b11:  ssp <= (ssp + 4'd1);
1322
                                        endcase
1323
                        12'b1??000000001:
1324 13 robfinch
                                if (!isOuterIndexed && ndxbyte[bitsPerByte-5]==1'b0)
1325 2 robfinch
                                        case(ndxbyte[10:9])
1326
                                        2'b00:  xr <= (xr + 4'd2);
1327
                                        2'b01:  yr <= (yr + 4'd2);
1328
                                        2'b10:  usp <= (usp + 4'd2);
1329
                                        2'b11:  ssp <= (ssp + 4'd2);
1330
                                        endcase
1331 15 robfinch
                        12'b1??0?0000010:
1332 2 robfinch
                                case(ndxbyte[10:9])
1333
                                2'b00:  xr <= (xr - 2'd1);
1334
                                2'b01:  yr <= (yr - 2'd1);
1335
                                2'b10:  usp <= (usp - 2'd1);
1336
                                2'b11:  ssp <= (ssp - 2'd1);
1337
                                endcase
1338 15 robfinch
                        12'b1??0?0000011:
1339 2 robfinch
                                case(ndxbyte[10:9])
1340
                                2'b00:  xr <= (xr - 2'd2);
1341
                                2'b01:  yr <= (yr - 2'd2);
1342
                                2'b10:  usp <= (usp - 2'd2);
1343
                                2'b11:  ssp <= (ssp - 2'd2);
1344
                                endcase
1345
                        endcase
1346
                end
1347
        end
1348
        case(ir12)
1349
        `NOP:   ;
1350
        `SYNC:  sync_state <= `TRUE;
1351
        `ORCC:  begin
1352
                        cf <= cf | ir[bitsPerByte];
1353
                        vf <= vf | ir[bitsPerByte+1];
1354
                        zf <= zf | ir[bitsPerByte+2];
1355
                        nf <= nf | ir[bitsPerByte+3];
1356
                        im <= im | ir[bitsPerByte+4];
1357
                        hf <= hf | ir[bitsPerByte+5];
1358
                        firqim <= firqim | ir[bitsPerByte+6];
1359
                        ef <= ef | ir[bitsPerByte+7];
1360
                        pc <= pcp2;
1361
                        end
1362
        `ANDCC:
1363
                        begin
1364
                        cf <= cf & ir[bitsPerByte];
1365
                        vf <= vf & ir[bitsPerByte+1];
1366
                        zf <= zf & ir[bitsPerByte+2];
1367
                        nf <= nf & ir[bitsPerByte+3];
1368
                        im <= im & ir[bitsPerByte+4];
1369
                        hf <= hf & ir[bitsPerByte+5];
1370
                        firqim <= firqim & ir[bitsPerByte+6];
1371
                        ef <= ef & ir[bitsPerByte+7];
1372
                        pc <= pcp2;
1373
                        end
1374
        `DAA:
1375
                        begin
1376
                                if (hf || acca[3:0] > 4'd9)
1377
                                        res12[3:0] <= acca[3:0] + 4'd6;
1378
                                if (cf || acca[7:4] > 4'd9 || (acca[7:4] > 4'd8 && acca[3:0] > 4'd9))
1379
                                        res12[8:4] <= acca[7:4] + 4'd6;
1380
                        end
1381
        `CWAI:
1382
                        begin
1383
                        cf <= cf & ir[bitsPerByte];
1384
                        vf <= vf & ir[bitsPerByte+1];
1385
                        zf <= zf & ir[bitsPerByte+2];
1386
                        nf <= nf & ir[bitsPerByte+3];
1387
                        im <= im & ir[bitsPerByte+4];
1388
                        hf <= hf & ir[bitsPerByte+5];
1389
                        firqim <= firqim & ir[bitsPerByte+6];
1390
                        ef <= 1'b1;
1391
                        pc <= pc + 2'd2;
1392
                        ir[`HIBYTE] <= -1;
1393
                        isFar <= `TRUE;
1394
                        wait_state <= `TRUE;
1395
                        next_state(PUSH1);
1396
                        end
1397
        `LDMD:  begin
1398
                        natMd <= ir[bitsPerByte];
1399
                        firqMd <= ir[bitsPerByte+1];
1400
                        pc <= pc + 2'd2;
1401
                        end
1402
        `TFR:   pc <= pc + 2'd2;
1403
        `EXG:   pc <= pc + 2'd2;
1404
        `ABX:   res <= xr + accb;
1405
        `SEX: res <= {{bitsPerByte{accb[BPBM1]}},accb[`LOBYTE]};
1406
        `PG2:   begin ipg <= 2'b01; ir <= ir[bitsPerByte*5-1:bitsPerByte]; next_state(DECODE); end
1407
        `PG3:   begin ipg <= 2'b10; ir <= ir[bitsPerByte*5-1:bitsPerByte]; next_state(DECODE); end
1408
        `FAR:   begin isFar <= `TRUE;  ir <= ir[bitsPerByte*5-1:bitsPerByte]; next_state(DECODE); end
1409 13 robfinch
`ifdef EIGHTBIT
1410 2 robfinch
        `OUTER: begin isOuterIndexed <= `TRUE;  ir <= ir[bitsPerByte*5-1:bitsPerByte]; next_state(DECODE); end
1411 13 robfinch
`endif
1412 2 robfinch
        `NEGA,`NEGB:    begin res12 <= -acc[`LOBYTE]; a <= 24'h00; b <= acc; end
1413
        `COMA,`COMB:    begin res12 <= ~acc[`LOBYTE]; end
1414
        `LSRA,`LSRB:    begin res12 <= {acc[0],1'b0,acc[BPBM1:1]}; end
1415
        `RORA,`RORB:    begin res12 <= {acc[0],cf,acc[BPBM1:1]}; end
1416
        `ASRA,`ASRB:    begin res12 <= {acc[0],acc[BPBM1],acc[BPBM1:1]}; end
1417
        `ASLA,`ASLB:    begin res12 <= {acc[`LOBYTE],1'b0}; end
1418
        `ROLA,`ROLB:    begin res12 <= {acc[`LOBYTE],cf}; end
1419
        `DECA,`DECB:    begin res12 <= acc[`LOBYTE] - 2'd1; end
1420
        `INCA,`INCB:    begin res12 <= acc[`LOBYTE] + 2'd1; end
1421
        `TSTA,`TSTB:    begin res12 <= acc[`LOBYTE]; end
1422
        `CLRA,`CLRB:    begin res12 <= 13'h000; end
1423
 
1424
        // Immediate mode instructions
1425
        `SUBA_IMM,`SUBB_IMM,`CMPA_IMM,`CMPB_IMM:
1426
                begin res12 <= acc[`LOBYTE] - ir[`HIBYTE]; pc <= pc + 4'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; end
1427
        `SBCA_IMM,`SBCB_IMM:
1428
                begin res12 <= acc[`LOBYTE] - ir[`HIBYTE] - cf; pc <= pc + 2'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; end
1429
        `ANDA_IMM,`ANDB_IMM,`BITA_IMM,`BITB_IMM:
1430
                begin res12 <= acc[`LOBYTE] & ir[`HIBYTE]; pc <= pc + 2'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; end
1431
        `LDA_IMM,`LDB_IMM:
1432
                begin res12 <= ir[`HIBYTE]; pc <= pc + 2'd2; end
1433
        `EORA_IMM,`EORB_IMM:
1434
                begin res12 <= acc[`LOBYTE] ^ ir[`HIBYTE]; pc <= pc + 2'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; end
1435
        `ADCA_IMM,`ADCB_IMM:
1436
                begin res12 <= acc[`LOBYTE] + ir[`HIBYTE] + cf; pc <= pc + 2'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; end
1437
        `ORA_IMM,`ORB_IMM:
1438
                begin res12 <= acc[`LOBYTE] | ir[`HIBYTE]; pc <= pc + 2'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; end
1439
        `ADDA_IMM,`ADDB_IMM:
1440
                begin res12 <= acc[`LOBYTE] + ir[`HIBYTE]; pc <= pc + 2'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; end
1441
        `ADDD_IMM:
1442
                                begin
1443
                                        res <= {acca[`LOBYTE],accb[`LOBYTE]} + {ir[`HIBYTE],ir[`BYTE3]};
1444
                                        pc <= pc + 2'd3;
1445
                                end
1446
        `SUBD_IMM:
1447
                                begin
1448
                                        res <= {acca[`LOBYTE],accb[`LOBYTE]} - {ir[`HIBYTE],ir[`BYTE3]};
1449
                                        pc <= pc + 2'd3;
1450
                                end
1451
        `LDD_IMM:
1452
                                begin
1453
                                        res <= {ir[`HIBYTE],ir[`BYTE3]};
1454
                                        pc <= pc + 2'd3;
1455
                                end
1456
        `LDX_IMM,`LDY_IMM,`LDU_IMM,`LDS_IMM:
1457
                                begin
1458
                                        res <= {ir[`HIBYTE],ir[`BYTE3]};
1459
                                        pc <= pc + 2'd3;
1460
                                end
1461
 
1462
        `CMPD_IMM:
1463
                                begin
1464
                                        res <= {acca[`LOBYTE],accb[`LOBYTE]} - {ir[`HIBYTE],ir[`BYTE3]};
1465
                                        pc <= pc + 2'd3;
1466
                                        a <= {acca[`LOBYTE],accb[`LOBYTE]};
1467
                                        b <= {ir[`HIBYTE],ir[`BYTE3]};
1468
                                end
1469
        `CMPX_IMM:
1470
                                begin
1471
                                        res <= xr[`DBLBYTE] - {ir[`HIBYTE],ir[`BYTE3]};
1472
                                        pc <= pc + 2'd3;
1473
                                        a <= xr[`DBLBYTE];
1474
                                        b <= {ir[`HIBYTE],ir[`BYTE3]};
1475
                                end
1476
        `CMPY_IMM:
1477
                                begin
1478
                                        res <= yr[`DBLBYTE] - {ir[`HIBYTE],ir[`BYTE3]};
1479
                                        pc <= pc + 2'd3;
1480
                                        a <= yr[`DBLBYTE];
1481
                                        b <= {ir[`HIBYTE],ir[`BYTE3]};
1482
                                end
1483
        `CMPU_IMM:
1484
                                begin
1485
                                        res <= usp[`DBLBYTE] - {ir[`HIBYTE],ir[`BYTE3]};
1486
                                        pc <= pc + 2'd3;
1487
                                        a <= usp[`DBLBYTE];
1488
                                        b <= {ir[`HIBYTE],ir[`BYTE3]};
1489
                                end
1490
        `CMPS_IMM:
1491
                                begin
1492
                                        res <= ssp[`DBLBYTE] - {ir[`HIBYTE],ir[`BYTE3]};
1493
                                        pc <= pc + 2'd3;
1494
                                        a <= ssp[`DBLBYTE];
1495
                                        b <= {ir[`HIBYTE],ir[`BYTE3]};
1496
                                end
1497
 
1498
        // Direct mode instructions
1499
        `NEG_DP,`COM_DP,`LSR_DP,`ROR_DP,`ASR_DP,`ASL_DP,`ROL_DP,`DEC_DP,`INC_DP,`TST_DP:
1500
                begin
1501
                        load_what <= `LW_BL;
1502
                        radr <= dp_address;
1503
                        pc <= pc + 2'd2;
1504
                        next_state(LOAD1);
1505
                end
1506
        `SUBA_DP,`CMPA_DP,`SBCA_DP,`ANDA_DP,`BITA_DP,`LDA_DP,`EORA_DP,`ADCA_DP,`ORA_DP,`ADDA_DP,
1507
        `SUBB_DP,`CMPB_DP,`SBCB_DP,`ANDB_DP,`BITB_DP,`LDB_DP,`EORB_DP,`ADCB_DP,`ORB_DP,`ADDB_DP:
1508
                begin
1509
                        load_what <= `LW_BL;
1510
                        radr <= dp_address;
1511
                        pc <= pc + 2'd2;
1512
                        next_state(LOAD1);
1513
                end
1514
        `SUBD_DP,`ADDD_DP,`LDD_DP,`CMPD_DP,`ADCD_DP,`SBCD_DP:
1515
                begin
1516
                        load_what <= `LW_BH;
1517
                        pc <= pc + 2'd2;
1518
                        radr <= dp_address;
1519
                        next_state(LOAD1);
1520
                end
1521
        `CMPX_DP,`LDX_DP,`LDU_DP,`LDS_DP,
1522
        `CMPY_DP,`CMPS_DP,`CMPU_DP,`LDY_DP:
1523
                begin
1524
                        load_what <= `LW_BH;
1525
                        pc <= pc + 2'd2;
1526
                        radr <= dp_address;
1527
                        next_state(LOAD1);
1528
                end
1529
        `CLR_DP:
1530
                begin
1531
                        dp_store(`SW_RES8);
1532
                        res12 <= 13'h000;
1533
                end
1534
        `STA_DP:        dp_store(`SW_ACCA);
1535
        `STB_DP:        dp_store(`SW_ACCB);
1536
        `STD_DP:        dp_store(`SW_ACCDH);
1537
        `STU_DP:        dp_store(`SW_USPH);
1538
        `STS_DP:        dp_store(`SW_SSPH);
1539
        `STX_DP:        dp_store(`SW_XH);
1540
        `STY_DP:        dp_store(`SW_YH);
1541
        // Indexed mode instructions
1542
        `NEG_NDX,`COM_NDX,`LSR_NDX,`ROR_NDX,`ASR_NDX,`ASL_NDX,`ROL_NDX,`DEC_NDX,`INC_NDX,`TST_NDX:
1543
                begin
1544
                        pc <= pc + insnsz;
1545
                        if (isIndirect) begin
1546
                                load_what <= isFar ? `LW_IA2316 : `LW_IAH;
1547
                                load_what2 <= `LW_BL;
1548
                                radr <= NdxAddr;
1549
                                next_state(LOAD1);
1550
                        end
1551
                        else begin
1552
                                b <= 24'd0;
1553
                                load_what <= `LW_BL;
1554
                                radr <= NdxAddr;
1555
                                next_state(LOAD1);
1556
                        end
1557
                end
1558
        `SUBA_NDX,`CMPA_NDX,`SBCA_NDX,`ANDA_NDX,`BITA_NDX,`LDA_NDX,`EORA_NDX,`ADCA_NDX,`ORA_NDX,`ADDA_NDX,
1559
        `SUBB_NDX,`CMPB_NDX,`SBCB_NDX,`ANDB_NDX,`BITB_NDX,`LDB_NDX,`EORB_NDX,`ADCB_NDX,`ORB_NDX,`ADDB_NDX:
1560
                begin
1561
                        pc <= pc + insnsz;
1562
                        if (isIndirect) begin
1563
                                load_what <= isFar ? `LW_IA2316 : `LW_IAH;
1564
                                load_what2 <= `LW_BL;
1565
                                radr <= NdxAddr;
1566
                                next_state(LOAD1);
1567
                        end
1568
                        else begin
1569
                                b <= 24'd0;
1570
                                load_what <= `LW_BL;
1571
                                radr <= NdxAddr;
1572
                                next_state(LOAD1);
1573
                        end
1574
                end
1575
        `SUBD_NDX,`ADDD_NDX,`LDD_NDX,`CMPD_NDX,`ADCD_NDX,`SBCD_NDX:
1576
                begin
1577
                        pc <= pc + insnsz;
1578
                        if (isIndirect) begin
1579
                                load_what <= isFar ? `LW_IA2316 : `LW_IAH;
1580
                                load_what2 <= `LW_BH;
1581
                                radr <= NdxAddr;
1582
                                next_state(LOAD1);
1583
                        end
1584
                        else begin
1585
                                load_what <= `LW_BH;
1586
                                radr <= NdxAddr;
1587
                                next_state(LOAD1);
1588
                        end
1589
                end
1590
        `CMPX_NDX,`LDX_NDX,`LDU_NDX,`LDS_NDX,
1591
        `CMPY_NDX,`CMPS_NDX,`CMPU_NDX,`LDY_NDX:
1592
                begin
1593
                        pc <= pc + insnsz;
1594
                        if (isIndirect) begin
1595
                                load_what <= isFar ? `LW_IA2316 : `LW_IAH;
1596
                                load_what2 <= `LW_BH;
1597
                                radr <= NdxAddr;
1598
                                next_state(LOAD1);
1599
                        end
1600
                        else begin
1601
                                load_what <= `LW_BH;
1602
                                radr <= NdxAddr;
1603
                                next_state(LOAD1);
1604
                        end
1605
                end
1606
        `CLR_NDX:
1607
                begin
1608
                        res12 <= 13'h000;
1609
                        indexed_store(`SW_RES8);
1610
                end
1611
        `STA_NDX:       indexed_store(`SW_ACCA);
1612
        `STB_NDX:       indexed_store(`SW_ACCB);
1613
        `STD_NDX:       indexed_store(`SW_ACCDH);
1614
        `STU_NDX:       indexed_store(`SW_USPH);
1615
        `STS_NDX:       indexed_store(`SW_SSPH);
1616
        `STX_NDX:       indexed_store(`SW_XH);
1617
        `STY_NDX:       indexed_store(`SW_YH);
1618
 
1619
        // Extended mode instructions
1620
        `NEG_EXT,`COM_EXT,`LSR_EXT,`ROR_EXT,`ASR_EXT,`ASL_EXT,`ROL_EXT,`DEC_EXT,`INC_EXT,`TST_EXT:
1621
                begin
1622
                        load_what <= `LW_BL;
1623
                        radr <= ex_address;
1624
                        pc <= pc + (isFar ? 32'd4 : 32'd3);
1625
                        next_state(LOAD1);
1626
                end
1627
        `SUBA_EXT,`CMPA_EXT,`SBCA_EXT,`ANDA_EXT,`BITA_EXT,`LDA_EXT,`EORA_EXT,`ADCA_EXT,`ORA_EXT,`ADDA_EXT,
1628
        `SUBB_EXT,`CMPB_EXT,`SBCB_EXT,`ANDB_EXT,`BITB_EXT,`LDB_EXT,`EORB_EXT,`ADCB_EXT,`ORB_EXT,`ADDB_EXT:
1629
                begin
1630
                        load_what <= `LW_BL;
1631
                        radr <= ex_address;
1632
                        pc <= pc + (isFar ? 32'd4 : 32'd3);
1633
                        next_state(LOAD1);
1634
                end
1635
        `SUBD_EXT,`ADDD_EXT,`LDD_EXT,`CMPD_EXT,`ADCD_EXT,`SBCD_EXT:
1636
                begin
1637
                        load_what <= `LW_BH;
1638
                        radr <= ex_address;
1639
                        pc <= pc + (isFar ? 32'd4 : 32'd3);
1640
                        next_state(LOAD1);
1641
                end
1642
        `CMPX_EXT,`LDX_EXT,`LDU_EXT,`LDS_EXT,
1643
        `CMPY_EXT,`CMPS_EXT,`CMPU_EXT,`LDY_EXT:
1644
                begin
1645
                        load_what <= `LW_BH;
1646
                        radr <= ex_address;
1647
                        pc <= pc + (isFar ? 32'd4 : 32'd3);
1648
                        next_state(LOAD1);
1649
                end
1650
        `CLR_EXT:
1651
                begin
1652
                        ex_store(`SW_RES8);
1653
                        res12 <= 13'h000;
1654
                end
1655
        `STA_EXT:       ex_store(`SW_ACCA);
1656
        `STB_EXT:       ex_store(`SW_ACCB);
1657
        `STD_EXT:       ex_store(`SW_ACCDH);
1658
        `STU_EXT:       ex_store(`SW_USPH);
1659
        `STS_EXT:       ex_store(`SW_SSPH);
1660
        `STX_EXT:       ex_store(`SW_XH);
1661
        `STY_EXT:       ex_store(`SW_YH);
1662
 
1663
        `BSR:
1664
                begin
1665
                        store_what <= `SW_PCH;
1666
                        wadr <= ssp - 2'd2;
1667
                        ssp <= ssp - 2'd2;
1668
                        pc <= pc + 2'd2;
1669
                        next_state(STORE1);
1670
                end
1671
        `LBSR:
1672
                begin
1673
                        store_what <= `SW_PCH;
1674
                        wadr <= ssp - 2'd2;
1675
                        ssp <= ssp - 2'd2;
1676
                        pc <= pc + 2'd3;
1677
                        next_state(STORE1);
1678
                end
1679
        `JSR_DP:
1680
                begin
1681
                        store_what <= `SW_PCH;
1682
                        wadr <= ssp - 2'd2;
1683
                        ssp <= ssp - 2'd2;
1684
                        pc <= pc + 2'd2;
1685
                        next_state(STORE1);
1686
                end
1687
        `JSR_NDX:
1688
                begin
1689 15 robfinch
            if (isFar) begin
1690
                                store_what <= `SW_PC2316;
1691
                    wadr <= ssp - 16'd3;
1692
                    ssp <= ssp - 16'd3;
1693
            end
1694
                        begin
1695
                                store_what <= `SW_PCH;
1696
                                wadr <= ssp - 2'd2;
1697
                                ssp <= ssp - 2'd2;
1698 2 robfinch
                        end
1699
                        pc <= pc + insnsz;
1700
                        next_state(STORE1);
1701
                end
1702
        `JSR_EXT:
1703
                begin
1704
                        begin
1705
                                store_what <= `SW_PCH;
1706
                                wadr <= ssp - 2'd2;
1707
                                ssp <= ssp - 2'd2;
1708
                        end
1709
                        pc <= pc + 2'd3;
1710
                        next_state(STORE1);
1711
                end
1712
        `JSR_FAR:
1713
                begin
1714
                        store_what <= `SW_PC2316;
1715
                        wadr <= ssp - 16'd4;
1716
                        ssp <= ssp - 16'd4;
1717
                        pc <= pc + 32'd4;
1718
                        next_state(STORE1);
1719
                end
1720
        `RTS:
1721
                begin
1722
                        load_what <= `LW_PCH;
1723
                        radr <= ssp;
1724
                        next_state(LOAD1);
1725
                end
1726
        `RTF:
1727
                begin
1728
                        load_what <= `LW_PC2316;
1729
                        radr <= ssp;
1730
                        next_state(LOAD1);
1731
                end
1732
        `JMP_DP:        pc <= dp_address;
1733
        `JMP_EXT:       pc <= address;
1734
        `JMP_FAR:       pc <= far_address;
1735
        `JMP_NDX:
1736
                begin
1737
                        if (isIndirect) begin
1738
                        radr <= NdxAddr;
1739
                            if (isFar)
1740
                                   load_what <= `LW_PC2316;
1741
                            else
1742
                                   load_what <= `LW_PCH;
1743
                                next_state(LOAD1);
1744
                        end
1745
                        else
1746
                                pc <= isFar ? NdxAddr : {pc[`BYTE3],NdxAddr[`DBLBYTE]};
1747
                end
1748
        `LEAX_NDX,`LEAY_NDX,`LEAS_NDX,`LEAU_NDX:
1749
                begin
1750
                        pc <= pc + insnsz;
1751
                        if (isIndirect) begin
1752
                                load_what <= `LW_IAH;
1753
                                radr <= NdxAddr;
1754
                                state <= LOAD1;
1755
                        end
1756
                        else
1757
                                res <= NdxAddr[`DBLBYTE];
1758
                end
1759
        `PSHU,`PSHS:
1760
                begin
1761
                        next_state(PUSH1);
1762
                        pc <= pc + 2'd2;
1763
                end
1764
        `PULS:
1765
                begin
1766
                        radr <= ssp;
1767
                        next_state(PULL1);
1768
                        pc <= pc + 2'd2;
1769
                end
1770
        `PULU:
1771
                begin
1772
                        radr <= {usppg,8'h00} + usp;
1773
                        next_state(PULL1);
1774
                        pc <= pc + 2'd2;
1775
                end
1776
        `BEQ,`BNE,`BMI,`BPL,`BVS,`BVC,`BHI,`BLS,`BHS,`BLO,`BGT,`BGE,`BLT,`BLE,`BRA,`BRN:
1777
                if (takb)
1778
                        pc <= pc + {{24{ir[BPBX2M1]}},ir[`HIBYTE]} + 2'd2;
1779
                else
1780
                        pc <= pc + 2'd2;
1781
        // PC is already incremented by one due to the PG10 prefix.
1782
        `LBEQ,`LBNE,`LBMI,`LBPL,`LBVS,`LBVC,`LBHI,`LBLS,`LBHS,`LBLO,`LBGT,`LBGE,`LBLT,`LBLE,`LBRN:
1783
                if (takb)
1784
                        pc <= pc + {{12{ir[BPB*3-1]}},ir[`HIBYTE],ir[`BYTE3]} + 2'd3;
1785
                else
1786
                        pc <= pc + 2'd3;
1787
        `LBRA:  pc <= pc + {{12{ir[BPB*3-1]}},ir[`HIBYTE],ir[`BYTE3]} + 2'd3;
1788
        `RTI:
1789
                begin
1790
                        load_what <= `LW_CCR;
1791
                        radr <= ssp;
1792
                        isFar <= `TRUE;
1793
                        next_state(LOAD1);
1794
                end
1795
        `SWI:
1796
                begin
1797
                        im <= 1'b1;
1798
                        firqim <= 1'b1;
1799
                        ir[`LOBYTE] <= `INT;
1800
                        ipg <= 2'b11;
1801
                        vect <= `SWI_VECT;
1802
                        next_state(DECODE);
1803
                end
1804
        `SWI2:
1805
                begin
1806
                        ir[`LOBYTE] <= `INT;
1807
                        ipg <= 2'b11;
1808
                        vect <= `SWI2_VECT;
1809
                        next_state(DECODE);
1810
                end
1811
        `SWI3:
1812
                begin
1813
                        ir[`LOBYTE] <= `INT;
1814
                        ipg <= 2'b11;
1815
                        vect <= `SWI3_VECT;
1816
                        next_state(DECODE);
1817
                end
1818
        // If the processor was in the wait state before the interrupt occurred
1819
        // the registers will have already been pushed. All that needs to be
1820
        // done is to vector to the interrupt routine.
1821
        `INT:
1822
                begin
1823
                        if (wait_state) begin
1824
                                wait_state <= `FALSE;
1825
                                if (vec_i != 24'h0) begin
1826
                                    pc <= vec_i;
1827
                                    next_state(IFETCH);
1828
                                end
1829
                                else begin
1830
                                    radr <= vect;
1831
                      load_what <= `LW_PCH;
1832
                                    pc <= 32'hFFFFFFFE;
1833
                                    next_state(LOAD1);
1834
                                end
1835
                        end
1836
                        else begin
1837
                                if (isNMI | isIRQ | isSWI | isSWI2 | isSWI3) begin
1838
                                        ir[`HIBYTE] <= 16'hFFFF;
1839
                                        ef <= 1'b1;
1840
                                end
1841
                                else if (isFIRQ) begin
1842
                                        if (natMd) begin
1843
                                                ef <= firqMd;
1844
                                                ir[`HIBYTE] <= firqMd ? 16'hFFFF : 12'h81;
1845
                                        end
1846
                                        else begin
1847
                                                ir[`HIBYTE] <= 12'h81;
1848
                                                ef <= 1'b0;
1849
                                        end
1850
                                end
1851
                                pc <= pc;
1852
                                isFar <= `TRUE;
1853
                                next_state(PUSH1);
1854
                        end
1855
                end
1856
        default:        ;
1857
        endcase
1858
end
1859
endtask
1860
 
1861
// ============================================================================
1862
// MEMORY LOAD
1863
// ============================================================================
1864
task tLoad1;
1865
begin
1866
`ifdef SUPPORT_DCACHE
1867
        if (unCachedData)
1868
`endif
1869
        case(radr)
1870
        {{BPB*3-8{1'b1}},8'hE0}:        load_tsk({2'b0,id});
1871
        {{BPB*3-8{1'b1}},8'hE1}:        load_tsk(chkpoint);
1872
        {{BPB*3-8{1'b1}},8'hE4}:        load_tsk(12'h0);
1873
        {{BPB*3-8{1'b1}},8'hE5}:        load_tsk(ms_count[35:24]);
1874
        {{BPB*3-8{1'b1}},8'hE6}:        load_tsk(ms_count[23:12]);
1875
        {{BPB*3-8{1'b1}},8'hE7}:        load_tsk(ms_count[11: 0]);
1876
        default:
1877
        if (~ack_i) begin
1878
                lock_o <= lock_bus;
1879
                wb_read(radr);
1880
                if (!tsc)
1881
                        next_state(LOAD2);
1882
        end
1883
`ifdef SUPPORT_DCACHE
1884
        else if (dhit)
1885
                load_tsk(rdat);
1886
        else begin
1887
                retstate <= LOAD1;
1888
                state <= DCACHE1;
1889
        end
1890
`endif
1891
        endcase
1892
end
1893
endtask
1894
 
1895
task tLoad2;
1896
begin
1897
        // On a tri-state condition abort the bus cycle and retry the load.
1898
        if (tsc|rty_i|bto) begin
1899
                wb_nack();
1900
                next_state(LOAD1);
1901
        end
1902
        else if (ack_i) begin
1903
                wb_nack();
1904
                load_tsk(dati);
1905
        end
1906
`ifdef SUPPORT_BERR
1907
        else if (err_i) begin
1908
                lock_o <= 1'b0;
1909
                wb_nack();
1910
                derr_address <= adr_o;
1911
//              intno <= 9'd508;
1912
                state <= BUS_ERROR;
1913
        end
1914
`endif
1915
end
1916
endtask
1917
 
1918
// ============================================================================
1919
// EXECUTE
1920
//
1921
// Perform calculations
1922
// ============================================================================
1923
task tExecute;
1924
begin
1925
        next_state(IFETCH);
1926
        case(ir12)
1927
        `SUBD_DP,`SUBD_NDX,`SUBD_EXT,
1928
        `CMPD_DP,`CMPD_NDX,`CMPD_EXT:
1929
                begin
1930
                    a <= {acca[`LOBYTE],accb[`LOBYTE]};
1931
                        res <= {acca[`LOBYTE],accb[`LOBYTE]} - b[`DBLBYTE];
1932
                end
1933
        `SBCD_DP,`SBCD_NDX,`SBCD_EXT:
1934
                begin
1935
                    a <= {acca[`LOBYTE],accb[`LOBYTE]};
1936
                        res <= {acca[`LOBYTE],accb[`LOBYTE]} - b[`DBLBYTE] - {23'b0,cf};
1937
                end
1938
        `ADDD_DP,`ADDD_NDX,`ADDD_EXT:
1939
                begin
1940
                    a <= {acca[`LOBYTE],accb[`LOBYTE]};
1941
                        res <= {acca[`LOBYTE],accb[`LOBYTE]} + b[`DBLBYTE];
1942
                end
1943
        `ADCD_DP,`ADCD_NDX,`ADCD_EXT:
1944
                begin
1945
                    a <= {acca[`LOBYTE],accb[`LOBYTE]};
1946
                        res <= {acca[`LOBYTE],accb[`LOBYTE]} + b[`DBLBYTE] + {23'b0,cf};
1947
                end
1948
        `LDD_DP,`LDD_NDX,`LDD_EXT:
1949
                res <= b[`DBLBYTE];
1950
 
1951
        `CMPA_DP,`CMPA_NDX,`CMPA_EXT,
1952
        `SUBA_DP,`SUBA_NDX,`SUBA_EXT,
1953
        `CMPB_DP,`CMPB_NDX,`CMPB_EXT,
1954
        `SUBB_DP,`SUBB_NDX,`SUBB_EXT:
1955
                begin
1956
                        a <= acc;
1957
           res12 <= acc[`LOBYTE] - b12;
1958
                        end
1959
 
1960
        `SBCA_DP,`SBCA_NDX,`SBCA_EXT,
1961
        `SBCB_DP,`SBCB_NDX,`SBCB_EXT:
1962
                begin
1963
                    a <= acc;
1964
          res12 <= acc[`LOBYTE] - b12 - cf;
1965
              end
1966
        `BITA_DP,`BITA_NDX,`BITA_EXT,
1967
        `ANDA_DP,`ANDA_NDX,`ANDA_EXT,
1968
        `BITB_DP,`BITB_NDX,`BITB_EXT,
1969
        `ANDB_DP,`ANDB_NDX,`ANDB_EXT:
1970
                                res12 <= acc[`LOBYTE] & b12;
1971
        `LDA_DP,`LDA_NDX,`LDA_EXT,
1972
        `LDB_DP,`LDB_NDX,`LDB_EXT:
1973
                        res12 <= b12;
1974
        `EORA_DP,`EORA_NDX,`EORA_EXT,
1975
        `EORB_DP,`EORB_NDX,`EORB_EXT:
1976
                                res12 <= acc[`LOBYTE] ^ b12;
1977
        `ADCA_DP,`ADCA_NDX,`ADCA_EXT,
1978
        `ADCB_DP,`ADCB_NDX,`ADCB_EXT:
1979
                        begin
1980
                            a <= acc;
1981
                                res12 <= acc[`LOBYTE] + b12 + cf;
1982
                        end
1983
        `ORA_DP,`ORA_NDX,`ORA_EXT,
1984
        `ORB_DP,`ORB_NDX,`ORB_EXT:
1985
                                res12 <= acc[`LOBYTE] | b12;
1986
        `ADDA_DP,`ADDA_NDX,`ADDA_EXT,
1987
        `ADDB_DP,`ADDB_NDX,`ADDB_EXT:
1988
                begin
1989
                    a <= acc;
1990
                      res12 <= acc[`LOBYTE] + b12;
1991
              end
1992
 
1993
        `LDU_DP,`LDS_DP,`LDX_DP,`LDY_DP,
1994
        `LDU_NDX,`LDS_NDX,`LDX_NDX,`LDY_NDX,
1995
        `LDU_EXT,`LDS_EXT,`LDX_EXT,`LDY_EXT:    res <= b[`DBLBYTE];
1996
        `CMPX_DP,`CMPX_NDX,`CMPX_EXT:   begin a <= xr; res <= xr[`DBLBYTE] - b[`DBLBYTE]; end
1997
        `CMPY_DP,`CMPY_NDX,`CMPY_EXT:   begin a <= yr; res <= yr[`DBLBYTE] - b[`DBLBYTE]; end
1998
        `CMPS_DP,`CMPS_NDX,`CMPS_EXT:   begin a <= ssp; res <= ssp[`DBLBYTE] - b[`DBLBYTE]; end
1999
        `CMPU_DP,`CMPU_NDX,`CMPU_EXT:   begin a <= usp; res <= usp[`DBLBYTE] - b[`DBLBYTE]; end
2000
 
2001
        `NEG_DP,`NEG_NDX,`NEG_EXT:      begin res12 <= -b12; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
2002
        `COM_DP,`COM_NDX,`COM_EXT:      begin res12 <= ~b12; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
2003
        `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
2004
        `ROR_DP,`ROR_NDX,`ROR_EXT:      begin res12 <= {b[0],cf,b[BPBM1:1]}; store_what <= `SW_RES8; wadr <= radr; next_state(STORE1); end
2005
        `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
2006
        `ASL_DP,`ASL_NDX,`ASL_EXT:      begin res12 <= {b12,1'b0}; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
2007
        `ROL_DP,`ROL_NDX,`ROL_EXT:      begin res12 <= {b12,cf}; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
2008
        `DEC_DP,`DEC_NDX,`DEC_EXT:      begin res12 <= b12 - 2'd1; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
2009
        `INC_DP,`INC_NDX,`INC_EXT:      begin res12 <= b12 + 2'd1; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
2010
        `TST_DP,`TST_NDX,`TST_EXT:      res12 <= b12;
2011
        /*
2012
        `AIM_DP,`AIM_NDX,`AIM_EXT:      begin res12 <= ir[`HIBYTE] & b12; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
2013
        `OIM_DP,`OIM_NDX,`OIM_EXT:      begin res12 <= ir[`HIBYTE] | b12; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
2014
        `EIM_DP,`EIM_NDX,`OIM_EXT:  begin res12 <= ir[`HIBYTE] ^ b12; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
2015
        `TIM_DP,`TIM_NDX,`TIM_EXT:      begin res12 <= ir[`HIBYTE] & b12; end
2016
        */
2017
        default:        ;
2018
        endcase
2019
end
2020
endtask
2021
 
2022
// ============================================================================
2023
// MEMORY STORE
2024
// ============================================================================
2025
 
2026
task tStore1;
2027
begin
2028
        if (!ack_i) begin
2029
                lock_o <= lock_bus;
2030
`ifdef SUPPORT_CHECKPOINT
2031
                if (wadr=={{BPB*3-8{1'b1}},8'hE1})
2032
                        next_state(IFETCH);
2033
                else
2034
`endif
2035
                begin
2036
                        case(store_what)
2037
                        `SW_ACCDH:      wb_write(wadr,acca[`LOBYTE]);
2038
                        `SW_ACCDL:      wb_write(wadr,accb[`LOBYTE]);
2039
                        `SW_ACCA:       wb_write(wadr,acca[`LOBYTE]);
2040
                        `SW_ACCB:       wb_write(wadr,accb[`LOBYTE]);
2041
                        `SW_DPR:        wb_write(wadr,dpr);
2042
                        `SW_XL: wb_write(wadr,xr[`LOBYTE]);
2043
                        `SW_XH: wb_write(wadr,xr[`HIBYTE]);
2044
                        `SW_YL: wb_write(wadr,yr[`LOBYTE]);
2045
                        `SW_YH: wb_write(wadr,yr[`HIBYTE]);
2046
                        `SW_USPL:       wb_write(wadr,usp[`LOBYTE]);
2047
                        `SW_USPH:       wb_write(wadr,usp[`HIBYTE]);
2048
                        `SW_SSPL:       wb_write(wadr,ssp[`LOBYTE]);
2049
                        `SW_SSPH:       wb_write(wadr,ssp[`HIBYTE]);
2050
                        `SW_PC2316:     wb_write(wadr,pc[`BYTE3]);
2051
                        `SW_PCH:        wb_write(wadr,pc[`HIBYTE]);
2052
                        `SW_PCL:        wb_write(wadr,pc[`LOBYTE]);
2053
                        `SW_CCR:        wb_write(wadr,ccr);
2054
                        `SW_RES8:       wb_write(wadr,res12[`LOBYTE]);
2055
                        `SW_RES16H:     wb_write(wadr,res[`HIBYTE]);
2056
                        `SW_RES16L:     wb_write(wadr,res[`LOBYTE]);
2057
                        `SW_DEF8:       wb_write(wadr,wdat);
2058
                        default:        wb_write(wadr,wdat);
2059
                        endcase
2060
`ifdef SUPPORT_DCACHE
2061
                        radr <= wadr;           // Do a cache read to test the hit
2062
`endif
2063
                        if (!tsc)
2064
                                next_state(STORE2);
2065
                end
2066
        end
2067
end
2068
endtask
2069
 
2070
// Terminal state for stores. Update the data cache if there was a cache hit.
2071
// Clear any previously set lock status
2072
task tStore2;
2073
begin
2074
        // On a tri-state condition abort the bus cycle and retry the store.
2075
        if (tsc|rty_i|bto) begin
2076
                wb_nack();
2077
                next_state(STORE1);
2078
        end
2079
        else if (ack_i) begin
2080
                wb_nack();
2081
                wdat <= dat_o;
2082
                wadr <= wadr + 2'd1;
2083
                next_state(IFETCH);
2084
                case(store_what)
2085
                `SW_CCR:
2086
                        begin
2087
                                if (isINT) begin
2088
                                        im <= 1'b1;
2089
                                        firqim <= 1'b1;
2090
                                end
2091
                                next_state(PUSH2);
2092
                        end
2093
                `SW_ACCA:
2094
                        if (isINT | isPSHS | isPSHU)
2095
                                next_state(PUSH2);
2096
                        else    // STA
2097
                                next_state(IFETCH);
2098
                `SW_ACCB:
2099
                        if (isINT | isPSHS | isPSHU)
2100
                                next_state(PUSH2);
2101
                        else    // STB
2102
                                next_state(IFETCH);
2103
                `SW_ACCDH:
2104
                        begin
2105
                                store_what <= `SW_ACCDL;
2106
                                next_state(STORE1);
2107
                        end
2108
                `SW_ACCDL:      next_state(IFETCH);
2109
                `SW_DPR:        next_state(PUSH2);
2110
                `SW_XH:
2111
                        begin
2112
                                store_what <= `SW_XL;
2113
                                next_state(STORE1);
2114
                        end
2115
                `SW_XL:
2116
                        if (isINT | isPSHS | isPSHU)
2117
                                next_state(PUSH2);
2118
                        else    // STX
2119
                                next_state(IFETCH);
2120
                `SW_YH:
2121
                        begin
2122
                                store_what <= `SW_YL;
2123
                                next_state(STORE1);
2124
                        end
2125
                `SW_YL:
2126
                        if (isINT | isPSHS | isPSHU)
2127
                                next_state(PUSH2);
2128
                        else    // STY
2129
                                next_state(IFETCH);
2130
                `SW_USPH:
2131
                        begin
2132
                                store_what <= `SW_USPL;
2133
                                next_state(STORE1);
2134
                        end
2135
                `SW_USPL:
2136
                        if (isINT | isPSHS | isPSHU)
2137
                                next_state(PUSH2);
2138
                        else    // STU
2139
                                next_state(IFETCH);
2140
                `SW_SSPH:
2141
                        begin
2142
                                store_what <= `SW_SSPL;
2143
                                next_state(STORE1);
2144
                        end
2145
                `SW_SSPL:
2146
                        if (isINT | isPSHS | isPSHU)
2147
                                next_state(PUSH2);
2148
                        else    // STS
2149
                                next_state(IFETCH);
2150
                `SW_PC2316:
2151
                        begin
2152
                                store_what <= `SW_PCH;
2153
                                next_state(STORE1);
2154
                        end
2155
                `SW_PCH:
2156
                        begin
2157
                                store_what <= `SW_PCL;
2158
                                next_state(STORE1);
2159
                        end
2160
                `SW_PCL:
2161
                        if (isINT | isPSHS | isPSHU)
2162
                                next_state(PUSH2);
2163
                        else begin      // JSR
2164
                                next_state(IFETCH);
2165
                                case(ir12)
2166
                                `BSR:           pc <= pc + {{24{ir[BPBX2M1]}},ir[`HIBYTE]};
2167
                                `LBSR:  pc <= pc + {{12{ir[BPB*3-1]}},ir[`HIBYTE],ir[`BYTE3]};
2168
                                `JSR_DP:        pc <= {dpr,ir[`HIBYTE]};
2169
                                `JSR_EXT:       pc <= {pc[`BYTE3],address[`DBLBYTE]};
2170
                                `JSR_FAR:
2171
                                        begin
2172
                                                pc <= far_address;
2173
                                                $display("Loading PC with %h", far_address);
2174
                                        end
2175
                                `JSR_NDX:
2176
                                        begin
2177
                                                if (isIndirect) begin
2178
                                                        radr <= NdxAddr;
2179
                                                        load_what <= isFar ? `LW_PC2316 : `LW_PCH;
2180
                                                        next_state(LOAD1);
2181
                                                end
2182
                                                else
2183
                                                        pc <= isFar ? NdxAddr : {pc[`BYTE3],NdxAddr[`DBLBYTE]};
2184
                                        end
2185
                                endcase
2186
                        end
2187
                endcase
2188
`ifdef SUPPORT_DCACHE
2189
                if (!dhit && write_allocate) begin
2190
                        state <= DCACHE1;
2191
                end
2192
`endif
2193
        end
2194
`ifdef SUPPORT_BERR
2195
        else if (err_i) begin
2196
                lock_o <= 1'b0;
2197
                wb_nack();
2198
                state <= BUS_ERROR;
2199
        end
2200
`endif
2201
end
2202
endtask
2203
 
2204
// ============================================================================
2205
// WRITEBACK
2206
//
2207
// Write results back to the register file and status flags.
2208
// Which registers and flags get updated depend on the instruction.
2209
// ============================================================================
2210
 
2211
task tWriteback;
2212
begin
2213
        if (first_ifetch) begin
2214
                first_ifetch <= `FALSE;
2215
                case(ir12)
2216
                `ABX:   xr <= res;
2217
                `ADDA_IMM,`ADDA_DP,`ADDA_NDX,`ADDA_EXT,
2218
                `ADCA_IMM,`ADCA_DP,`ADCA_NDX,`ADCA_EXT:
2219
                        begin
2220
                                cf <= (a[BPBM1]&b[BPBM1])|(a[BPBM1]&~res12[BPBM1])|(b[BPBM1]&~res12[BPBM1]);
2221
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
2222
                                vf <= (res12[BPBM1] ^ b[BPBM1]) & (1'b1 ^ a[BPBM1] ^ b[BPBM1]);
2223
                                nf <= res12[BPBM1];
2224
                                zf <= res12[`LOBYTE]==12'h000;
2225
                                acca <= res12[`LOBYTE];
2226
                        end
2227
                `ADDB_IMM,`ADDB_DP,`ADDB_NDX,`ADDB_EXT,
2228
                `ADCB_IMM,`ADCB_DP,`ADCB_NDX,`ADCB_EXT:
2229
                        begin
2230
                                cf <= (a[BPBM1]&b[BPBM1])|(a[BPBM1]&~res12[BPBM1])|(b[BPBM1]&~res12[BPBM1]);
2231
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
2232
                                vf <= (res12[BPBM1] ^ b[BPBM1]) & (1'b1 ^ a[BPBM1] ^ b[BPBM1]);
2233
                                nf <= res12[BPBM1];
2234
                                zf <= res12[`LOBYTE]==12'h000;
2235
                                accb <= res12[`LOBYTE];
2236
                        end
2237
                `ADDD_IMM,`ADDD_DP,`ADDD_NDX,`ADDD_EXT:
2238
                        begin
2239
                                cf <= (a[BPBX2M1]&b[BPBX2M1])|(a[BPBX2M1]&~res[BPBX2M1])|(b[BPBX2M1]&~res[BPBX2M1]);
2240
                                vf <= (res[BPBX2M1] ^ b[BPBX2M1]) & (1'b1 ^ a[BPBX2M1] ^ b[BPBX2M1]);
2241
                                nf <= res[BPBX2M1];
2242
                                zf <= res[`DBLBYTE]==24'h000000;
2243
                                acca <= res[`HIBYTE];
2244
                                accb <= res[`LOBYTE];
2245
                        end
2246
                `ANDA_IMM,`ANDA_DP,`ANDA_NDX,`ANDA_EXT:
2247
                        begin
2248
                                nf <= res12n;
2249
                                zf <= res12z;
2250
                                vf <= 1'b0;
2251
                                acca <= res12[`LOBYTE];
2252
                        end
2253
                `ANDB_IMM,`ANDB_DP,`ANDB_NDX,`ANDB_EXT:
2254
                        begin
2255
                                nf <= res12n;
2256
                                zf <= res12z;
2257
                                vf <= 1'b0;
2258
                                accb <= res12[`LOBYTE];
2259
                        end
2260
                `ASLA:
2261
                        begin
2262
                                cf <= res12c;
2263
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
2264
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
2265
                                nf <= res12[BPBM1];
2266
                                zf <= res12[`LOBYTE]==12'h000;
2267
                                acca <= res12[`LOBYTE];
2268
                        end
2269
                `ASLB:
2270
                        begin
2271
                                cf <= res12c;
2272
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
2273
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
2274
                                nf <= res12[BPBM1];
2275
                                zf <= res12[`LOBYTE]==12'h000;
2276
                                accb <= res12[`LOBYTE];
2277
                        end
2278
                `ASL_DP,`ASL_NDX,`ASL_EXT:
2279
                        begin
2280
                                cf <= res12c;
2281
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
2282
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
2283
                                nf <= res12[BPBM1];
2284
                                zf <= res12[`LOBYTE]==12'h000;
2285
                        end
2286
                `ASRA:
2287
                        begin
2288
                                cf <= res12c;
2289
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
2290
                                nf <= res12[BPBM1];
2291
                                zf <= res12[`LOBYTE]==12'h000;
2292
                                acca <= res12[`LOBYTE];
2293
                        end
2294
                `ASRB:
2295
                        begin
2296
                                cf <= res12c;
2297
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
2298
                                nf <= res12[BPBM1];
2299
                                zf <= res12[`LOBYTE]==12'h000;
2300
                                accb <= res12[`LOBYTE];
2301
                        end
2302
                `ASR_DP,`ASR_NDX,`ASR_EXT:
2303
                        begin
2304
                                cf <= res12c;
2305
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
2306
                                nf <= res12[BPBM1];
2307
                                zf <= res12[`LOBYTE]==12'h000;
2308
                        end
2309
                `BITA_IMM,`BITA_DP,`BITA_NDX,`BITA_EXT,
2310
                `BITB_IMM,`BITB_DP,`BITB_NDX,`BITB_EXT:
2311
                        begin
2312
                                vf <= 1'b0;
2313
                                nf <= res12[BPBM1];
2314
                                zf <= res12[`LOBYTE]==12'h000;
2315
                        end
2316
                `CLRA:
2317
                        begin
2318
                                vf <= 1'b0;
2319
                                cf <= 1'b0;
2320
                                nf <= 1'b0;
2321
                                zf <= 1'b1;
2322
                                acca <= 12'h000;
2323
                        end
2324
                `CLRB:
2325
                        begin
2326
                                vf <= 1'b0;
2327
                                cf <= 1'b0;
2328
                                nf <= 1'b0;
2329
                                zf <= 1'b1;
2330
                                accb <= 12'h000;
2331
                        end
2332
                `CLR_DP,`CLR_NDX,`CLR_EXT:
2333
                        begin
2334
                                vf <= 1'b0;
2335
                                cf <= 1'b0;
2336
                                nf <= 1'b0;
2337
                                zf <= 1'b1;
2338
                        end
2339
                `CMPA_IMM,`CMPA_DP,`CMPA_NDX,`CMPA_EXT,
2340
                `CMPB_IMM,`CMPB_DP,`CMPB_NDX,`CMPB_EXT:
2341
                        begin
2342
                                cf <= (~a[BPBM1]&b[BPBM1])|(res12[BPBM1]&~a[BPBM1])|(res12[BPBM1]&b[BPBM1]);
2343
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
2344
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
2345
                                nf <= res12[BPBM1];
2346
                                zf <= res12[`LOBYTE]==12'h000;
2347
                        end
2348
                `CMPD_IMM,`CMPD_DP,`CMPD_NDX,`CMPD_EXT:
2349
                        begin
2350
                                cf <= (~a[BPBX2M1]&b[BPBX2M1])|(res[BPBX2M1]&~a[BPBX2M1])|(res[BPBX2M1]&b[BPBX2M1]);
2351
                                vf <= (1'b1 ^ res[BPBX2M1] ^ b[BPBX2M1]) & (a[BPBX2M1] ^ b[BPBX2M1]);
2352
                                nf <= res[BPBX2M1];
2353
                                zf <= res[`DBLBYTE]==24'h000000;
2354
                        end
2355
                `CMPS_IMM,`CMPS_DP,`CMPS_NDX,`CMPS_EXT,
2356
                `CMPU_IMM,`CMPU_DP,`CMPU_NDX,`CMPU_EXT,
2357
                `CMPX_IMM,`CMPX_DP,`CMPX_NDX,`CMPX_EXT,
2358
                `CMPY_IMM,`CMPY_DP,`CMPY_NDX,`CMPY_EXT:
2359
                        begin
2360
                                cf <= (~a[BPBX2M1]&b[BPBX2M1])|(res[BPBX2M1]&~a[BPBX2M1])|(res[BPBX2M1]&b[BPBX2M1]);
2361
                                vf <= (1'b1 ^ res[BPBX2M1] ^ b[BPBX2M1]) & (a[BPBX2M1] ^ b[BPBX2M1]);
2362
                                nf <= res[BPBX2M1];
2363
                                zf <= res[`DBLBYTE]==24'h000000;
2364
                        end
2365
                `COMA:
2366
                        begin
2367
                                cf <= 1'b1;
2368
                                vf <= 1'b0;
2369
                                nf <= res12n;
2370
                                zf <= res12z;
2371
                                acca <= res12[`LOBYTE];
2372
                        end
2373
                `COMB:
2374
                        begin
2375
                                cf <= 1'b1;
2376
                                vf <= 1'b0;
2377
                                nf <= res12n;
2378
                                zf <= res12z;
2379
                                accb <= res12[`LOBYTE];
2380
                        end
2381
                `COM_DP,`COM_NDX,`COM_EXT:
2382
                        begin
2383
                                cf <= 1'b1;
2384
                                vf <= 1'b0;
2385
                                nf <= res12n;
2386
                                zf <= res12z;
2387
                        end
2388
                `DAA:
2389
                        begin
2390
                                cf <= res12c;
2391
                                zf <= res12z;
2392
                                nf <= res12n;
2393
                                vf <= (res12[BPBM1] ^ b[BPBM1]) & (1'b1 ^ a[BPBM1] ^ b[BPBM1]);
2394
                                acca <= res12[`LOBYTE];
2395
                        end
2396
                `DECA:
2397
                        begin
2398
                                nf <= res12n;
2399
                                zf <= res12z;
2400
                                vf <= res12[BPBM1] != acca[BPBM1];
2401
                                acca <= res12[`LOBYTE];
2402
                        end
2403
                `DECB:
2404
                        begin
2405
                                nf <= res12n;
2406
                                zf <= res12z;
2407
                                vf <= res12[BPBM1] != accb[BPBM1];
2408
                                accb <= res12[`LOBYTE];
2409
                        end
2410
                `DEC_DP,`DEC_NDX,`DEC_EXT:
2411
                        begin
2412
                                nf <= res12n;
2413
                                zf <= res12z;
2414
                                vf <= res12[BPBM1] != b[BPBM1];
2415
                        end
2416
                `EORA_IMM,`EORA_DP,`EORA_NDX,`EORA_EXT,
2417
                `ORA_IMM,`ORA_DP,`ORA_NDX,`ORA_EXT:
2418
                        begin
2419
                                nf <= res12n;
2420
                                zf <= res12z;
2421
                                vf <= 1'b0;
2422
                                acca <= res12[`LOBYTE];
2423
                        end
2424
                `EORB_IMM,`EORB_DP,`EORB_NDX,`EORB_EXT,
2425
                `ORB_IMM,`ORB_DP,`ORB_NDX,`ORB_EXT:
2426
                        begin
2427
                                nf <= res12n;
2428
                                zf <= res12z;
2429
                                vf <= 1'b0;
2430
                                accb <= res12[`LOBYTE];
2431
                        end
2432
                `EXG:
2433
                        begin
2434
                                case(ir[bitsPerByte+3:bitsPerByte])
2435
                                4'b0000:
2436
                                                        begin
2437
                                                                acca <= src1[`HIBYTE];
2438
                                                                accb <= src1[`LOBYTE];
2439
                                                        end
2440
                                4'b0001:        xr <= src1;
2441
                                4'b0010:        yr <= src1;
2442
                                4'b0011:        usp <= src1;
2443
                                4'b0100:        begin ssp <= src1; nmi_armed <= `TRUE; end
2444
                                4'b0101:        pc <= src1[`DBLBYTE];
2445
                                4'b1000:        acca <= src1[`LOBYTE];
2446
                                4'b1001:        accb <= src1[`LOBYTE];
2447
                                4'b1010:
2448
                                        begin
2449
                                                cf <= src1[0];
2450
                                                vf <= src1[1];
2451
                                                zf <= src1[2];
2452
                                                nf <= src1[3];
2453
                                                im <= src1[4];
2454
                                                hf <= src1[5];
2455
                                                firqim <= src1[6];
2456
                                                ef <= src1[7];
2457
                                        end
2458
                                4'b1011:        dpr <= src1[`LOBYTE];
2459
                                4'b1110:        usppg <= src1[`DBLBYTE];
2460
                                4'b1111:        ;
2461
                                default:        ;
2462
                                endcase
2463
                                case(ir[bitsPerByte+7:bitsPerByte+4])
2464
                                4'b0000:
2465
                                                        begin
2466
                                                                acca <= src2[`HIBYTE];
2467
                                                                accb <= src2[`LOBYTE];
2468
                                                        end
2469
                                4'b0001:        xr <= src2;
2470
                                4'b0010:        yr <= src2;
2471
                                4'b0011:        usp <= src2;
2472
                                4'b0100:        begin ssp <= src2; nmi_armed <= `TRUE; end
2473
                                4'b0101:        pc <= src2[`DBLBYTE];
2474
                                4'b1000:        acca <= src2[`LOBYTE];
2475
                                4'b1001:        accb <= src2[`LOBYTE];
2476
                                4'b1010:
2477
                                        begin
2478
                                                cf <= src2[0];
2479
                                                vf <= src2[1];
2480
                                                zf <= src2[2];
2481
                                                nf <= src2[3];
2482
                                                im <= src2[4];
2483
                                                hf <= src2[5];
2484
                                                firqim <= src2[6];
2485
                                                ef <= src2[7];
2486
                                        end
2487
                                4'b1011:        dpr <= src2[`LOBYTE];
2488
                                4'b1110:        usppg <= src2[`DBLBYTE];
2489
                                4'b1111:        ;
2490
                                default:        ;
2491
                                endcase
2492
                        end
2493
                `INCA:
2494
                        begin
2495
                                nf <= res12n;
2496
                                zf <= res12z;
2497
                                vf <= res12[BPBM1] != acca[BPBM1];
2498
                                acca <= res12[`LOBYTE];
2499
                        end
2500
                `INCB:
2501
                        begin
2502
                                nf <= res12n;
2503
                                zf <= res12z;
2504
                                vf <= res12[BPBM1] != accb[BPBM1];
2505
                                accb <= res12[`LOBYTE];
2506
                        end
2507
                `INC_DP,`INC_NDX,`INC_EXT:
2508
                        begin
2509
                                nf <= res12n;
2510
                                zf <= res12z;
2511
                                vf <= res12[BPBM1] != b[BPBM1];
2512
                        end
2513
                `LDA_IMM,`LDA_DP,`LDA_NDX,`LDA_EXT:
2514
                        begin
2515
                                vf <= 1'b0;
2516
                                zf <= res12z;
2517
                                nf <= res12n;
2518
                                acca <= res12[`LOBYTE];
2519
                        end
2520
                `LDB_IMM,`LDB_DP,`LDB_NDX,`LDB_EXT:
2521
                        begin
2522
                                vf <= 1'b0;
2523
                                zf <= res12z;
2524
                                nf <= res12n;
2525
                                accb <= res12[`LOBYTE];
2526
                        end
2527
                `LDD_IMM,`LDD_DP,`LDD_NDX,`LDD_EXT:
2528
                        begin
2529
                                vf <= 1'b0;
2530
                                zf <= res24z;
2531
                                nf <= res24n;
2532
                                acca <= res[`HIBYTE];
2533
                                accb <= res[`LOBYTE];
2534
                        end
2535
                `LDU_IMM,`LDU_DP,`LDU_NDX,`LDU_EXT:
2536
                        begin
2537
                                vf <= 1'b0;
2538
                                zf <= res24z;
2539
                                nf <= res24n;
2540
                                usp <= res[`DBLBYTE];
2541
                        end
2542
                `LDS_IMM,`LDS_DP,`LDS_NDX,`LDS_EXT:
2543
                        begin
2544
                                vf <= 1'b0;
2545
                                zf <= res24z;
2546
                                nf <= res24n;
2547
                                ssp <= res[`DBLBYTE];
2548
                                nmi_armed <= 1'b1;
2549
                        end
2550
                `LDX_IMM,`LDX_DP,`LDX_NDX,`LDX_EXT:
2551
                        begin
2552
                                vf <= 1'b0;
2553
                                zf <= res24z;
2554
                                nf <= res24n;
2555
                                xr <= res[`DBLBYTE];
2556
                        end
2557
                `LDY_IMM,`LDY_DP,`LDY_NDX,`LDY_EXT:
2558
                        begin
2559
                                vf <= 1'b0;
2560
                                zf <= res24z;
2561
                                nf <= res24n;
2562
                                yr <= res[`DBLBYTE];
2563
                        end
2564
                `LEAS_NDX:
2565
                        begin ssp <= res[`DBLBYTE]; nmi_armed <= 1'b1; end
2566
                `LEAU_NDX:
2567
                        usp <= res[`DBLBYTE];
2568
                `LEAX_NDX:
2569
                        begin
2570
                                zf <= res24z;
2571
                                xr <= res[`DBLBYTE];
2572
                        end
2573
                `LEAY_NDX:
2574
                        begin
2575
                                zf <= res24z;
2576
                                yr <= res[`DBLBYTE];
2577
                        end
2578
                `LSRA:
2579
                        begin
2580
                                cf <= res12c;
2581
                                nf <= res12[BPBM1];
2582
                                zf <= res12[`LOBYTE]==12'h000;
2583
                                acca <= res12[`LOBYTE];
2584
                        end
2585
                `LSRB:
2586
                        begin
2587
                                cf <= res12c;
2588
                                nf <= res12[BPBM1];
2589
                                zf <= res12[`LOBYTE]==12'h000;
2590
                                accb <= res12[`LOBYTE];
2591
                        end
2592
                `LSR_DP,`LSR_NDX,`LSR_EXT:
2593
                        begin
2594
                                cf <= res12c;
2595
                                nf <= res12[BPBM1];
2596
                                zf <= res12[`LOBYTE]==12'h000;
2597
                        end
2598
                `MUL:
2599
                        begin
2600
                                cf <= prod[BPBM1];
2601
                                zf <= res24z;
2602
                                acca <= prod[`HIBYTE];
2603
                                accb <= prod[`LOBYTE];
2604
                        end
2605
                `NEGA:
2606
                        begin
2607
                                cf <= (~a[BPBM1]&b[BPBM1])|(res12[BPBM1]&~a[BPBM1])|(res12[BPBM1]&b[BPBM1]);
2608
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
2609
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
2610
                                nf <= res12[BPBM1];
2611
                                zf <= res12[`LOBYTE]==12'h000;
2612
                                acca <= res12[`LOBYTE];
2613
                        end
2614
                `NEGB:
2615
                        begin
2616
                                cf <= (~a[BPBM1]&b[BPBM1])|(res12[BPBM1]&~a[BPBM1])|(res12[BPBM1]&b[BPBM1]);
2617
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
2618
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
2619
                                nf <= res12[BPBM1];
2620
                                zf <= res12[`LOBYTE]==12'h000;
2621
                                accb <= res12[`LOBYTE];
2622
                        end
2623
                `NEG_DP,`NEG_NDX,`NEG_EXT:
2624
                        begin
2625
                                cf <= (~a[BPBM1]&b[BPBM1])|(res12[BPBM1]&~a[BPBM1])|(res12[BPBM1]&b[BPBM1]);
2626
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
2627
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
2628
                                nf <= res12[BPBM1];
2629
                                zf <= res12[`LOBYTE]==12'h000;
2630
                        end
2631
                `ROLA:
2632
                        begin
2633
                                cf <= res12c;
2634
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
2635
                                nf <= res12[BPBM1];
2636
                                zf <= res12[`LOBYTE]==12'h000;
2637
                                acca <= res12[`LOBYTE];
2638
                        end
2639
                `ROLB:
2640
                        begin
2641
                                cf <= res12c;
2642
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
2643
                                nf <= res12[BPBM1];
2644
                                zf <= res12[`LOBYTE]==12'h000;
2645
                                accb <= res12[`LOBYTE];
2646
                        end
2647
                `ROL_DP,`ROL_NDX,`ROL_EXT:
2648
                        begin
2649
                                cf <= res12c;
2650
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
2651
                                nf <= res12[BPBM1];
2652
                                zf <= res12[`LOBYTE]==12'h000;
2653
                        end
2654
                `RORA:
2655
                        begin
2656
                                cf <= res12c;
2657
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
2658
                                nf <= res12[BPBM1];
2659
                                zf <= res12[`LOBYTE]==12'h000;
2660
                                acca <= res12[`LOBYTE];
2661
                        end
2662
                `RORB:
2663
                        begin
2664
                                cf <= res12c;
2665
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
2666
                                nf <= res12[BPBM1];
2667
                                zf <= res12[`LOBYTE]==12'h000;
2668
                                accb <= res12[`LOBYTE];
2669
                        end
2670
                `ROR_DP,`ROR_NDX,`ROR_EXT:
2671
                        begin
2672
                                cf <= res12c;
2673
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
2674
                                nf <= res12[BPBM1];
2675
                                zf <= res12[`LOBYTE]==12'h000;
2676
                        end
2677
                `SBCA_IMM,`SBCA_DP,`SBCA_NDX,`SBCA_EXT:
2678
                        begin
2679
                                cf <= (~a[BPBM1]&b[BPBM1])|(res12[BPBM1]&~a[BPBM1])|(res12[BPBM1]&b[BPBM1]);
2680
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
2681
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
2682
                                nf <= res12[BPBM1];
2683
                                zf <= res12[`LOBYTE]==12'h000;
2684
                                acca <= res12[`LOBYTE];
2685
                        end
2686
                `SBCB_IMM,`SBCB_DP,`SBCB_NDX,`SBCB_EXT:
2687
                        begin
2688
                                cf <= (~a[BPBM1]&b[BPBM1])|(res12[BPBM1]&~a[BPBM1])|(res12[BPBM1]&b[BPBM1]);
2689
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
2690
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
2691
                                nf <= res12[BPBM1];
2692
                                zf <= res12[`LOBYTE]==12'h000;
2693
                                accb <= res12[`LOBYTE];
2694
                        end
2695
                `SEX:
2696
                        begin
2697
                                vf <= 1'b0;
2698
                                nf <= res12n;
2699
                                zf <= res12z;
2700
                                acca <= res12[`LOBYTE];
2701
                        end
2702
                `STA_DP,`STA_NDX,`STA_EXT,
2703
                `STB_DP,`STB_NDX,`STB_EXT:
2704
                        begin
2705
                                vf <= 1'b0;
2706
                                zf <= res12z;
2707
                                nf <= res12n;
2708
                        end
2709
                `STD_DP,`STD_NDX,`STD_EXT,
2710
                `STU_DP,`STU_NDX,`STU_EXT,
2711
                `STX_DP,`STX_NDX,`STX_EXT,
2712
                `STY_DP,`STY_NDX,`STY_EXT:
2713
                        begin
2714
                                vf <= 1'b0;
2715
                                zf <= res24z;
2716
                                nf <= res24n;
2717
                        end
2718
                `TFR:
2719
                        begin
2720
                                case(ir[bitsPerByte+3:bitsPerByte])
2721
                                4'b0000:
2722
                                                        begin
2723
                                                                acca <= src1[`HIBYTE];
2724
                                                                accb <= src1[`LOBYTE];
2725
                                                        end
2726
                                4'b0001:        xr <= src1;
2727
                                4'b0010:        yr <= src1;
2728
                                4'b0011:        usp <= src1;
2729
                                4'b0100:        begin ssp <= src1; nmi_armed <= `TRUE; end
2730
                                4'b0101:        pc <= src1[`DBLBYTE];
2731
                                4'b1000:        acca <= src1[`LOBYTE];
2732
                                4'b1001:        accb <= src1[`LOBYTE];
2733
                                4'b1010:
2734
                                        begin
2735
                                                cf <= src1[0];
2736
                                                vf <= src1[1];
2737
                                                zf <= src1[2];
2738
                                                nf <= src1[3];
2739
                                                im <= src1[4];
2740
                                                hf <= src1[5];
2741
                                                firqim <= src1[6];
2742
                                                ef <= src1[7];
2743
                                        end
2744
                                4'b1011:        dpr <= src1[`LOBYTE];
2745
                                4'b1110:        usppg <= src1[`DBLBYTE];
2746
                                4'b1111:        ;
2747
                                default:        ;
2748
                                endcase
2749
                        end
2750
                `TSTA,`TSTB:
2751
                        begin
2752
                                vf <= 1'b0;
2753
                                nf <= res12n;
2754
                                zf <= res12z;
2755
                        end
2756
                `TSTD:
2757
                        begin
2758
                                vf <= 1'b0;
2759
                                nf <= res24n;
2760
                                zf <= res24z;
2761
                        end
2762
                `TST_DP,`TST_NDX,`TST_EXT:
2763
                        begin
2764
                                vf <= 1'b0;
2765
                                nf <= res12n;
2766
                                zf <= res12z;
2767
                        end
2768
                `SUBA_IMM,`SUBA_DP,`SUBA_NDX,`SUBA_EXT:
2769
                        begin
2770
                                acca <= res12[`LOBYTE];
2771
                                nf <= res12n;
2772
                                zf <= res12z;
2773
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
2774
                                cf <= res12c;
2775
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
2776
                        end
2777
                `SUBB_IMM,`SUBB_DP,`SUBB_NDX,`SUBB_EXT:
2778
                        begin
2779
                                accb <= res12[`LOBYTE];
2780
                                nf <= res12n;
2781
                                zf <= res12z;
2782
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
2783
                                cf <= res12c;
2784
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
2785
                        end
2786
                `SUBD_IMM,`SUBD_DP,`SUBD_NDX,`SUBD_EXT:
2787
                        begin
2788
                                cf <= res24c;
2789
                                vf <= (1'b1 ^ res[BPBX2M1] ^ b[BPBX2M1]) & (a[BPBX2M1] ^ b[BPBX2M1]);
2790
                                nf <= res[BPBX2M1];
2791
                                zf <= res[`DBLBYTE]==24'h000000;
2792
                                acca <= res[`HIBYTE];
2793
                                accb <= res[`LOBYTE];
2794
                        end
2795
                endcase
2796
        end
2797
end
2798
endtask
2799
 
2800
task dp_store;
2801
input [5:0] stw;
2802
begin
2803
        store_what <= stw;
2804
        wadr <= dp_address;
2805
        pc <= pc + 2'd2;
2806
        next_state(STORE1);
2807
end
2808
endtask
2809
 
2810
task indexed_store;
2811
input [5:0] stw;
2812
begin
2813
        store_what <= stw;
2814
        pc <= pc + insnsz;
2815
        if (isIndirect) begin
2816
                load_what <= isFar ? `LW_IA2316 : `LW_IAH;
2817
                radr <= NdxAddr;
2818
                next_state(LOAD1);
2819
        end
2820
        else begin
2821
                wadr <= NdxAddr;
2822
                next_state(STORE1);
2823
        end
2824
end
2825
endtask
2826
 
2827
task ex_store;
2828
input [5:0] stw;
2829
begin
2830
        pc <= pc + (isFar ? 3'd4 : 3'd3);
2831
        store_what <= stw;
2832
        wadr <= ex_address;
2833
        next_state(STORE1);
2834
end
2835
endtask
2836
 
2837
task next_state;
2838
input [5:0] st;
2839
begin
2840
        state <= st;
2841
end
2842
endtask
2843
 
2844
task wb_burst;
2845
input [5:0] len;
2846
input [bitsPerByte*2-1:0] adr;
2847
begin
2848
        if (!tsc) begin
2849
                bte_o <= 2'b00;
2850
                cti_o <= 3'b001;
2851
                bl_o <= len;
2852
                cyc_o <= 1'b1;
2853
                stb_o <= 1'b1;
2854 12 robfinch
                we_o <= 1'b0;
2855 2 robfinch
                adr_o <= adr;
2856
        end
2857
end
2858
endtask
2859
 
2860
task wb_read;
2861
input [`TRPBYTE] adr;
2862
begin
2863
        if (!tsc) begin
2864
                cyc_o <= 1'b1;
2865
                stb_o <= 1'b1;
2866 12 robfinch
                we_o <= 1'b0;
2867 2 robfinch
                adr_o <= adr;
2868
        end
2869
end
2870
endtask
2871
 
2872
task wb_write;
2873
input [`TRPBYTE] adr;
2874
input [`LOBYTE] dat;
2875
begin
2876
        if (!tsc) begin
2877
                cyc_o <= 1'b1;
2878
                stb_o <= 1'b1;
2879
                we_o <= 1'b1;
2880
                adr_o <= adr;
2881
                dat_o <= dat;
2882
        end
2883
end
2884
endtask
2885
 
2886
task wb_nack;
2887
begin
2888
        cti_o <= 3'b000;
2889
        bl_o <= 6'd0;
2890
        cyc_o <= 1'b0;
2891
        stb_o <= 1'b0;
2892
        we_o <= 1'b0;
2893
        adr_o <= 24'd0;
2894
        dat_o <= 12'd0;
2895
end
2896
endtask
2897
 
2898
task load_tsk;
2899
input [`LOBYTE] dat;
2900
begin
2901
        case(load_what)
2902
        `LW_BH:
2903
                        begin
2904
                                radr <= radr + 2'd1;
2905
                                b[`HIBYTE] <= dat;
2906
                                load_what <= `LW_BL;
2907
                                next_state(LOAD1);
2908
                        end
2909
        `LW_BL:
2910
                        begin
2911
                                // Don't increment address here for the benefit of the memory
2912
                                // operate instructions which set wadr=radr in CALC.
2913
                                b[`LOBYTE] <= dat;
2914
                                next_state(CALC);
2915
                        end
2916
        `LW_CCR:        begin
2917
                                next_state(PULL1);
2918
                                radr <= radr + 2'd1;
2919
                                cf <= dat[0];
2920
                                vf <= dat[1];
2921
                                zf <= dat[2];
2922
                                nf <= dat[3];
2923
                                im <= dat[4];
2924
                                hf <= dat[5];
2925
                                firqim <= dat[6];
2926
                                ef <= dat[7];
2927
                                if (isRTI) begin
2928
                                        $display("loaded ccr=%b", dat);
2929
                                        ir[`HIBYTE] <= dat[7] ? 12'hFE : 12'h80;
2930
                                        ssp <= ssp + 2'd1;
2931
                                end
2932
                                else if (isPULS)
2933
                                        ssp <= ssp + 2'd1;
2934
                                else if (isPULU)
2935
                                        usp <= usp + 2'd1;
2936
                        end
2937
        `LW_ACCA:       begin
2938
                                acca <= dat;
2939
                                radr <= radr + 2'd1;
2940
                                if (isRTI) begin
2941
                                        $display("loaded acca=%h from %h", dat, radr);
2942
                                        ssp <= ssp + 2'd1;
2943
                                        next_state(PULL1);
2944
                                end
2945
                                else if (isPULU) begin
2946
                                        usp <= usp + 2'd1;
2947
                                        next_state(PULL1);
2948
                                end
2949
                                else if (isPULS) begin
2950
                                        ssp <= ssp + 2'd1;
2951
                                        next_state(PULL1);
2952
                                end
2953
                                else
2954
                                        next_state(IFETCH);
2955
                        end
2956
        `LW_ACCB:       begin
2957
                                accb <= dat;
2958
                                radr <= radr + 2'd1;
2959
                                if (isRTI) begin
2960 11 robfinch
                                        $display("loaded accb=%h from %h", dat, radr);
2961 2 robfinch
                                        ssp <= ssp + 2'd1;
2962
                                        next_state(PULL1);
2963
                                end
2964
                                else if (isPULU) begin
2965
                                        usp <= usp + 2'd1;
2966
                                        next_state(PULL1);
2967
                                end
2968
                                else if (isPULS) begin
2969
                                        ssp <= ssp + 2'd1;
2970
                                        next_state(PULL1);
2971
                                end
2972
                                else
2973
                                        next_state(IFETCH);
2974
                        end
2975
        `LW_DPR:        begin
2976
                                dpr <= dat;
2977
                                radr <= radr + 2'd1;
2978
                                if (isRTI) begin
2979
                                        $display("loaded dpr=%h from %h", dat, radr);
2980
                                        ssp <= ssp + 2'd1;
2981
                                        next_state(PULL1);
2982
                                end
2983
                                else if (isPULU) begin
2984
                                        usp <= usp + 2'd1;
2985
                                        next_state(PULL1);
2986
                                end
2987
                                else if (isPULS) begin
2988
                                        ssp <= ssp + 2'd1;
2989
                                        next_state(PULL1);
2990
                                end
2991
                                else
2992
                                        next_state(IFETCH);
2993
                        end
2994
        `LW_XH: begin
2995
                                load_what <= `LW_XL;
2996
                                next_state(LOAD1);
2997
                                xr[`HIBYTE] <= dat;
2998
                                radr <= radr + 2'd1;
2999
                                if (isRTI) begin
3000
                                        $display("loaded XH=%h from %h", dat, radr);
3001
                                        ssp <= ssp + 2'd1;
3002
                                end
3003
                                else if (isPULU) begin
3004
                                        usp <= usp + 2'd1;
3005
                                end
3006
                                else if (isPULS) begin
3007
                                        ssp <= ssp + 2'd1;
3008
                                end
3009
                        end
3010
        `LW_XL: begin
3011
                                xr[`LOBYTE] <= dat;
3012
                                radr <= radr + 2'd1;
3013
                                if (isRTI) begin
3014
                                        $display("loaded XL=%h from %h", dat, radr);
3015
                                        ssp <= ssp + 2'd1;
3016
                                        next_state(PULL1);
3017
                                end
3018
                                else if (isPULU) begin
3019
                                        usp <= usp + 2'd1;
3020
                                        next_state(PULL1);
3021
                                end
3022
                                else if (isPULS) begin
3023
                                        ssp <= ssp + 2'd1;
3024
                                        next_state(PULL1);
3025
                                end
3026
                                else
3027
                                        next_state(IFETCH);
3028
                        end
3029
        `LW_YH:
3030
                        begin
3031
                                load_what <= `LW_YL;
3032
                                next_state(LOAD1);
3033
                                yr[`HIBYTE] <= dat;
3034
                                radr <= radr + 2'd1;
3035
                                if (isRTI) begin
3036
                                        $display("loadded YH=%h", dat);
3037
                                        ssp <= ssp + 2'd1;
3038
                                end
3039
                                else if (isPULU) begin
3040
                                        usp <= usp + 2'd1;
3041
                                end
3042
                                else if (isPULS) begin
3043
                                        ssp <= ssp + 2'd1;
3044
                                end
3045
                        end
3046
        `LW_YL: begin
3047
                                yr[`LOBYTE] <= dat;
3048
                                radr <= radr + 2'd1;
3049
                                if (isRTI) begin
3050
                                        $display("loadded YL=%h", dat);
3051
                                        ssp <= ssp + 2'd1;
3052
                                        next_state(PULL1);
3053
                                end
3054
                                else if (isPULU) begin
3055
                                        usp <= usp + 2'd1;
3056
                                        next_state(PULL1);
3057
                                end
3058
                                else if (isPULS) begin
3059
                                        ssp <= ssp + 2'd1;
3060
                                        next_state(PULL1);
3061
                                end
3062
                                else
3063
                                        next_state(IFETCH);
3064
                        end
3065
        `LW_USPH:       begin
3066
                                load_what <= `LW_USPL;
3067
                                next_state(LOAD1);
3068
                                usp[`HIBYTE] <= dat;
3069
                                radr <= radr + 2'd1;
3070
                                if (isRTI) begin
3071
                                        $display("loadded USPH=%h", dat);
3072
                                        ssp <= ssp + 2'd1;
3073
                                end
3074
                                else if (isPULS) begin
3075
                                        ssp <= ssp + 2'd1;
3076
                                end
3077
                        end
3078
        `LW_USPL:       begin
3079
                                usp[`LOBYTE] <= dat;
3080
                                radr <= radr + 2'd1;
3081
                                if (isRTI) begin
3082
                                        $display("loadded USPL=%h", dat);
3083
                                        ssp <= ssp + 2'd1;
3084
                                        next_state(PULL1);
3085
                                end
3086
                                else if (isPULS) begin
3087
                                        ssp <= ssp + 2'd1;
3088
                                        next_state(PULL1);
3089
                                end
3090
                                else
3091
                                        next_state(IFETCH);
3092
                        end
3093
        `LW_SSPH:       begin
3094
                                load_what <= `LW_SSPL;
3095
                                next_state(LOAD1);
3096
                                ssp[`HIBYTE] <= dat;
3097
                                radr <= radr + 2'd1;
3098
                                if (isRTI) begin
3099
                                        ssp <= ssp + 2'd1;
3100
                                end
3101
                                else if (isPULU) begin
3102
                                        usp <= usp + 2'd1;
3103
                                end
3104
                        end
3105
        `LW_SSPL:       begin
3106
                                ssp[`LOBYTE] <= dat;
3107
                                radr <= radr + 2'd1;
3108
                                if (isRTI) begin
3109
                                        ssp <= ssp + 2'd1;
3110
                                        next_state(PULL1);
3111
                                end
3112
                                else if (isPULU) begin
3113
                                        usp <= usp + 2'd1;
3114
                                        next_state(PULL1);
3115
                                end
3116
                                else
3117
                                        next_state(IFETCH);
3118
                        end
3119
        `LW_PCL:        begin
3120
                                pc[`LOBYTE] <= dat;
3121
                                radr <= radr + 2'd1;
3122 11 robfinch
                                // If loading from the vector table in bank zero, force pc[23:16]=0
3123
                                if (radr[`BYTE3]=={BPB{1'b0}} && radr[`BYTE2]=={BPB{1'b1}} && radr[7:4]==4'hF)
3124
                                        pc[`BYTE3] <= {BPB{1'b0}};
3125 15 robfinch
                                if (isRTI|isRTS|isRTF|isPULS) begin
3126 2 robfinch
                                        $display("loadded PCL=%h", dat);
3127
                                        ssp <= ssp + 2'd1;
3128
                                end
3129
                                else if (isPULU)
3130
                                        usp <= usp + 2'd1;
3131
                                next_state(IFETCH);
3132
                        end
3133
        `LW_PCH:        begin
3134
                                pc[`HIBYTE] <= dat;
3135
                                load_what <= `LW_PCL;
3136
                                radr <= radr + 2'd1;
3137 15 robfinch
                                if (isRTI|isRTS|isRTF|isPULS) begin
3138 2 robfinch
                                        $display("loadded PCH=%h", dat);
3139
                                        ssp <= ssp + 2'd1;
3140
                                end
3141
                                else if (isPULU)
3142
                                        usp <= usp + 2'd1;
3143
                                next_state(LOAD1);
3144
                        end
3145
        `LW_PC2316:     begin
3146
                                pc[`BYTE3] <= dat;
3147
                                load_what <= `LW_PCH;
3148
                                radr <= radr + 16'd1;
3149
                                if (isRTI|isRTF|isPULS)
3150
                                        ssp <= ssp + 16'd1;
3151
                                else if (isPULU)
3152
                                        usp <= usp + 16'd1;
3153
                                next_state(LOAD1);
3154
                        end
3155
        `LW_IAL:
3156
                        begin
3157
                                ia[`LOBYTE] <= dat;
3158
                                res[`LOBYTE] <= dat;
3159
                                radr <= {ia[`BYTE3],ia[`HIBYTE],dat};
3160
                                wadr <= {ia[`BYTE3],ia[`HIBYTE],dat};
3161
`ifdef SUPPORT_DBL_IND
3162
                                if (isDblIndirect) begin
3163
          load_what <= `LW_IAH;
3164
          next_state(LOAD1);
3165
          isDblIndirect <= `FALSE;
3166
                                end
3167
                                else
3168
`endif
3169
                                begin
3170
          load_what <= load_what2;
3171
          if (isOuterIndexed)
3172
              next_state(OUTER_INDEXING);
3173
          else begin
3174
              if (isLEA)
3175
                  next_state(IFETCH);
3176
              else if (isStore)
3177
                  next_state(STORE1);
3178
              else
3179
                  next_state(LOAD1);
3180
          end
3181
                                end
3182
                        end
3183
        `LW_IAH:
3184
                        begin
3185
                                ia[`HIBYTE] <= dat;
3186
                                res[`HIBYTE] <= dat;
3187
                                load_what <= `LW_IAL;
3188
                                radr <= radr + 2'd1;
3189
                                next_state(LOAD1);
3190
                        end
3191
        `LW_IA2316:
3192
                        begin
3193
                                ia[`BYTE3] <= dat;
3194
                                load_what <= `LW_IAH;
3195
                                radr <= radr + 32'd1;
3196
                                next_state(LOAD1);
3197
                        end
3198
        endcase
3199
end
3200
endtask
3201
 
3202
endmodule
3203
 
3204
// ============================================================================
3205
// Cache Memories
3206
// ============================================================================
3207
module rf6809_icachemem(wclk, wce, wr, wa, i, rclk, rce, pc, insn);
3208
input wclk;
3209
input wce;
3210
input wr;
3211
input [11:0] wa;
3212
input [BPB*16-1:0] i;
3213
input rclk;
3214
input rce;
3215
input [11:0] pc;
3216
output [`HEXBYTE] insn;
3217
reg [`HEXBYTE] insn;
3218
 
3219
integer n;
3220
reg [BPB*16-1:0] mem [0:255];
3221
reg [11:0] rpc,rpcp16;
3222
initial begin
3223
        for (n = 0; n < 256; n = n + 1)
3224
                mem[n] = {16{`NOP}};
3225
end
3226
 
3227
always_ff @(posedge wclk)
3228
        if (wce & wr) mem[wa[11:4]] <= i;
3229
 
3230
always_ff @(posedge rclk)
3231
        if (rce) rpc <= pc;
3232
always_ff @(posedge rclk)
3233
        if (rce) rpcp16 <= pc + 5'd16;
3234
wire [BPB*16-1:0] insn0 = mem[rpc[11:4]];
3235
wire [BPB*16-1:0] insn1 = mem[rpcp16[11:4]];
3236
always_comb
3237
        insn = {insn1,insn0} >> ({4'h0,rpc[3:0]} * BPB);
3238
 
3239
endmodule
3240
 
3241
module rf6809_itagmem(wclk, wce, wr, wa, invalidate, rclk, rce, pc, hit0, hit1);
3242
input wclk;
3243
input wce;
3244
input wr;
3245
input [`TRPBYTE] wa;
3246
input invalidate;
3247
input rclk;
3248
input rce;
3249
input [`TRPBYTE] pc;
3250
output hit0;
3251
output hit1;
3252
 
3253
integer n;
3254
reg [BPB*3-1:12] mem [0:255];
3255
reg [0:255] tvalid = 256'd0;
3256
reg [`TRPBYTE] rpc,rpcp16;
3257
wire [BPB*3-1:11] tag0,tag1;
3258
initial begin
3259
        for (n = 0; n < 256; n = n + 1)
3260
                mem[n] = {BPB*2{1'b0}};
3261
end
3262
 
3263
always_ff @(posedge wclk)
3264
        if (wce & wr) mem[wa[11:4]] <= wa[BPB*3-1:12];
3265
always_ff @(posedge wclk)
3266
        if (invalidate) tvalid <= 256'd0;
3267
        else if (wce & wr) tvalid[wa[11:4]] <= 1'b1;
3268
always_ff @(posedge rclk)
3269
        if (rce) rpc <= pc;
3270
always_ff @(posedge rclk)
3271
        if (rce) rpcp16 <= pc + 5'd16;
3272
assign tag0 = {mem[rpc[11:4]],tvalid[rpc[11:4]]};
3273
assign tag1 = {mem[rpcp16[11:4]],tvalid[rpcp16[11:4]]};
3274
 
3275
assign hit0 = tag0 == {rpc[BPB*3-1:12],1'b1};
3276 12 robfinch
// Consider a hit on port 1 if the instruction will not span onto it.
3277
assign hit1 = tag1 == {rpcp16[BPB*3-1:12],1'b1} || rpc[3:0] < 4'h9;
3278 2 robfinch
 
3279
endmodule
3280
 

powered by: WebSVN 2.1.0

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