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

Subversion Repositories t6507lp

[/] [t6507lp/] [trunk/] [rtl/] [verilog/] [t6507lp_fsm.v] - Blame information for rev 100

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

Line No. Rev Author Line
1 61 creep
////////////////////////////////////////////////////////////////////////////
2
////                                                                    ////
3
//// T6507LP IP Core                                                    ////
4
////                                                                    ////
5
//// This file is part of the T6507LP project                           ////
6
//// http://www.opencores.org/cores/t6507lp/                            ////
7
////                                                                    ////
8
//// Description                                                        ////
9
//// 6507 FSM                                                           ////
10
////                                                                    ////
11
//// TODO:                                                              ////
12 96 creep
//// - Fix relative mode, bit 7 means negative                          ////
13 61 creep
//// - Code the indirect indexed mode                                   ////
14
//// - Code the absolute indirect mode                                  ////
15
////                                                                    ////
16
//// Author(s):                                                         ////
17
//// - Gabriel Oshiro Zardo, gabrieloshiro@gmail.com                    ////
18
//// - Samuel Nascimento Pagliarini (creep), snpagliarini@gmail.com     ////
19
////                                                                    ////
20
////////////////////////////////////////////////////////////////////////////
21
////                                                                    ////
22
//// Copyright (C) 2001 Authors and OPENCORES.ORG                       ////
23
////                                                                    ////
24
//// This source file may be used and distributed without               ////
25
//// restriction provided that this copyright statement is not          ////
26
//// removed from the file and that any derivative work contains        ////
27
//// the original copyright notice and the associated disclaimer.       ////
28
////                                                                    ////
29
//// This source file is free software; you can redistribute it         ////
30
//// and/or modify it under the terms of the GNU Lesser General         ////
31
//// Public License as published by the Free Software Foundation;       ////
32
//// either version 2.1 of the License, or (at your option) any         ////
33
//// later version.                                                     ////
34
////                                                                    ////
35
//// This source is distributed in the hope that it will be             ////
36
//// useful, but WITHOUT ANY WARRANTY; without even the implied         ////
37
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR            ////
38
//// PURPOSE. See the GNU Lesser General Public License for more        ////
39
//// details.                                                           ////
40
////                                                                    ////
41
//// You should have received a copy of the GNU Lesser General          ////
42
//// Public License along with this source; if not, download it         ////
43
//// from http://www.opencores.org/lgpl.shtml                           ////
44
////                                                                    ////
45
////////////////////////////////////////////////////////////////////////////
46
 
47
`timescale 1ns / 1ps
48
 
49 86 creep
module t6507lp_fsm(clk, reset_n, alu_result, alu_status, data_in, address, control, data_out, alu_opcode, alu_a, alu_enable, alu_x, alu_y);
50 87 creep
        parameter DATA_SIZE = 8;
51
        parameter ADDR_SIZE = 13;
52 68 creep
 
53 83 creep
        localparam DATA_SIZE_ = DATA_SIZE - 4'b0001;
54
        localparam ADDR_SIZE_ = ADDR_SIZE - 4'b0001;
55 82 creep
 
56 71 creep
        input clk;
57
        input reset_n;
58 82 creep
        input [DATA_SIZE_:0] alu_result;
59
        input [DATA_SIZE_:0] alu_status;
60
        input [DATA_SIZE_:0] data_in;
61
        output reg [ADDR_SIZE_:0] address;
62 61 creep
        output reg control; // one bit is enough? read = 0, write = 1
63 82 creep
        output reg [DATA_SIZE_:0] data_out;
64
        output reg [DATA_SIZE_:0] alu_opcode;
65
        output reg [DATA_SIZE_:0] alu_a;
66 68 creep
        output reg alu_enable;
67 61 creep
 
68 86 creep
        input [DATA_SIZE_:0] alu_x;
69
        input [DATA_SIZE_:0] alu_y;
70 68 creep
 
71 61 creep
        // FSM states
72 95 creep
        localparam FETCH_OP = 5'b00000;
73
        localparam FETCH_OP_CALC = 5'b00001;
74
        localparam FETCH_LOW = 5'b00010;
75
        localparam FETCH_HIGH = 5'b00011;
76
        localparam READ_MEM = 5'b00100;
77
        localparam DUMMY_WRT_CALC = 5'b00101;
78
        localparam WRITE_MEM = 5'b00110;
79
        localparam FETCH_OP_CALC_PARAM = 5'b00111;
80
        localparam READ_MEM_CALC_INDEX = 5'b01000;
81
        localparam FETCH_HIGH_CALC_INDEX = 5'b01001;
82
        localparam READ_MEM_FIX_ADDR = 5'b01010;
83
        localparam FETCH_OP_EVAL_BRANCH = 5'b01011;
84
        localparam FETCH_OP_FIX_PC = 5'b01100;
85
        localparam READ_FROM_POINTER = 5'b01101;
86
        localparam READ_FROM_POINTER_X = 5'b01110;
87
        localparam READ_FROM_POINTER_X1 = 5'b01111;
88 61 creep
 
89 95 creep
        localparam RESET = 5'b11111;
90
 
91 61 creep
        // OPCODES TODO: verify how this get synthesised
92
        `include "../T6507LP_Package.v"
