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

Subversion Repositories t6507lp

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

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

Line No. Rev Author Line
1 128 gabrielosh
////////////////////////////////////////////////////////////////////////////
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
//// - Fix relative mode, bit 7 means negative                          ////
13
//// - Check reset behavior                                             ////
14
//// - Comment the code                                                 ////
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
`include "timescale.v"
48
 
49
module t6507lp_fsm(clk, reset_n, alu_result, alu_status, data_in, alu_x, alu_y, address, mem_rw, data_out, alu_opcode, alu_a, alu_enable);
50
        parameter [3:0] DATA_SIZE = 4'd8;
51
        parameter [3:0] ADDR_SIZE = 4'd13;
52
 
53
        localparam [3:0] DATA_SIZE_ = DATA_SIZE - 4'b0001;
54
        localparam [3:0] ADDR_SIZE_ = ADDR_SIZE - 4'b0001;
55
 
56
        input clk;                              // master clock
57
        input reset_n;                          // active low reset
58
        input [DATA_SIZE_:0] alu_result; // result from alu operation
59
        input [DATA_SIZE_:0] alu_status; // alu status register
60
        input [DATA_SIZE_:0] data_in;            // data that comes from the bus controller
61
        input [DATA_SIZE_:0] alu_x;              // alu x index register
62
        input [DATA_SIZE_:0] alu_y;              // alu y index register
63
        output reg [ADDR_SIZE_:0] address;       // system bus address
64
        output reg mem_rw;                      // read = 0, write = 1
65
        output reg [DATA_SIZE_:0] data_out;      // data that will be written somewhere else
66
        output reg [DATA_SIZE_:0] alu_opcode;    // current opcode
67
        output reg [DATA_SIZE_:0] alu_a; // extra operand sent to the alu
68
        output reg alu_enable;                  // a flag that when high tells the alu when to perform the operations
69
 
70
 
71
        // FSM states. If aiming for less power consumption try gray coding.
72
        //localparam FETCH_OP_CALC = 5'b00001; this was never used
73
        localparam FETCH_OP = 5'b00000;
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
        localparam PUSH_PCH = 5'b10000;
89
        localparam PUSH_PCL = 5'b10001;
90
        localparam PUSH_STATUS = 5'b10010;
91
        localparam FETCH_PCL = 5'b10011;
92
        localparam FETCH_PCH = 5'b10100;
93
        localparam INCREMENT_SP = 5'b10101;
94
        localparam PULL_STATUS = 5'b10110;
95
        localparam PULL_PCL = 5'b10111;
96
        localparam PULL_PCH = 5'b11000;
97
        localparam INCREMENT_PC = 5'b11001;
98
        localparam PUSH_REGISTER = 5'b11010;
99
        localparam PULL_REGISTER = 5'b11011;
100
        localparam DUMMY = 5'b11100;
101
        localparam RESET = 5'b11111;
102
 
103
        // OPCODES TODO: verify how this get synthesised
104 146 creep
        `include "t6507lp_package.v"
105 128 gabrielosh
 
106
        // mem_rw signals
107
        localparam MEM_READ = 1'b0;
108
        localparam MEM_WRITE = 1'b1;
109
 
110
        reg [ADDR_SIZE_:0] pc;           // program counter
111
        reg [DATA_SIZE:0] sp;            // stack pointer. 9 bits wide.
112
        reg [DATA_SIZE_:0] ir;           // instruction register
113
        reg [ADDR_SIZE_:0] temp_addr;    // temporary address
114
        reg [DATA_SIZE_:0] temp_data;    // temporary data
115
 
116
        reg [4:0] state, next_state; // current and next state registers
117
 
118
        // wiring that simplifies the FSM logic by simplifying the addressing modes
119
        reg absolute;
120
        reg absolute_indexed;
121
        reg accumulator;
122
        reg immediate;
123
        reg implied;
124
        reg indirectx;
125
        reg indirecty;
126
        reg relative;
127
        reg zero_page;
128
        reg zero_page_indexed;
129
        reg [DATA_SIZE_:0] index; // will be assigned with either X or Y
130
 
131
        // regs that store the type of operation. again, this simplifies the FSM a lot.
132
        reg read;
133
        reg read_modify_write;
134
        reg write;
135
        reg jump;
136
        reg jump_indirect;
137 212 creep
        reg index_is_x;
138
        reg index_is_branch;
139 128 gabrielosh
 
140
        // regs for the special instructions
141
        reg brk;
142
        reg rti;
143
        reg rts;
144
        reg pha;
145
        reg php;
146 212 creep
        reg pla;
147 128 gabrielosh
        reg plp;
148 194 creep
        reg jsr;
149
        reg tsx;
150 205 creep
        reg txs;
151 212 creep
        reg nop;
152 128 gabrielosh
 
153
        wire [ADDR_SIZE_:0] next_pc;      // a simple logic to add one to the PC
154
        assign next_pc = pc + 13'b0000000000001;
155
 
156 200 creep
        wire [DATA_SIZE:0] sp_plus_one;          // simple adder and subtracter for the stack pointer
157
        assign sp_plus_one = {1'b1, sp[7:0] + 8'b000000001};
158 128 gabrielosh
 
159 200 creep
        wire [DATA_SIZE:0] sp_minus_one;
160
        assign sp_minus_one = {1'b1, sp[7:0] - 8'b000000001};
161 128 gabrielosh
 
162
        reg [ADDR_SIZE_:0] address_plus_index;   // this two registers are used when the instruction uses indexing.
163
        reg page_crossed;                       // address_plus_index always adds index to address and page_crossed asserts when the sum creates a carry.
