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

Subversion Repositories t6507lp

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

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

powered by: WebSVN 2.1.0

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