93
 
94
        // control signals
95
        localparam MEM_READ = 1'b0;
96
        localparam MEM_WRITE = 1'b1;
97
 
98 82 creep
        reg [ADDR_SIZE_:0] pc;           // program counter
99
        reg [DATA_SIZE_:0] sp;           // stack pointer
100
        reg [DATA_SIZE_:0] ir;           // instruction register
101
        reg [ADDR_SIZE_:0] temp_addr;    // temporary address
102
        reg [DATA_SIZE_:0] temp_data;    // temporary data
103 61 creep
 
104 96 creep
        reg [4:0] state, next_state; // current and next state registers
105 61 creep
        // TODO: not sure if this will be 4 bits wide. as of march 9th this was 4bit wide.
106
 
107
        // wiring that simplifies the FSM logic
108
        reg absolute;
109
        reg absolute_indexed;
110
        reg accumulator;
111
        reg immediate;
112
        reg implied;
113 95 creep
        reg indirectx;
114
        reg indirecty;
115 61 creep
        reg relative;
116
        reg zero_page;
117
        reg zero_page_indexed;
118 86 creep
        reg [DATA_SIZE_:0] index; // will be assigned with either X or Y
119 61 creep
 
120
        // regs that store the type of operation. again, this simplifies the FSM a lot.
121
        reg read;
122
        reg read_modify_write;
123
        reg write;
124
        reg jump;
125
 
126 82 creep
        wire [ADDR_SIZE_:0] next_pc;
127 63 creep
        assign next_pc = pc + 13'b0000000000001;
128 61 creep
 
129 87 creep
        reg [ADDR_SIZE_:0] address_plus_index; // this would update more times than actually needed, consuming power.
130
        reg page_crossed;                       // so the simple assign was changed into a combinational always block
131
 
132 94 creep
        reg branch;
133
 
134 87 creep
        always @(*) begin
135
                address_plus_index = 0;
136
                page_crossed = 0;
137 86 creep
 
138 88 creep
                if (state == READ_MEM_CALC_INDEX || state == READ_MEM_FIX_ADDR || state == FETCH_HIGH_CALC_INDEX) begin
139
                        {page_crossed, address_plus_index[7:0]} = temp_addr[7:0] + index;
140
                        address_plus_index[12:8] = temp_addr[12:8] + page_crossed;
141 87 creep
                end
142 94 creep
                else if (branch) begin
143
                        if (state == FETCH_OP_FIX_PC || state == FETCH_OP_EVAL_BRANCH) begin
144
                                {page_crossed, address_plus_index[7:0]} = pc[7:0] + index;
145
                                address_plus_index[12:8] = pc[12:8] + page_crossed;// warning: pc might feed these lines twice and cause branch failure
146
                        end                                                             // solution: add a temp reg i guess
147
                end
148 95 creep
                else if (state == READ_FROM_POINTER) begin
149 96 creep
                        if (indirectx) begin
150
                                {page_crossed, address_plus_index[7:0]} = temp_data + index;
151
                                address_plus_index[12:8] = 5'b00000;
152
                        end
153
                        else begin // indirecty falls here
154
                                address_plus_index[7:0] = temp_data + 8'h01;
155
                                address_plus_index[12:8] = 5'b00000;
156
                        end
157 95 creep
                end
158
                else if (state == READ_FROM_POINTER_X) begin
159
                        {page_crossed, address_plus_index[7:0]} = temp_data + index + 8'h01;
160
                        address_plus_index[12:8] = 5'b00000;
161
                end
162 96 creep
                else if (state == READ_FROM_POINTER_X1) begin
163
                        {page_crossed, address_plus_index[7:0]} = temp_addr[7:0] + index;
164
                        address_plus_index[12:8] = temp_addr[12:8] + page_crossed;
165
                end
166 87 creep
        end
167
 
168 71 creep
        always @ (posedge clk or negedge reset_n) begin // sequencial always block