164
 
165
        reg branch;     // a simple reg that is asserted everytime a branch will be executed.                   
166
 
167
        // this is the combinational logic related to indexed instructions
168
        always @(*) begin
169 200 creep
                address_plus_index = 13'h000;
170 128 gabrielosh
                page_crossed = 1'b0;
171
 
172 212 creep
                case (state)
173
                        READ_MEM_FIX_ADDR, FETCH_HIGH_CALC_INDEX, READ_FROM_POINTER_X1: begin
174
                                {page_crossed, address_plus_index[7:0]} = temp_addr[7:0] + index;
175
                                address_plus_index[12:8] = temp_addr[12:8] + page_crossed;
176 128 gabrielosh
                        end
177 212 creep
 
178
                        FETCH_OP_FIX_PC, FETCH_OP_EVAL_BRANCH: begin
179
                                if (branch) begin
180
                                        {page_crossed, address_plus_index[7:0]} = pc[7:0] + index;
181
                                        address_plus_index[12:8] = pc[12:8] + page_crossed;
182
                                        // warning: pc might feed these lines twice and cause branch failure
183
                                end     // solution: add a temp reg i guess
184 128 gabrielosh
                        end
185 212 creep
 
186
                        READ_FROM_POINTER: begin
187
                                if (indirectx) begin
188
                                        {page_crossed, address_plus_index[7:0]} = temp_data + index;
189
                                        //address_plus_index[12:8] = 5'b00000; // already assigned earlier at this block
190
                                end
191
                                else if (jump_indirect) begin
192
                                        address_plus_index[7:0] = temp_addr[7:0] + 8'h01;
193
                                        //address_plus_index[12:8] = 5'b00000;
194
                                end
195
                                else begin // indirecty falls here
196
                                        address_plus_index[7:0] = temp_data + 8'h01;
197
                                        //address_plus_index[12:8] = 5'b00000;
198
                                end
199 128 gabrielosh
                        end
200 212 creep
 
201
                        READ_FROM_POINTER_X: begin
202
                                {page_crossed, address_plus_index[7:0]} = temp_data + index + 8'h01;
203
                                //address_plus_index[12:8] = 5'b00000;
204
                        end
205
 
206
                        READ_MEM_CALC_INDEX: begin
207
                                {page_crossed, address_plus_index[7:0]} = temp_addr[7:0] + index;
208
                                //address_plus_index[12:8] = 5'b00000;
209
                        end
210
                endcase
211 128 gabrielosh
        end
212
 
213 146 creep
        reg [2:0] rst_counter; // a counter to preserve the cpu idle for six cycles
214
 
215 128 gabrielosh
        always @ (posedge clk or negedge reset_n) begin // sequencial always block
