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

Subversion Repositories t6507lp

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

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

powered by: WebSVN 2.1.0

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