169
                if (reset_n == 1'b0) begin
170
                        // all registers must assume default values
171 68 creep
                        pc <= 0; // TODO: this is written somewhere. something about a reset vector. must be checked.
172
                        sp <= 0; // TODO: the default is not 0. maybe $0100 or something like that. must be checked.
173 82 creep
                        ir <= 8'h00;
174 71 creep
                        temp_addr <= 0;
175 82 creep
                        temp_data <= 8'h00;
176 68 creep
                        state <= RESET;
177 71 creep
                        // registered outputs also receive default values
178
                        address <= 0;
179 82 creep
                        control <= MEM_READ;
180
                        data_out <= 8'h00;
181 61 creep
                end
182
                else begin
183
                        state <= next_state;
184 83 creep
 
185 61 creep
                        case (state)
186 68 creep
                                RESET: begin
187 82 creep
                                        // The processor was reset
188
                                        $write("under reset");
189 68 creep
                                end
190 61 creep
                                FETCH_OP: begin // this state is the simplest one. it is a simple fetch that must be done when the cpu was reset or
191
                                                // the last cycle was a memory write.
192
                                        pc <= next_pc;
193 71 creep
                                        address <= next_pc;
194 83 creep
                                        control <= MEM_READ;
195 70 creep
                                        ir <= data_in;
196 61 creep
                                end
197 71 creep
                                FETCH_OP_CALC, FETCH_OP_CALC_PARAM: begin // this is the pipeline happening!
198 61 creep
                                        pc <= next_pc;
199 71 creep
                                        address <= next_pc;
200 83 creep
                                        control <= MEM_READ;
201 70 creep
                                        ir <= data_in;
202 61 creep
                                end
203 68 creep
                                FETCH_LOW: begin // in this state the opcode is already known so truly execution begins
204
                                        if (accumulator || implied) begin
205 70 creep
                                                pc <= pc; // is this better?
206 71 creep
                                                address <= pc;
207 83 creep
                                                control <= MEM_READ;
208 61 creep
                                        end
209 94 creep
                                        else if (immediate || relative) begin
210 68 creep
                                                pc <= next_pc;
211 71 creep
                                                address <= next_pc;
212 83 creep
                                                control <= MEM_READ;
213 70 creep
                                                temp_data <= data_in; // the follow-up byte is saved in temp_data 
214 61 creep
                                        end
215 87 creep
                                        else if (absolute || absolute_indexed) begin
216 71 creep
                                                pc <= next_pc;
217
                                                address <= next_pc;
218 83 creep
                                                control <= MEM_READ;
219 87 creep
                                                temp_addr <= {{5{1'b0}},data_in};
220 71 creep
                                        end
221 77 creep
                                        else if (zero_page) begin
222
                                                pc <= next_pc;
223
                                                address <= {{5{1'b0}},data_in};
224 78 creep
                                                temp_addr <= {{5{1'b0}},data_in};
225
 
226 77 creep
                                                if (write) begin
227
                                                        control <= MEM_WRITE;
228 78 creep
                                                        data_out <= alu_result;
229 77 creep
                                                end
230 83 creep
                                                else begin
231
                                                        control <= MEM_READ;
232
                                                        data_out <= 8'h00;
233
                                                end
234 77 creep
                                        end
235 86 creep
                                        else if (zero_page_indexed) begin
236
                                                pc <= next_pc;
237 94 creep
                                                address <= {{5{1'b0}}, data_in};
238
                                                temp_addr <= {{5{1'b0}}, data_in};
239 86 creep
                                                control <= MEM_READ;
240
                                        end
241 96 creep
                                        else if (indirectx || indirecty) begin
242 95 creep
                                                pc <= next_pc;
243
                                                address <= data_in;
244
                                                temp_data <= data_in;
245
                                                control <= MEM_READ;
246
                                        end
247 61 creep
                                end
248 87 creep
                                FETCH_HIGH_CALC_INDEX: begin
249
                                        pc <= next_pc;
250
                                        temp_addr[12:8] <= data_in[4:0];
251
                                        address <= {data_in[4:0], address_plus_index[7:0]};
252
                                        control <= MEM_READ;
253
                                        data_out <= 8'h00;
254
                                end
255 94 creep
                                FETCH_OP_EVAL_BRANCH: begin
256
                                        if (branch) begin
257
                                                pc <= {{5{1'b0}}, address_plus_index[7:0]};
258
                                                address <= {{5{1'b0}}, address_plus_index[7:0]};
259
                                                control <= MEM_READ;
260
                                                data_out <= 8'h00;
261
                                        end
262
                                        else begin
263
                                                pc <= next_pc;
264
                                                address <= next_pc;
265
                                                control <= MEM_READ;
266
                                                data_out <= 8'h00;
267 95 creep
                                                ir <= data_in;
268 94 creep
                                        end
269
                                end
270
                                FETCH_OP_FIX_PC: begin
271
                                        if (page_crossed) begin
272
                                                pc[12:8] <= address_plus_index[12:8];
273
                                                address[12:8] <= address_plus_index[12:8];
274
                                        end
275
                                        else begin
276
                                                pc <= next_pc;
277
                                                address <= next_pc;
278
                                                control <= MEM_READ;
279
                                                ir <= data_in;
280
                                        end
281
                                end
282 71 creep
                                FETCH_HIGH: begin
283
                                        if (jump) begin
284 83 creep
                                                pc <= {data_in[4:0], temp_addr[7:0]}; // PCL <= first byte, PCH <= second byte
285
                                                address <= {data_in[4:0], temp_addr[7:0]};
286
                                                control <= MEM_READ;
287
                                                data_out <= 8'h00;
288 71 creep
                                        end
289
                                        else begin
290
                                                if (write) begin
291
                                                        pc <= next_pc;
292
                                                        temp_addr[12:8] <= data_in[4:0];
293
                                                        address <= {data_in[4:0],temp_addr[7:0]};
294
                                                        control <= MEM_WRITE;
295 77 creep
                                                        data_out <= alu_result;
296 71 creep
                                                end
297
                                                else begin // read_modify_write or just read
298
                                                        pc <= next_pc;
299
                                                        temp_addr[12:8] <= data_in[4:0];
300
                                                        address <= {data_in[4:0],temp_addr[7:0]};
301 83 creep
                                                        control <= MEM_READ;
302
                                                        data_out <= 8'h00;
303 71 creep
                                                end
304
                                        end
305
                                        //else begin
306
                                        //      $write("FETCHHIGH PROBLEM"); 
307
                                        //      $finish(0); 
308
                                        //end
309 61 creep
                                end
310 71 creep
                                READ_MEM: begin
311
                                        if (read_modify_write) begin
312
                                                pc <= pc;
313
                                                address <= temp_addr;
314
                                                control <= MEM_WRITE;
315
                                                temp_data <= data_in;
316
                                                data_out <= data_in; // writeback the same value
317
                                        end
318
                                        else begin
319
                                                pc <= pc;
320
                                                address <= pc;
321
                                                temp_data <= data_in;
322 83 creep
                                                control <= MEM_READ;
323
                                                data_out <= 8'h00;
324 71 creep
                                        end
325 70 creep
                                end
326 86 creep
                                READ_MEM_CALC_INDEX: begin
327
                                                //pc <= next_pc; // pc was  already updated in the previous cycle
328
                                                address <= address_plus_index;
329
                                                temp_addr <= address_plus_index;
330
 
331
                                                if (write) begin
332
                                                        control <= MEM_WRITE;
333
                                                        data_out <= alu_result;
334
                                                end
335
                                                else begin
336
                                                        control <= MEM_READ;
337
                                                        data_out <= 8'h00;
338
                                                end
339
 
340
                                end
341 87 creep
                                READ_MEM_FIX_ADDR: begin
342
                                        if (read) begin
343
                                                control <= MEM_READ;
344
                                                data_out <= 8'h00;
345
 
346
                                                if (page_crossed) begin
347
                                                        address <= address_plus_index;
348
                                                        temp_addr <= address_plus_index;
349
                                                end
350
                                                else begin
351
                                                        address <= pc;
352
                                                        temp_data <= data_in;
353
                                                end
354
                                        end
355
                                        else if (write) begin
356
                                                control <= MEM_WRITE;
357
                                                data_out <= alu_result;
358
                                                address <= address_plus_index;
359
                                                temp_addr <= address_plus_index;
360
 
361
                                        end
362
                                        else begin // read modify write
363
                                                control <= MEM_READ;
364
                                                data_out <= 8'h00;
365
                                                address <= address_plus_index;
366
                                                temp_addr <= address_plus_index;
367
                                        end
368
                                end
369 71 creep
                                DUMMY_WRT_CALC: begin
370
                                        pc <= pc;
371
                                        address <= temp_addr;
372
                                        control <= MEM_WRITE;
373
                                        data_out <= alu_result;
374 70 creep
                                end
375 71 creep
                                WRITE_MEM: begin
376
                                        pc <= pc;
377
                                        address <= pc;
378 83 creep
                                        control <= MEM_READ;
379
                                        data_out <= 8'h00;
380 70 creep
                                end
381 95 creep
                                READ_FROM_POINTER: begin
382
                                        pc <= pc;
383
                                        control <= MEM_READ;
384 96 creep
 
385
                                        if (indirectx) begin
386
                                                address <= address_plus_index;
387
                                        end
388
                                        else begin // indirecty falls here
389
                                                address <= address_plus_index;
390
                                                temp_addr <= {{5{1'b0}}, data_in};
391
                                        end
392 95 creep
                                end
393
                                READ_FROM_POINTER_X: begin
394
                                        pc <= pc;
395
                                        address <= address_plus_index;
396
                                        temp_addr[7:0] <= data_in;
397
                                        control <= MEM_READ;
398
                                end
399
                                READ_FROM_POINTER_X1: begin
400
                                        pc <= pc;
401 96 creep
 
402
                                        if (indirectx) begin
403
                                                address <= {data_in[5:0], temp_addr[7:0]};
404
                                                if (write) begin
405
                                                        control <= MEM_WRITE;
406
                                                        data_out <= alu_result;
407
                                                end
408
                                                else begin
409
                                                        control <= MEM_READ;
410
                                                end
411 95 creep
                                        end
412 96 creep
                                        else begin // indirecty falls here
413
                                                address <= address_plus_index;
414
                                                temp_addr[12:8] <= data_in;
415 95 creep
                                                control <= MEM_READ;
416
                                        end
417
                                end
418 70 creep
                                default: begin
419 95 creep
                                        $write("unknown state"); // TODO: check if synth really ignores this 2 lines. Otherwise wrap it with a `ifdef 
420 70 creep
                                        $finish(0);
421
                                end
422
 
423
                        endcase
424
                end
425
        end
426
 
427 71 creep
        always @ (*) begin // this is the next_state logic and the output logic always block
428
                alu_opcode = 8'h00;
429
                alu_a = 8'h00;
430
                alu_enable = 1'b0;
431 70 creep
 
432 71 creep
                next_state = RESET; // this prevents the latch
433 68 creep
 
434 71 creep
                case (state)
435
                        RESET: begin
436
                                next_state = FETCH_OP;
437
                        end
438
                        FETCH_OP: begin
439
                                next_state = FETCH_LOW;
440
                        end
441 82 creep
                        //FETCH_OP_CALC: begin // so far no addressing mode required the use of this state
442
                        //      next_state = FETCH_LOW;
443
                        //      alu_opcode = ir;
444
                        //      alu_enable = 1'b1;
445
                        //end
446 71 creep
                        FETCH_OP_CALC_PARAM: begin
447
                                next_state = FETCH_LOW;
448
                                alu_opcode = ir;
449
                                alu_enable = 1'b1;
450
                                alu_a = temp_data;
451
                        end
452
                        FETCH_LOW: begin
453
                                if (accumulator  || implied) begin
454
                                        alu_opcode = ir;
455
                                        alu_enable = 1'b1;
456
                                        next_state = FETCH_OP;
457
                                end
458
                                else if (immediate) begin
459
                                        next_state = FETCH_OP_CALC_PARAM;
460
                                end
461 77 creep
                                else if (zero_page) begin
462
                                        if (read || read_modify_write) begin
463
                                                next_state = READ_MEM;
464
                                        end
465
                                        else if (write) begin
466
                                                next_state = WRITE_MEM;
467 86 creep
                                                alu_opcode = ir;
468
                                                alu_enable = 1'b1;
469
                                                alu_a = 8'h00;
470 77 creep
                                        end
471
                                        else begin
472
                                                $write("unknown behavior");
473
                                                $finish(0);
474
                                        end
475
                                end
476 86 creep
                                else if (zero_page_indexed) begin
477
                                        next_state = READ_MEM_CALC_INDEX;
478
                                end
479 87 creep
                                else if (absolute) begin // at least the absolute address mode falls here
480 71 creep
                                        next_state = FETCH_HIGH;
481 87 creep
                                        if (write) begin // this is being done one cycle early but i have checked and the ALU will still work properly
482 86 creep
                                                alu_opcode = ir;
483
                                                alu_enable = 1'b1;
484
                                                alu_a = 8'h00;
485
                                        end
486 71 creep
                                end
487 87 creep
                                else if (absolute_indexed) begin
488
                                        next_state = FETCH_HIGH_CALC_INDEX;
489
                                end
490 94 creep
                                else if (relative) begin
491
                                        next_state = FETCH_OP_EVAL_BRANCH;
492
                                end
493 96 creep
                                else if (indirectx || indirecty) begin
494 95 creep
                                        next_state = READ_FROM_POINTER;
495
                                end
496 71 creep
                        end
497 95 creep
                        READ_FROM_POINTER: begin
498 96 creep
                                if (indirectx) begin
499
                                        next_state = READ_FROM_POINTER_X;
500
                                end
501
                                else begin // indirecty falls here
502
                                        next_state = READ_FROM_POINTER_X1;
503
                                end
504 95 creep
                        end
505
                        READ_FROM_POINTER_X: begin
506
                                next_state = READ_FROM_POINTER_X1;
507
                        end
508
                        READ_FROM_POINTER_X1: begin
509 96 creep
                                if (indirecty) begin
510
                                        next_state = READ_MEM_FIX_ADDR;
511 95 creep
                                end
512 96 creep
                                else begin
513
                                        if (read || read_modify_write) begin
514
                                                next_state = READ_MEM;
515
                                        end
516
                                        else if (write) begin
517
                                                alu_opcode = ir;
518
                                                alu_enable = 1'b1;
519
                                                next_state = WRITE_MEM;
520
                                        end
521 95 creep
                                end
522
                        end
523 94 creep
                        FETCH_OP_EVAL_BRANCH: begin
524
                                if (branch) begin
525
                                        next_state = FETCH_OP_FIX_PC;
526
                                end
527
                                else begin
528
                                        next_state = FETCH_LOW;
529
                                end
530
                        end
531
                        FETCH_OP_FIX_PC: begin
532
                                if (page_crossed) begin
533
                                        next_state = FETCH_OP;
534
                                end
535
                                else begin
536
                                        next_state = FETCH_LOW;
537
                                end
538
                        end
539 87 creep
                        FETCH_HIGH_CALC_INDEX: begin
540
                                next_state = READ_MEM_FIX_ADDR;
541
                        end
542
                        READ_MEM_FIX_ADDR: begin
543
                                if (read) begin
544
                                        if (page_crossed) begin
545
                                                next_state = READ_MEM;
546
                                        end
547
                                        else begin
548
                                                next_state = FETCH_OP_CALC_PARAM;
549
                                        end
550
                                end
551
                                else if (read_modify_write) begin
552
                                        next_state = READ_MEM;
553
                                end
554
                                else if (write) begin
555
                                        next_state = WRITE_MEM;
556 100 creep
                                        alu_enable = 1'b1;
557
                                        alu_opcode = ir;
558 87 creep
                                end
559
                                else begin
560
                                        $write("unknown behavior");
561
                                        $finish(0);
562
                                end
563
                        end
564 71 creep
                        FETCH_HIGH: begin
565
                                if (jump) begin
566 68 creep
                                        next_state = FETCH_OP;
567 61 creep
                                end
568 71 creep
                                else if (read || read_modify_write) begin
569
                                        next_state = READ_MEM;
570 61 creep
                                end
571 71 creep
                                else if (write) begin
572
                                        next_state = WRITE_MEM;
573 68 creep
                                end
574 71 creep
                                else begin
575
                                        $write("unknown behavior");
576
                                        $finish(0);
577 61 creep
                                end
578 71 creep
                        end
579 86 creep
                        READ_MEM_CALC_INDEX: begin
580
                                if (read || read_modify_write) begin
581
                                        next_state = READ_MEM;
582
                                end
583
                                else if (write) begin
584
                                        alu_opcode = ir;
585
                                        alu_enable = 1'b1;
586
                                        next_state = WRITE_MEM;
587
                                end
588
                                else begin
589
                                        $write("unknown behavior");
590
                                        $finish(0);
591
                                end
592
                        end
593 71 creep
                        READ_MEM: begin
594
                                if (read) begin
595
                                        next_state = FETCH_OP_CALC_PARAM;
596 61 creep
                                end
597 71 creep
                                else if (read_modify_write) begin
598
                                        next_state = DUMMY_WRT_CALC;
599
                                end
600
                        end
601
                        DUMMY_WRT_CALC: begin
602
                                alu_opcode = ir;
603
                                alu_enable = 1'b1;
604
                                alu_a = data_in;
605
                                next_state = WRITE_MEM;
606
                        end
607
                        WRITE_MEM: begin
608
                                next_state = FETCH_OP;
609
                        end
610
                        default: begin
611
                                next_state = RESET;
612
                        end
613
                endcase
614 61 creep
        end
615
 
616 77 creep
        // this always block is responsible for updating the address mode and the type of operation being done
617 68 creep
        always @ (*) begin // 
618 61 creep
                absolute = 1'b0;
619
                absolute_indexed = 1'b0;
620
                accumulator = 1'b0;
621
                immediate = 1'b0;
622
                implied = 1'b0;
623 95 creep
                indirectx = 1'b0;
624
                indirecty = 1'b0;
625 61 creep
                relative = 1'b0;
626
                zero_page = 1'b0;
627
                zero_page_indexed = 1'b0;
628 86 creep
 
629
                index = 1'b0;
630 61 creep
 
631
                read = 1'b0;
632
                read_modify_write = 1'b0;
633
                write = 1'b0;
634
                jump = 1'b0;
635 94 creep
                branch = 1'b0;
636
 
637
                //$write("trying with %h\n", ir);
638 61 creep
 
639 70 creep
                case (ir)
640
                        BRK_IMP, CLC_IMP, CLD_IMP, CLI_IMP, CLV_IMP, DEX_IMP, DEY_IMP, INX_IMP, INY_IMP, NOP_IMP, PHA_IMP, PHP_IMP, PLA_IMP,
641
                        PLP_IMP, RTI_IMP, RTS_IMP, SEC_IMP, SED_IMP, SEI_IMP, TAX_IMP, TAY_IMP, TSX_IMP, TXA_IMP, TXS_IMP, TYA_IMP: begin
642
                                implied = 1'b1;
643
                        end
644
                        ASL_ACC, LSR_ACC, ROL_ACC, ROR_ACC: begin
645
                                accumulator = 1'b1;
646
                        end
647
                        ADC_IMM, AND_IMM, CMP_IMM, CPX_IMM, CPY_IMM, EOR_IMM, LDA_IMM, LDX_IMM, LDY_IMM, ORA_IMM, SBC_IMM: begin
648
                                immediate = 1'b1;
649
                        end
650
                        ADC_ZPG, AND_ZPG, ASL_ZPG, BIT_ZPG, CMP_ZPG, CPX_ZPG, CPY_ZPG, DEC_ZPG, EOR_ZPG, INC_ZPG, LDA_ZPG, LDX_ZPG, LDY_ZPG,
651
                        LSR_ZPG, ORA_ZPG, ROL_ZPG, ROR_ZPG, SBC_ZPG, STA_ZPG, STX_ZPG, STY_ZPG: begin
652
                                zero_page = 1'b1;
653
                        end
654
                        ADC_ZPX, AND_ZPX, ASL_ZPX, CMP_ZPX, DEC_ZPX, EOR_ZPX, INC_ZPX, LDA_ZPX, LDY_ZPX, LSR_ZPX, ORA_ZPX, ROL_ZPX, ROR_ZPX,
655 86 creep
                        SBC_ZPX, STA_ZPX, STY_ZPX: begin
656 70 creep
                                zero_page_indexed = 1'b1;
657 86 creep
                                index = alu_x;
658 70 creep
                        end
659 86 creep
                        LDX_ZPY, STX_ZPY: begin
660
                                zero_page_indexed = 1'b1;
661
                                index = alu_y;
662
                        end
663 94 creep
                        BCC_REL: begin
664 70 creep
                                relative = 1'b1;
665 94 creep
                                index = temp_data;
666
 
667
                                if (!alu_status[C]) begin
668
                                        branch = 1'b1;
669
                                end
670
                                else begin
671
                                        branch = 1'b0;
672
                                end
673 70 creep
                        end
674 94 creep
                        BCS_REL: begin
675
                                relative = 1'b1;
676
                                index = temp_data;
677
 
678
                                if (alu_status[C]) begin
679
                                        branch = 1'b1;
680
                                end
681
                                else begin
682
                                        branch = 1'b0;
683
                                end
684
                        end
685
                        BEQ_REL: begin
686
                                relative = 1'b1;
687
                                index = temp_data;
688
 
689
                                if (alu_status[Z]) begin
690
                                        branch = 1'b1;
691
                                end
692
                                else begin
693
                                        branch = 1'b0;
694
                                end
695
                        end
696
                        BNE_REL: begin
697
                                relative = 1'b1;
698
                                index = temp_data;
699
 
700
                                if (alu_status[Z] == 1'b0) begin
701
                                        branch = 1'b1;
702
                                end
703
                                else begin
704
                                        branch = 1'b0;
705
                                end
706
                        end
707
                        BPL_REL: begin
708
                                relative = 1'b1;
709
                                index = temp_data;
710
 
711
                                if (!alu_status[N]) begin
712
                                        branch = 1'b1;
713
                                end
714
                                else begin
715
                                        branch = 1'b0;
716
                                end
717
                        end
718
                        BMI_REL: begin
719
                                relative = 1'b1;
720
                                index = temp_data;
721
 
722
                                if (alu_status[N]) begin
723
                                        branch = 1'b1;
724
                                end
725
                                else begin
726
                                        branch = 1'b0;
727
                                end
728
                        end
729
                        BVC_REL: begin
730
                                relative = 1'b1;
731
                                index = temp_data;
732
 
733
                                if (!alu_status[V]) begin
734
                                        branch = 1'b1;
735
                                end
736
                                else begin
737
                                        branch = 1'b0;
738
                                end
739
                        end
740
                        BVS_REL: begin
741
                                relative = 1'b1;
742
                                index = temp_data;
743
 
744
                                if (alu_status[V]) begin
745
                                        branch = 1'b1;
746
                                end
747
                                else begin
748
                                        branch = 1'b0;
749
                                end
750
                        end
751 70 creep
                        ADC_ABS, AND_ABS, ASL_ABS, BIT_ABS, CMP_ABS, CPX_ABS, CPY_ABS, DEC_ABS, EOR_ABS, INC_ABS, JMP_ABS, JSR_ABS, LDA_ABS,
752
                        LDX_ABS, LDY_ABS, LSR_ABS, ORA_ABS, ROL_ABS, ROR_ABS, SBC_ABS, STA_ABS, STX_ABS, STY_ABS: begin
753
                                absolute = 1'b1;
754
                        end
755
                        ADC_ABX, AND_ABX, ASL_ABX, CMP_ABX, DEC_ABX, EOR_ABX, INC_ABX, LDA_ABX, LDY_ABX, LSR_ABX, ORA_ABX, ROL_ABX, ROR_ABX,
756 87 creep
                        SBC_ABX, STA_ABX: begin
757 70 creep
                                absolute_indexed = 1'b1;
758 87 creep
                                index = alu_x;
759 70 creep
                        end
760 87 creep
                        ADC_ABY, AND_ABY, CMP_ABY, EOR_ABY, LDA_ABY, LDX_ABY, ORA_ABY, SBC_ABY, STA_ABY: begin
761
                                absolute_indexed = 1'b1;
762
                                index = alu_y;
763
                        end
764 95 creep
                        ADC_IDX, AND_IDX, CMP_IDX, EOR_IDX, LDA_IDX, ORA_IDX, SBC_IDX, STA_IDX: begin
765
                                indirectx = 1'b1;
766
                                index = alu_x;
767 70 creep
                        end
768 95 creep
                        ADC_IDY, AND_IDY, CMP_IDY, EOR_IDY, LDA_IDY, ORA_IDY, SBC_IDY, STA_IDY: begin
769
                                indirecty = 1'b1;
770
                                index = alu_y;
771
                        end
772 71 creep
                        default: begin
773 94 creep
                                $write("state : %b", state);
774
                                if (reset_n == 1 && state != FETCH_OP_FIX_PC) begin // the processor is NOT being reset neither it is fixing the pc
775 86 creep
                                        $write("\nunknown OPCODE!!!!! 0x%h\n", ir);
776
                                        $finish();
777
                                end
778 71 creep
                        end
779 70 creep
                endcase
780 71 creep
 
781
                case (ir)
782
                        ASL_ACC, ASL_ZPG, ASL_ZPX, ASL_ABS, ASL_ABX, LSR_ACC, LSR_ZPG, LSR_ZPX, LSR_ABS, LSR_ABX, ROL_ACC, ROL_ZPG, ROL_ZPX, ROL_ABS,
783
                        ROL_ABX, ROR_ACC, ROR_ZPG, ROR_ZPX, ROR_ABS, ROR_ABX, INC_ZPG, INC_ZPX, INC_ABS, INC_ABX, DEC_ZPG, DEC_ZPX, DEC_ABS,
784
                        DEC_ABX: begin
785
                                read_modify_write = 1'b1;
786
                        end
787
                        STA_ZPG, STA_ZPX, STA_ABS, STA_ABX, STA_ABY, STA_IDX, STA_IDY, STX_ZPG, STX_ZPY, STX_ABS, STY_ZPG, STY_ZPX, STY_ABS: begin
788
                                write = 1'b1;
789
                        end
790
                        default: begin // this should work fine since the previous case statement will detect the unknown/undocumented/unsupported opcodes
791
                                read = 1'b1;
792
                        end
793
                endcase
794 61 creep
 
795 71 creep
                if (ir == JMP_ABS || ir == JMP_IND) begin // the opcodes are 8'h4C and 8'h6C
796 70 creep
                        jump = 1'b1;
797
                end
798 86 creep
        end
799 61 creep
endmodule
800
 
801
 
802
 

powered by: WebSVN 2.1.0

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