216
                if (reset_n == 1'b0) begin
217
                        // all registers must assume default values
218 200 creep
                        pc <= 13'h0; // TODO: this is written somewhere. something about a reset vector. must be checked.
219 194 creep
                        sp <= 9'b111111111; // the default is 'h1FF 
220 128 gabrielosh
                        ir <= 8'h00;
221
                        temp_addr <= 13'h0000;
222
                        temp_data <= 8'h00;
223
                        state <= RESET;
224
                        // registered outputs also receive default values
225
                        address <= 13'h0000;
226
                        mem_rw <= MEM_READ;
227
                        data_out <= 8'h00;
228 200 creep
                        rst_counter <= 3'h0;
229 212 creep
                        index <= 8'h00;
230 128 gabrielosh
                end
231
                else begin
232
                        state <= next_state;
233 146 creep
 
234 128 gabrielosh
                        case (state)
235
                                RESET: begin    // The processor was reset
236 200 creep
                                        rst_counter <= rst_counter + 3'b001;
237 194 creep
                                        //sp <= 9'b111111111; // this prevents flipflops with different drivers
238 128 gabrielosh
                                        //$write("under reset"); 
239
                                end
240
                                /*
241
                                FETCH_OP: executed when the processor was reset or the last instruction could not fetch.
242
                                FETCH_OP_CALC_PARAM: enables the alu with an argument (alu_a) and fetchs the next instruction opcode. (pipelining)
243
                                */
244
                                FETCH_OP, FETCH_OP_CALC_PARAM: begin // this is the pipeline happening!
245
                                        pc <= next_pc;
246
                                        address <= next_pc;
247
                                        mem_rw <= MEM_READ;
248
                                        ir <= data_in;
249
                                end
250
                                /*
251
                                in this state the opcode is already known so truly execution begins.
252
                                all instructions execute this cycle.
253
                                */
254 196 creep
                                FETCH_LOW: begin
255 212 creep
                                        //$display("index_is_x = %b",index_is_x);
256
                                        if (index_is_x == 1'b1) begin
257
                                                index <= alu_x;
258
                                                //$display("alu_x = %d",alu_x);
259
                                        end
260
                                        else begin
261
                                                index <= alu_y;
262
                                                //$display("alu_y = %d",alu_y);
263
                                        end
264
                                        if (index_is_branch) begin
265
                                                index <= temp_data;
266
                                        end
267 195 creep
                                        if (accumulator || implied || txs || tsx) begin
268 128 gabrielosh
                                                pc <= pc; // is this better?
269
                                                address <= pc;
270 194 creep
                                                mem_rw <= MEM_READ;
271
 
272
                                                if (txs) begin
273 196 creep
                                                        sp[7:0] <= alu_x;
274 194 creep
                                                end
275
                                                //alu_a
276 128 gabrielosh
                                        end
277
                                        else if (immediate || relative) begin
278
                                                pc <= next_pc;
279
                                                address <= next_pc;
280
                                                mem_rw <= MEM_READ;
281
                                                temp_data <= data_in; // the follow-up byte is saved in temp_data 
282
                                        end
283
                                        else if (absolute || absolute_indexed || jump_indirect) begin
284
                                                pc <= next_pc;
285 196 creep
                                                address <= next_pc;
286 128 gabrielosh
                                                mem_rw <= MEM_READ;
287
                                                temp_addr <= {{5{1'b0}},data_in};
288
                                                temp_data <= 8'h00;
289
                                        end
290
                                        else if (zero_page) begin
291
                                                pc <= next_pc;
292
                                                address <= {{5{1'b0}},data_in};
293
                                                temp_addr <= {{5{1'b0}},data_in};
294
 
295
                                                if (write) begin
296
                                                        mem_rw <= MEM_WRITE;
297
                                                        data_out <= alu_result;
298
                                                end
299
                                                else begin
300
                                                        mem_rw <= MEM_READ;
301
                                                        data_out <= 8'h00;
302
                                                end
303
                                        end
304
                                        else if (zero_page_indexed) begin
305
                                                pc <= next_pc;
306
                                                address <= {{5{1'b0}}, data_in};
307
                                                temp_addr <= {{5{1'b0}}, data_in};
308
                                                mem_rw <= MEM_READ;
309
                                        end
310
                                        else if (indirectx || indirecty) begin
311
                                                pc <= next_pc;
312
                                                address <= data_in;
313
                                                temp_data <= data_in;
314
                                                mem_rw <= MEM_READ;
315
                                        end
316
                                        else begin // the special instructions will fall here: BRK, RTI, RTS...
317
                                                if (brk) begin
318
                                                        pc <= next_pc;
319
                                                        address <= sp;
320
                                                        data_out <= {{3{1'b0}}, pc[12:8]};
321
                                                        mem_rw <= MEM_WRITE;
322
                                                end
323
                                                else if (rti || rts) begin
324
                                                        address <= sp;
325
                                                        mem_rw <= MEM_READ;
326
                                                end
327
                                                else if (pha || php) begin
328
                                                        pc <= pc;
329
                                                        address <= sp;
330
                                                        data_out <= (pha) ? alu_result : alu_status;
331
                                                        mem_rw <= MEM_WRITE;
332
                                                end
333
                                                else if (pla || plp) begin
334
                                                        pc <= pc;
335
                                                        address <= sp;
336
                                                        mem_rw <= MEM_READ;
337
                                                end
338
                                                else begin // jsr
339
                                                        address <= sp;
340
                                                        mem_rw <= MEM_READ;
341
                                                        temp_addr <= {{5{1'b0}}, data_in};
342
                                                        pc <= next_pc;
343
                                                end
344
                                        end
345
                                end
346
                                FETCH_HIGH_CALC_INDEX: begin
347
                                        pc <= next_pc;
348
                                        temp_addr[12:8] <= data_in[4:0];
349
                                        address <= {data_in[4:0], address_plus_index[7:0]};
350
                                        mem_rw <= MEM_READ;
351
                                        data_out <= 8'h00;
352
                                end
353
                                // this cycle fetchs the next operand while still evaluating if a branch occurred.
354
                                FETCH_OP_EVAL_BRANCH: begin
355
                                        if (branch) begin
356
                                                pc <= {{5{1'b0}}, address_plus_index[7:0]};
357
                                                address <= {{5{1'b0}}, address_plus_index[7:0]};
358
                                                mem_rw <= MEM_READ;
359
                                                data_out <= 8'h00;
360
                                        end
361
                                        else begin
362
                                                pc <= next_pc;
363
                                                address <= next_pc;
364
                                                mem_rw <= MEM_READ;
365
                                                data_out <= 8'h00;
366
                                                ir <= data_in;
367
                                        end
368
                                end
369
                                // sometimes when reading memory page crosses may occur. the pc register must be fixed, i.e., add 16'h0100
370
                                FETCH_OP_FIX_PC: begin
371
                                        if (page_crossed) begin
372
                                                pc[12:8] <= address_plus_index[12:8];
373
                                                address[12:8] <= address_plus_index[12:8];
374
                                        end
375
                                        else begin
376
                                                pc <= next_pc;
377
                                                address <= next_pc;
378
                                                mem_rw <= MEM_READ;
379
                                                ir <= data_in;
380
                                        end
381
                                end
382
                                // several instructions ocupy 3 bytes in memory. this cycle reads the third byte.
383
                                FETCH_HIGH: begin
384
                                        if (jump) begin
385
                                                pc <= {data_in[4:0], temp_addr[7:0]}; // PCL <= first byte, PCH <= second byte
386
                                                address <= {data_in[4:0], temp_addr[7:0]};
387
                                                mem_rw <= MEM_READ;
388
                                                data_out <= 8'h00;
389
                                        end
390
                                        else begin
391
                                                if (write) begin
392
                                                        pc <= next_pc;
393
                                                        temp_addr[12:8] <= data_in[4:0];
394
                                                        address <= {data_in[4:0],temp_addr[7:0]};
395
                                                        mem_rw <= MEM_WRITE;
396
                                                        data_out <= alu_result;
397
                                                end
398
                                                else begin // read_modify_write or just read
399
                                                        pc <= next_pc;
400
                                                        temp_addr[12:8] <= data_in[4:0];
401
                                                        address <= {data_in[4:0],temp_addr[7:0]};
402
                                                        mem_rw <= MEM_READ;
403
                                                        data_out <= 8'h00;
404
                                                end
405
                                        end
406
                                end
407
                                // read memory at address
408
                                READ_MEM: begin
409
                                        if (read_modify_write) begin
410
                                                pc <= pc;
411
                                                address <= temp_addr;
412
                                                mem_rw <= MEM_WRITE;
413
                                                temp_data <= data_in;
414
                                                data_out <= data_in; // writeback the same value
415
                                        end
416
                                        else begin
417
                                                pc <= pc;
418
                                                address <= pc;
419
                                                temp_data <= data_in;
420
                                                mem_rw <= MEM_READ;
421
                                                data_out <= 8'h00;
422
                                        end
423
                                end
424
                                READ_MEM_CALC_INDEX: begin
425
                                                address <= address_plus_index;
426
                                                temp_addr <= address_plus_index;
427
 
428
                                                if (write) begin
429
                                                        mem_rw <= MEM_WRITE;
430
                                                        data_out <= alu_result;
431
                                                end
432
                                                else begin
433
                                                        mem_rw <= MEM_READ;
434
                                                        data_out <= 8'h00;
435
                                                end
436
 
437
                                end
438
                                READ_MEM_FIX_ADDR: begin
439
                                        if (read) begin
440
                                                mem_rw <= MEM_READ;
441
                                                data_out <= 8'h00;
442
 
443
                                                if (page_crossed) begin // fix address 
444
                                                        address <= address_plus_index;
445
                                                        temp_addr <= address_plus_index;
446
                                                end
447
                                                else begin
448
                                                        address <= pc;
449
                                                        temp_data <= data_in;
450
                                                end
451
                                        end
452
                                        else if (write) begin
453
                                                mem_rw <= MEM_WRITE;
454
                                                data_out <= alu_result;
455
                                                address <= address_plus_index;
456
                                                temp_addr <= address_plus_index;
457
 
458
                                        end
459
                                        else begin // read modify write
460
                                                mem_rw <= MEM_READ;
461
                                                data_out <= 8'h00;
462
                                                address <= address_plus_index;
463
                                                temp_addr <= address_plus_index;
464
                                        end
465
                                end
466
                                // some instructions have a dummy write cycle. this is it.
467
                                DUMMY_WRT_CALC: begin
468
                                        pc <= pc;
469
                                        address <= temp_addr;
470
                                        mem_rw <= MEM_WRITE;
471
                                        data_out <= alu_result;
472
                                end
473
                                WRITE_MEM: begin
474
                                        pc <= pc;
475
                                        address <= pc;
476
                                        mem_rw <= MEM_READ;
477
                                        data_out <= 8'h00;
478
                                end
479
                                READ_FROM_POINTER: begin
480
                                        if (jump_indirect) begin
481
                                                pc[7:0] <= data_in;
482
                                                mem_rw <= MEM_READ;
483
                                                address <= address_plus_index;
484
                                        end
485
                                        else begin
486
                                                pc <= pc;
487
                                                mem_rw <= MEM_READ;
488
 
489
                                                if (indirectx) begin
490
                                                        address <= address_plus_index;
491
                                                end
492
                                                else begin // indirecty falls here
493
                                                        address <= address_plus_index;
494
                                                        temp_addr <= {{5{1'b0}}, data_in};
495
                                                end
496
                                        end
497
                                end
498
                                READ_FROM_POINTER_X: begin
499
                                        pc <= pc;
500
                                        address <= address_plus_index;
501
                                        temp_addr[7:0] <= data_in;
502
                                        mem_rw <= MEM_READ;
503
                                end
504
                                READ_FROM_POINTER_X1: begin
505
                                        if (jump_indirect) begin
506
                                                pc[12:8] <= data_in[4:0];
507
                                                mem_rw <= MEM_READ;
508
                                                address <= {data_in[4:0], pc[7:0]};
509
                                        end
510
                                        else if (indirectx) begin
511
                                                address <= {data_in[4:0], temp_addr[7:0]};
512
                                                if (write) begin
513
                                                        mem_rw <= MEM_WRITE;
514
                                                        data_out <= alu_result;
515
                                                end
516
                                                else begin
517
                                                        mem_rw <= MEM_READ;
518
                                                end
519
                                        end
520
                                        else begin // indirecty falls here
521
                                                address <= address_plus_index;
522
                                                temp_addr[12:8] <= data_in;
523
                                                mem_rw <= MEM_READ;
524
                                        end
525
                                end
526
                                PUSH_PCH: begin
527
                                        pc <= pc;
528
                                        address <= sp_minus_one;
529
                                        data_out <= pc[7:0];
530
                                        mem_rw <= MEM_WRITE;
531
                                        sp <= sp_minus_one;
532
                                end
533
                                PUSH_PCL: begin
534
                                        if (jsr) begin
535
                                                pc <= pc;
536
                                                address <= pc;
537
                                                mem_rw <= MEM_READ;
538
                                                sp <= sp_minus_one;
539
                                        end
540
                                        else begin
541
                                                pc <= pc;
542
                                                address <= sp_minus_one;
543
                                                data_out <= alu_status;
544
                                                mem_rw <= MEM_WRITE;
545
                                                sp <= sp_minus_one;
546
                                        end
547
                                end
548
                                PUSH_STATUS: begin
549 199 creep
                                        address <= 13'h1FFE;
550 128 gabrielosh
                                        mem_rw <= MEM_READ;
551 196 creep
                                        sp <= sp_minus_one;
552 128 gabrielosh
                                end
553
                                FETCH_PCL: begin
554
                                        pc[7:0] <= data_in;
555 199 creep
                                        address <= 13'h1FFF;
556 128 gabrielosh
                                        mem_rw <= MEM_READ;
557
                                end
558
                                FETCH_PCH: begin
559
                                        pc[12:8] <= data_in[4:0];
560
                                        address <= {data_in[4:0], pc[7:0]};
561
                                        mem_rw <= MEM_READ;
562
                                end
563
                                INCREMENT_SP: begin
564
                                        sp <= sp_plus_one;
565
                                        address <= sp_plus_one;
566
                                end
567
                                PULL_STATUS: begin
568
                                        sp <= sp_plus_one;
569
                                        address <= sp_plus_one;
570
                                        temp_data <= data_in;
571
                                end
572
                                PULL_PCL: begin
573
                                        sp <= sp_plus_one;
574
                                        address <= sp_plus_one;
575
                                        pc[7:0] <= data_in;
576
                                end
577
                                PULL_PCH: begin
578
                                        pc[12:8] <= data_in[4:0];
579
                                        address <= {data_in[4:0], pc[7:0]};
580
                                end
581
                                INCREMENT_PC: begin
582
                                        pc <= next_pc;
583
                                        address <= next_pc;
584
                                end
585
                                PUSH_REGISTER: begin
586
                                        pc <= pc;
587
                                        address <= pc;
588
                                        sp <= sp_minus_one;
589
                                        mem_rw <= MEM_READ;
590
                                        temp_data <= data_in;
591
                                end
592
                                PULL_REGISTER: begin
593
                                        pc <= pc;
594
                                        address <= pc;
595
                                        temp_data <= data_in;
596
                                end
597
                                DUMMY: begin
598
                                        address <= sp;
599
                                        mem_rw <= MEM_WRITE;
600
                                end
601
                                default: begin
602
                                        //$write("unknown state"); // TODO: check if synth really ignores this 2 lines. Otherwise wrap it with a `ifdef 
603
                                        //$finish(0); 
604
                                end
605
 
606
                        endcase
607
                end
608
        end
609
 
610
        always @ (*) begin // this is the next_state logic and the combinational output logic always block
611
                alu_opcode = 8'h00;
612
                alu_a = 8'h00;
613
                alu_enable = 1'b0;
614
                next_state = RESET; // these lines prevents latches
615
 
616
                case (state)
617
                        RESET: begin
618 200 creep
                                if (rst_counter == 3'd6) begin
619 146 creep
                                        next_state = FETCH_OP;
620
                                end
621 128 gabrielosh
                        end
622
                        FETCH_OP: begin
623
                                next_state = FETCH_LOW;
624
                        end
625
                        FETCH_OP_CALC_PARAM: begin
626
                                next_state = FETCH_LOW;
627
                                alu_opcode = ir;
628
                                alu_enable = 1'b1;
629
                                alu_a = temp_data;
630
                        end
631
                        FETCH_LOW: begin
632 195 creep
                                if (accumulator  || implied || txs) begin
633 205 creep
                                        if (!nop) begin
634
                                                alu_opcode = ir;
635
                                                alu_enable = 1'b1;
636
                                        end
637 194 creep
                                        next_state = FETCH_OP;
638 128 gabrielosh
                                end
639 195 creep
                                else if (tsx) begin
640
                                        alu_opcode = ir;
641
                                        alu_enable = 1'b1;
642
                                        next_state = FETCH_OP;
643
                                        alu_a = sp[7:0];
644
                                end
645 128 gabrielosh
                                else if (immediate) begin
646
                                        next_state = FETCH_OP_CALC_PARAM;
647
                                end
648
                                else if (zero_page) begin
649
                                        if (read || read_modify_write) begin
650
                                                next_state = READ_MEM;
651
                                        end
652
                                        else if (write) begin
653
                                                next_state = WRITE_MEM;
654
                                                alu_opcode = ir;
655
                                                alu_enable = 1'b1;
656
                                                alu_a = 8'h00;
657
                                        end
658
                                        else begin
659
                                                //$write("unknown behavior"); 
660
                                                //$finish(0);
661
                                        end
662
                                end
663
                                else if (zero_page_indexed) begin
664
                                        next_state = READ_MEM_CALC_INDEX;
665
                                end
666
                                else if (absolute || jump_indirect) begin
667
                                        next_state = FETCH_HIGH;
668
                                        if (write) begin // this is being done one cycle early but i have checked and the ALU will still work properly
669
                                                alu_opcode = ir;
670
                                                alu_enable = 1'b1;
671
                                                alu_a = 8'h00;
672
                                        end
673
                                end
674
                                else if (absolute_indexed) begin
675
                                        next_state = FETCH_HIGH_CALC_INDEX;
676
                                end
677
                                else if (relative) begin
678
                                        next_state = FETCH_OP_EVAL_BRANCH;
679
                                end
680
                                else if (indirectx || indirecty) begin
681
                                        next_state = READ_FROM_POINTER;
682
                                end
683
                                else begin // all the special instructions will fall here
684
                                        if (brk) begin
685
                                                next_state = PUSH_PCH;
686
                                        end
687
                                        else if (rti || rts) begin
688
                                                next_state = INCREMENT_SP;
689
                                        end
690
                                        else if (pha) begin
691
                                                alu_opcode = ir;
692
                                                alu_enable = 1'b1;
693
                                                //alu_a = 8'h00;
694
                                                next_state = PUSH_REGISTER;
695
                                        end
696
                                        else if (php) begin
697
                                                next_state = PUSH_REGISTER;
698
                                        end
699
                                        else if (pla || plp) begin
700
                                                next_state = INCREMENT_SP;
701
                                        end
702
                                        else begin // jsr
703
                                                next_state = DUMMY;
704
                                        end
705
                                end
706
                        end
707
                        READ_FROM_POINTER: begin
708
                                if (indirectx) begin
709
                                        next_state = READ_FROM_POINTER_X;
710
                                end
711
                                else begin // indirecty and jump indirect falls here
712
                                        next_state = READ_FROM_POINTER_X1;
713
                                end
714
                        end
715
                        READ_FROM_POINTER_X: begin
716
                                next_state = READ_FROM_POINTER_X1;
717
                        end
718
                        READ_FROM_POINTER_X1: begin
719
                                if (jump_indirect) begin
720
                                        next_state = FETCH_OP;
721
                                end
722
                                else if (indirecty) begin
723
                                        next_state = READ_MEM_FIX_ADDR;
724
                                end
725
                                else begin
726
                                        if (read) begin // no instruction using pointers is from type read_modify_write
727
                                                next_state = READ_MEM;
728
                                        end
729
                                        else if (write) begin
730
                                                alu_opcode = ir;
731
                                                alu_enable = 1'b1;
732
                                                next_state = WRITE_MEM;
733
                                        end
734
                                end
735
                        end
736
                        FETCH_OP_EVAL_BRANCH: begin
737
                                if (branch) begin
738
                                        next_state = FETCH_OP_FIX_PC;
739
                                end
740
                                else begin
741
                                        next_state = FETCH_LOW;
742
                                end
743
                        end
744
                        FETCH_OP_FIX_PC: begin
745
                                if (page_crossed) begin
746
                                        next_state = FETCH_OP;
747
                                end
748
                                else begin
749
                                        next_state = FETCH_LOW;
750
                                end
751
                        end
752
                        FETCH_HIGH_CALC_INDEX: begin
753
                                next_state = READ_MEM_FIX_ADDR;
754
                        end
755
                        READ_MEM_FIX_ADDR: begin
756
                                if (read) begin
757
                                        if (page_crossed) begin
758
                                                next_state = READ_MEM;
759
                                        end
760
                                        else begin
761
                                                next_state = FETCH_OP_CALC_PARAM;
762
                                        end
763
                                end
764
                                else if (read_modify_write) begin
765
                                        next_state = READ_MEM;
766
                                end
767
                                else if (write) begin
768
                                        next_state = WRITE_MEM;
769
                                        alu_enable = 1'b1;
770
                                        alu_opcode = ir;
771
                                end
772
                                else begin
773
                                        //$write("unknown behavior"); 
774
                                        //$finish(0);
775
                                end
776
                        end
777
                        FETCH_HIGH: begin
778
                                if (jump_indirect) begin
779
                                        next_state = READ_FROM_POINTER;
780
                                end
781
                                else if (jump) begin
782
                                        next_state = FETCH_OP;
783
                                end
784
                                else if (read || read_modify_write) begin
785
                                        next_state = READ_MEM;
786
                                end
787
                                else if (write) begin
788
                                        next_state = WRITE_MEM;
789
                                end
790
                                else begin
791
                                        //$write("unknown behavior"); 
792
                                        //$finish(0);
793
                                end
794
                        end
795
                        READ_MEM_CALC_INDEX: begin
796
                                if (read || read_modify_write) begin
797
                                        next_state = READ_MEM;
798
                                end
799
                                else if (write) begin
800
                                        alu_opcode = ir;
801
                                        alu_enable = 1'b1;
802
                                        next_state = WRITE_MEM;
803
                                end
804
                                else begin
805
                                        //$write("unknown behavior"); 
806
                                        //$finish(0);
807
                                end
808
                        end
809
                        READ_MEM: begin
810
                                if (read) begin
811
                                        next_state = FETCH_OP_CALC_PARAM;
812
                                end
813
                                else if (read_modify_write) begin
814
                                        next_state = DUMMY_WRT_CALC;
815
                                end
816
                        end
817
                        DUMMY_WRT_CALC: begin
818
                                alu_opcode = ir;
819
                                alu_enable = 1'b1;
820
                                alu_a = data_in;
821
                                next_state = WRITE_MEM;
822
                        end
823
                        WRITE_MEM: begin
824
                                next_state = FETCH_OP;
825
                        end
826
                        PUSH_PCH: begin
827
                                next_state = PUSH_PCL;
828
                        end
829
                        PUSH_PCL: begin
830
                                if (jsr) begin
831
                                        next_state = FETCH_HIGH;
832
                                end
833
                                else begin
834
                                        next_state = PUSH_STATUS;
835
                                end
836
                        end
837
                        PUSH_STATUS: begin
838
                                next_state = FETCH_PCL;
839
                        end
840
                        FETCH_PCL: begin
841
                                next_state = FETCH_PCH;
842
                        end
843
                        FETCH_PCH: begin
844
                                next_state = FETCH_OP;
845
                        end
846
                        INCREMENT_SP: begin
847
                                if (rti) begin
848
                                        next_state = PULL_STATUS;
849
                                end
850
                                else if (pla || plp) begin
851
                                        next_state = PULL_REGISTER;
852
                                end
853
                                else begin // rts
854
                                        next_state = PULL_PCL;
855
                                end
856
                        end
857
                        PULL_STATUS: begin
858
                                next_state = PULL_PCL;
859
                        end
860
                        PULL_PCL: begin
861
                                next_state = PULL_PCH;
862 202 creep
 
863
                                if (rti) begin
864
                                        alu_opcode = ir;
865
                                        alu_enable = 1'b1;
866
                                        alu_a = temp_data;
867
                                end
868 128 gabrielosh
                        end
869
                        PULL_PCH: begin
870
                                if (rti) begin
871
                                        next_state = FETCH_OP;
872
                                end
873
                                else begin // rts
874
                                        next_state = INCREMENT_PC;
875
                                end
876
                        end
877
                        INCREMENT_PC: begin
878
                                next_state = FETCH_OP;
879
                        end
880
                        PUSH_REGISTER: begin
881
                                next_state = FETCH_OP;
882
                        end
883
                        PULL_REGISTER: begin
884
                                next_state = FETCH_OP_CALC_PARAM;
885
                        end
886
                        DUMMY: begin
887
                                next_state = PUSH_PCH;
888
                        end
889
                        default: begin
890
                                next_state = RESET;
891
                        end
892
                endcase
893
        end
894
 
895
        // this always block is responsible for updating the address mode and the type of operation being done
896
        always @ (*) begin // 
897
                absolute = 1'b0;
898
                absolute_indexed = 1'b0;
899
                accumulator = 1'b0;
900
                immediate = 1'b0;
901
                implied = 1'b0;
902
                indirectx = 1'b0;
903
                indirecty = 1'b0;
904
                relative = 1'b0;
905
                zero_page = 1'b0;
906
                zero_page_indexed = 1'b0;
907 212 creep
                //index_is_x = 1'b1;
908
                index_is_branch = 1'b0;
909 128 gabrielosh
 
910 212 creep
                //index = 8'h00;
911 128 gabrielosh
 
912
                read = 1'b0;
913
                read_modify_write = 1'b0;
914
                write = 1'b0;
915
                jump = 1'b0;
916
                jump_indirect = 1'b0;
917
                branch = 1'b0;
918
 
919
                brk = 1'b0;
920
                rti = 1'b0;
921
                rts = 1'b0;
922
                pha = 1'b0;
923
                php = 1'b0;
924
                pla = 1'b0;
925
                plp = 1'b0;
926
                jsr = 1'b0;
927 194 creep
                tsx = 1'b0;
928
                txs = 1'b0;
929 205 creep
                nop = 1'b0;
930 128 gabrielosh
 
931
                case (ir)
932 205 creep
                        CLC_IMP, CLD_IMP, CLI_IMP, CLV_IMP, DEX_IMP, DEY_IMP, INX_IMP, INY_IMP, SEC_IMP, SED_IMP, SEI_IMP, TAX_IMP,
933
                        TAY_IMP, TXA_IMP, TYA_IMP: begin
934 128 gabrielosh
                                implied = 1'b1;
935
                        end
936 205 creep
                        NOP_IMP: begin
937
                                implied = 1'b1;
938
                                nop = 1'b1;
939
                        end
940 128 gabrielosh
                        ASL_ACC, LSR_ACC, ROL_ACC, ROR_ACC: begin
941
                                accumulator = 1'b1;
942
                        end
943
                        ADC_IMM, AND_IMM, CMP_IMM, CPX_IMM, CPY_IMM, EOR_IMM, LDA_IMM, LDX_IMM, LDY_IMM, ORA_IMM, SBC_IMM: begin
944
                                immediate = 1'b1;
945
                        end
946
                        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,
947
                        LSR_ZPG, ORA_ZPG, ROL_ZPG, ROR_ZPG, SBC_ZPG, STA_ZPG, STX_ZPG, STY_ZPG: begin
948
                                zero_page = 1'b1;
949
                        end
950
                        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,
951
                        SBC_ZPX, STA_ZPX, STY_ZPX: begin
952
                                zero_page_indexed = 1'b1;
953 212 creep
                                index_is_x = 1'b1;
954
                                //index = alu_x;
955 128 gabrielosh
                        end
956
                        LDX_ZPY, STX_ZPY: begin
957
                                zero_page_indexed = 1'b1;
958 212 creep
                                index_is_x = 1'b0;
959
                                //index = alu_y;
960 128 gabrielosh
                        end
961
                        BCC_REL: begin
962
                                relative = 1'b1;
963 212 creep
                                index_is_branch = 1'b1;
964
                                //index = temp_data;
965 128 gabrielosh
 
966
                                if (!alu_status[C]) begin
967
                                        branch = 1'b1;
968
                                end
969
                                else begin
970
                                        branch = 1'b0;
971
                                end
972
                        end
973
                        BCS_REL: begin
974
                                relative = 1'b1;
975 212 creep
                                index_is_branch = 1'b1;
976
                                //index = temp_data;
977 128 gabrielosh
 
978
                                if (alu_status[C]) begin
979
                                        branch = 1'b1;
980
                                end
981
                                else begin
982
                                        branch = 1'b0;
983
                                end
984
                        end
985
                        BEQ_REL: begin
986
                                relative = 1'b1;
987 212 creep
                                index_is_branch = 1'b1;
988
                                //index = temp_data;
989 128 gabrielosh
 
990
                                if (alu_status[Z]) begin
991
                                        branch = 1'b1;
992
                                end
993
                                else begin
994
                                        branch = 1'b0;
995
                                end
996
                        end
997
                        BNE_REL: begin
998
                                relative = 1'b1;
999 212 creep
                                index_is_branch = 1'b1;
1000
                                //index = temp_data;
1001 128 gabrielosh
 
1002
                                if (alu_status[Z] == 1'b0) begin
1003
                                        branch = 1'b1;
1004
                                end
1005
                                else begin
1006
                                        branch = 1'b0;
1007
                                end
1008
                        end
1009
                        BPL_REL: begin
1010
                                relative = 1'b1;
1011 212 creep
                                index_is_branch = 1'b1;
1012
                                //index = temp_data;
1013 128 gabrielosh
 
1014
                                if (!alu_status[N]) begin
1015
                                        branch = 1'b1;
1016
                                end
1017
                                else begin
1018
                                        branch = 1'b0;
1019
                                end
1020
                        end
1021
                        BMI_REL: begin
1022
                                relative = 1'b1;
1023 212 creep
                                index_is_branch = 1'b1;
1024
                                //index = temp_data;
1025 128 gabrielosh
 
1026
                                if (alu_status[N]) begin
1027
                                        branch = 1'b1;
1028
                                end
1029
                                else begin
1030
                                        branch = 1'b0;
1031
                                end
1032
                        end
1033
                        BVC_REL: begin
1034
                                relative = 1'b1;
1035 212 creep
                                index_is_branch = 1'b1;
1036
                                //index = temp_data;
1037 128 gabrielosh
 
1038
                                if (!alu_status[V]) begin
1039
                                        branch = 1'b1;
1040
                                end
1041
                                else begin
1042
                                        branch = 1'b0;
1043
                                end
1044
                        end
1045
                        BVS_REL: begin
1046
                                relative = 1'b1;
1047 212 creep
                                index_is_branch = 1'b1;
1048
                                //index = temp_data;
1049 128 gabrielosh
 
1050
                                if (alu_status[V]) begin
1051
                                        branch = 1'b1;
1052
                                end
1053
                                else begin
1054
                                        branch = 1'b0;
1055
                                end
1056
                        end
1057
                        ADC_ABS, AND_ABS, ASL_ABS, BIT_ABS, CMP_ABS, CPX_ABS, CPY_ABS, DEC_ABS, EOR_ABS, INC_ABS, LDA_ABS,
1058
                        LDX_ABS, LDY_ABS, LSR_ABS, ORA_ABS, ROL_ABS, ROR_ABS, SBC_ABS, STA_ABS, STX_ABS, STY_ABS: begin
1059
                                absolute = 1'b1;
1060
                        end
1061
                        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,
1062
                        SBC_ABX, STA_ABX: begin
1063
                                absolute_indexed = 1'b1;
1064 212 creep
                                index_is_x = 1'b1;
1065
                                //index = alu_x;
1066 128 gabrielosh
                        end
1067
                        ADC_ABY, AND_ABY, CMP_ABY, EOR_ABY, LDA_ABY, LDX_ABY, ORA_ABY, SBC_ABY, STA_ABY: begin
1068
                                absolute_indexed = 1'b1;
1069 212 creep
                                index_is_x = 1'b0;
1070
                                //index = alu_y;
1071 128 gabrielosh
                        end
1072
                        ADC_IDX, AND_IDX, CMP_IDX, EOR_IDX, LDA_IDX, ORA_IDX, SBC_IDX, STA_IDX: begin
1073
                                indirectx = 1'b1;
1074 212 creep
                                index_is_x = 1'b1;
1075
                                //index = alu_x;
1076 128 gabrielosh
                        end
1077
                        ADC_IDY, AND_IDY, CMP_IDY, EOR_IDY, LDA_IDY, ORA_IDY, SBC_IDY, STA_IDY: begin
1078
                                indirecty = 1'b1;
1079 212 creep
                                index_is_x = 1'b0;
1080
                                //index = alu_y;        
1081 128 gabrielosh
                        end
1082
                        JMP_ABS: begin
1083
                                absolute = 1'b1;
1084
                                jump = 1'b1;
1085
                        end
1086
                        JMP_IND: begin
1087
                                jump_indirect = 1'b1;
1088
                        end
1089
                        BRK_IMP: begin
1090
                                brk = 1'b1;
1091
                        end
1092
                        RTI_IMP: begin
1093
                                rti = 1'b1;
1094
                        end
1095
                        RTS_IMP: begin
1096
                                rts = 1'b1;
1097
                        end
1098
                        PHA_IMP: begin
1099
                                pha = 1'b1;
1100
                        end
1101
                        PHP_IMP: begin
1102
                                php = 1'b1;
1103
                        end
1104
                        PLA_IMP: begin
1105
                                pla = 1'b1;
1106
                        end
1107
                        PLP_IMP: begin
1108
                                plp = 1'b1;
1109
                        end
1110
                        JSR_ABS: begin
1111
                                jsr = 1'b1;
1112
                        end
1113 194 creep
                        TSX_IMP: begin
1114
                                tsx = 1'b1;
1115
                        end
1116
                        TXS_IMP: begin
1117
                                txs = 1'b1;
1118
                        end
1119 128 gabrielosh
                        default: begin
1120 212 creep
                                index_is_x = 1'b1;
1121 128 gabrielosh
                                //$write("state : %b", state);
1122
                                if (reset_n == 1'b1 && state != FETCH_OP_FIX_PC) begin // the processor is NOT being reset neither it is fixing the pc
1123
                                        //$write("\nunknown OPCODE!!!!! 0x%h\n", ir);
1124
                                        //$finish();
1125
                                end
1126
                        end
1127
                endcase
1128
 
1129
                case (ir)
1130
                        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,
1131
                        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,
1132
                        DEC_ABX: begin
1133
                                read_modify_write = 1'b1;
1134
                        end
1135
                        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
1136
                                write = 1'b1;
1137
                        end
1138
                        default: begin // this should work fine since the previous case statement will detect the unknown/undocumented/unsupported opcodes
1139
                                read = 1'b1;
1140
                        end
1141
                endcase
1142
        end
1143
endmodule
1144
 
1145
 
1146
 

powered by: WebSVN 2.1.0

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