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

Subversion Repositories t6507lp

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

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

Line No. Rev Author Line
1 61 creep
////////////////////////////////////////////////////////////////////////////
2
////                                                                    ////
3
//// T6507LP IP Core                                                    ////
4
////                                                                    ////
5
//// This file is part of the T6507LP project                           ////
6
//// http://www.opencores.org/cores/t6507lp/                            ////
7
////                                                                    ////
8
//// Description                                                        ////
9
//// 6507 FSM                                                           ////
10
////                                                                    ////
11
//// TODO:                                                              ////
12
//// - Fix absolute indexed mode                                        ////
13
//// - Code the relative mode                                           ////
14
//// - Code the indexed indirect mode                                   ////
15
//// - Code the indirect indexed mode                                   ////
16
//// - Code the absolute indirect mode                                  ////
17
////                                                                    ////
18
//// Author(s):                                                         ////
19
//// - Gabriel Oshiro Zardo, gabrieloshiro@gmail.com                    ////
20
//// - Samuel Nascimento Pagliarini (creep), snpagliarini@gmail.com     ////
21
////                                                                    ////
22
////////////////////////////////////////////////////////////////////////////
23
////                                                                    ////
24
//// Copyright (C) 2001 Authors and OPENCORES.ORG                       ////
25
////                                                                    ////
26
//// This source file may be used and distributed without               ////
27
//// restriction provided that this copyright statement is not          ////
28
//// removed from the file and that any derivative work contains        ////
29
//// the original copyright notice and the associated disclaimer.       ////
30
////                                                                    ////
31
//// This source file is free software; you can redistribute it         ////
32
//// and/or modify it under the terms of the GNU Lesser General         ////
33
//// Public License as published by the Free Software Foundation;       ////
34
//// either version 2.1 of the License, or (at your option) any         ////
35
//// later version.                                                     ////
36
////                                                                    ////
37
//// This source is distributed in the hope that it will be             ////
38
//// useful, but WITHOUT ANY WARRANTY; without even the implied         ////
39
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR            ////
40
//// PURPOSE. See the GNU Lesser General Public License for more        ////
41
//// details.                                                           ////
42
////                                                                    ////
43
//// You should have received a copy of the GNU Lesser General          ////
44
//// Public License along with this source; if not, download it         ////
45
//// from http://www.opencores.org/lgpl.shtml                           ////
46
////                                                                    ////
47
////////////////////////////////////////////////////////////////////////////
48
 
49
`timescale 1ns / 1ps
50
 
51 71 creep
module t6507lp_fsm(clk, reset_n, alu_result, alu_status, data_in, address, control, data_out, alu_opcode, alu_a, alu_enable);
52 68 creep
        parameter DATA_SIZE = 4'd8;
53
        parameter ADDR_SIZE = 4'd13;
54
 
55 71 creep
        input clk;
56
        input reset_n;
57 68 creep
        input [DATA_SIZE-1:0] alu_result;
58
        input [DATA_SIZE-1:0] alu_status;
59
        input [DATA_SIZE-1:0] data_in;
60
        output reg [ADDR_SIZE-1:0] address;
61 61 creep
        output reg control; // one bit is enough? read = 0, write = 1
62 68 creep
        output reg [DATA_SIZE-1:0] data_out;
63
        output reg [DATA_SIZE-1:0] alu_opcode;
64
        output reg [DATA_SIZE-1:0] alu_a;
65
        output reg alu_enable;
66 61 creep
 
67 68 creep
 
68 61 creep
        // FSM states
69 68 creep
        localparam RESET = 4'b1111;
70 63 creep
        localparam FETCH_OP = 4'b0000;
71 68 creep
        localparam FETCH_OP_CALC = 4'b0001;
72
        localparam FETCH_LOW = 4'b0010;
73
        localparam FETCH_HIGH = 4'b0011;
74 71 creep
        localparam READ_MEM = 4'b0100;
75
        localparam DUMMY_WRT_CALC = 4'b0101;
76
        localparam WRITE_MEM = 4'b0110;
77
        localparam FETCH_OP_CALC_PARAM = 4'b0111;
78 61 creep
 
79
        // OPCODES TODO: verify how this get synthesised
80
        `include "../T6507LP_Package.v"
81
 
82
        // control signals
83
        localparam MEM_READ = 1'b0;
84
        localparam MEM_WRITE = 1'b1;
85
 
86 68 creep
        reg [ADDR_SIZE-1:0] pc;          // program counter
87
        reg [DATA_SIZE-1:0] sp;          // stack pointer
88
        reg [DATA_SIZE-1:0] ir;          // instruction register
89 71 creep
        reg [ADDR_SIZE:0] temp_addr;     // temporary address
90 68 creep
        reg [DATA_SIZE-1:0] temp_data;   // temporary data
91 61 creep
 
92
        reg [3:0] state, next_state; // current and next state registers
93
        // TODO: not sure if this will be 4 bits wide. as of march 9th this was 4bit wide.
94
 
95
        // wiring that simplifies the FSM logic
96
        reg absolute;
97
        reg absolute_indexed;
98
        reg accumulator;
99
        reg immediate;
100
        reg implied;
101
        reg indirect;
102
        reg relative;
103
        reg zero_page;
104
        reg zero_page_indexed;
105
 
106
        // regs that store the type of operation. again, this simplifies the FSM a lot.
107
        reg read;
108
        reg read_modify_write;
109
        reg write;
110
        reg jump;
111
 
112 68 creep
        wire [ADDR_SIZE-1:0] next_pc;
113 63 creep
        assign next_pc = pc + 13'b0000000000001;
114 61 creep
 
115 71 creep
        always @ (posedge clk or negedge reset_n) begin // sequencial always block
116
                if (reset_n == 1'b0) begin
117
                        // all registers must assume default values
118 68 creep
                        pc <= 0; // TODO: this is written somewhere. something about a reset vector. must be checked.
119
                        sp <= 0; // TODO: the default is not 0. maybe $0100 or something like that. must be checked.
120
                        ir <= 0;
121 71 creep
                        temp_addr <= 0;
122 68 creep
                        temp_data <= 0;
123
                        state <= RESET;
124 71 creep
                        // registered outputs also receive default values
125
                        address <= 0;
126
                        control <= 0; // check if these 2 shouldnt be on the other always block along with the address
127
                        data_out <= 0;
128 61 creep
                end
129
                else begin
130
                        state <= next_state;
131 71 creep
                        control <= MEM_READ;
132 77 creep
                        data_out = 8'hZ;
133 61 creep
                        case (state)
134 68 creep
                                RESET: begin
135
                                        // The processor was reset 
136
                                end
137 61 creep
                                FETCH_OP: begin // this state is the simplest one. it is a simple fetch that must be done when the cpu was reset or
138
                                                // the last cycle was a memory write.
139
                                        pc <= next_pc;
140 71 creep
                                        address <= next_pc;
141 70 creep
                                        ir <= data_in;
142 61 creep
                                end
143 71 creep
                                FETCH_OP_CALC, FETCH_OP_CALC_PARAM: begin // this is the pipeline happening!
144 61 creep
                                        pc <= next_pc;
145 71 creep
                                        address <= next_pc;
146 70 creep
                                        ir <= data_in;
147 61 creep
                                end
148 68 creep
                                FETCH_LOW: begin // in this state the opcode is already known so truly execution begins
149
                                        if (accumulator || implied) begin
150 70 creep
                                                pc <= pc; // is this better?
151 71 creep
                                                address <= pc;
152 61 creep
                                        end
153 70 creep
                                        else if (immediate) begin
154 68 creep
                                                pc <= next_pc;
155 71 creep
                                                address <= next_pc;
156 70 creep
                                                temp_data <= data_in; // the follow-up byte is saved in temp_data 
157 61 creep
                                        end
158 71 creep
                                        else if (absolute) begin
159
                                                pc <= next_pc;
160
                                                address <= next_pc;
161
                                                temp_addr[7:0] <= data_in;
162
                                        end
163 77 creep
                                        else if (zero_page) begin
164
                                                pc <= next_pc;
165
                                                address <= {{5{1'b0}},data_in};
166 78 creep
                                                temp_addr <= {{5{1'b0}},data_in};
167
 
168 77 creep
                                                if (write) begin
169
                                                        control <= MEM_WRITE;
170 78 creep
                                                        data_out <= alu_result;
171 77 creep
                                                end
172
                                        end
173 61 creep
                                end
174 71 creep
                                FETCH_HIGH: begin
175
                                        if (jump) begin
176
                                                pc <= {data_in[4:0], temp_addr}; // PCL <= first byte, PCH <= second byte
177
                                                address <= {data_in[4:0], temp_addr};
178
                                        end
179
                                        else begin
180
                                                if (write) begin
181
                                                        pc <= next_pc;
182
                                                        temp_addr[12:8] <= data_in[4:0];
183
                                                        address <= {data_in[4:0],temp_addr[7:0]};
184
                                                        control <= MEM_WRITE;
185 77 creep
                                                        data_out <= alu_result;
186 71 creep
                                                end
187
                                                else begin // read_modify_write or just read
188
                                                        pc <= next_pc;
189
                                                        temp_addr[12:8] <= data_in[4:0];
190
                                                        address <= {data_in[4:0],temp_addr[7:0]};
191
                                                end
192
                                        end
193
                                        //else begin
194
                                        //      $write("FETCHHIGH PROBLEM"); 
195
                                        //      $finish(0); 
196
                                        //end
197 61 creep
                                end
198 71 creep
                                READ_MEM: begin
199
                                        if (read_modify_write) begin
200
                                                pc <= pc;
201
                                                address <= temp_addr;
202
                                                control <= MEM_WRITE;
203
                                                temp_data <= data_in;
204
                                                data_out <= data_in; // writeback the same value
205
                                        end
206
                                        else begin
207
                                                pc <= pc;
208
                                                address <= pc;
209
                                                temp_data <= data_in;
210
                                        end
211 70 creep
                                end
212 71 creep
                                DUMMY_WRT_CALC: begin
213
                                        pc <= pc;
214
                                        address <= temp_addr;
215
                                        control <= MEM_WRITE;
216
                                        data_out <= alu_result;
217 70 creep
                                end
218 71 creep
                                WRITE_MEM: begin
219
                                        pc <= pc;
220
                                        address <= pc;
221 70 creep
                                end
222
                                default: begin
223
                                        $write("unknown state");        // TODO: check if synth really ignores this 2 lines. Otherwise wrap it with a `ifdef 
224
                                        $finish(0);
225
                                end
226
 
227
                        endcase
228
                end
229
        end
230
 
231 71 creep
        always @ (*) begin // this is the next_state logic and the output logic always block
232 70 creep
                //control = MEM_READ; 
233
                //data_out = 8'h00;
234 71 creep
 
235
                alu_opcode = 8'h00;
236
                alu_a = 8'h00;
237
                alu_enable = 1'b0;
238
                //address = pc;
239 70 creep
 
240 71 creep
                next_state = RESET; // this prevents the latch
241 68 creep
 
242 71 creep
                case (state)
243
                        RESET: begin
244
                                next_state = FETCH_OP;
245
                        end
246
                        FETCH_OP: begin
247
                                next_state = FETCH_LOW;
248
                        end
249
                        FETCH_OP_CALC: begin
250
                                next_state = FETCH_LOW;
251
                                alu_opcode = ir;
252
                                alu_enable = 1'b1;
253
 
254
                        end
255
                        FETCH_OP_CALC_PARAM: begin
256
                                next_state = FETCH_LOW;
257
                                alu_opcode = ir;
258
                                alu_enable = 1'b1;
259
                                alu_a = temp_data;
260
                        end
261
                        FETCH_LOW: begin
262
                                if (accumulator  || implied) begin
263
                                        alu_opcode = ir;
264
                                        alu_enable = 1'b1;
265
                                        next_state = FETCH_OP;
266
                                end
267
                                else if (immediate) begin
268
                                        next_state = FETCH_OP_CALC_PARAM;
269
                                end
270 77 creep
                                else if (zero_page) begin
271
                                        if (read || read_modify_write) begin
272
                                                next_state = READ_MEM;
273
                                        end
274
                                        else if (write) begin
275
                                                next_state = WRITE_MEM;
276
                                        end
277
                                        else begin
278
                                                $write("unknown behavior");
279
                                                $finish(0);
280
                                        end
281
                                end
282 71 creep
                                else begin // at least the absolute address mode falls here
283
                                        next_state = FETCH_HIGH;
284
                                end
285 78 creep
 
286
                                if (write) begin
287
                                        alu_opcode = ir;
288
                                        alu_enable = 1'b1;
289
                                        alu_a = 8'hzz;
290
                                end
291
 
292
 
293 71 creep
                        end
294
                        FETCH_HIGH: begin
295
                                if (jump) begin
296 68 creep
                                        next_state = FETCH_OP;
297 61 creep
                                end
298 71 creep
                                else if (read || read_modify_write) begin
299
                                        next_state = READ_MEM;
300 61 creep
                                end
301 71 creep
                                else if (write) begin
302
                                        next_state = WRITE_MEM;
303 68 creep
                                end
304 71 creep
                                else begin
305
                                        $write("unknown behavior");
306
                                        $finish(0);
307 61 creep
                                end
308 71 creep
                        end
309
                        READ_MEM: begin
310
                                if (read) begin
311
                                        next_state = FETCH_OP_CALC_PARAM;
312 61 creep
                                end
313 71 creep
                                else if (read_modify_write) begin
314
                                        next_state = DUMMY_WRT_CALC;
315
                                end
316
                        end
317
                        DUMMY_WRT_CALC: begin
318
                                alu_opcode = ir;
319
                                alu_enable = 1'b1;
320
                                alu_a = data_in;
321
                                next_state = WRITE_MEM;
322
                        end
323
                        WRITE_MEM: begin
324
                                next_state = FETCH_OP;
325
                        end
326
                        default: begin
327
                                next_state = RESET;
328
                        end
329
                endcase
330 61 creep
        end
331
 
332 77 creep
        // this always block is responsible for updating the address mode and the type of operation being done
333 68 creep
        always @ (*) begin // 
334 61 creep
                absolute = 1'b0;
335
                absolute_indexed = 1'b0;
336
                accumulator = 1'b0;
337
                immediate = 1'b0;
338
                implied = 1'b0;
339
                indirect = 1'b0;
340
                relative = 1'b0;
341
                zero_page = 1'b0;
342
                zero_page_indexed = 1'b0;
343
 
344
                read = 1'b0;
345
                read_modify_write = 1'b0;
346
                write = 1'b0;
347
                jump = 1'b0;
348
 
349 70 creep
                case (ir)
350
                        BRK_IMP, CLC_IMP, CLD_IMP, CLI_IMP, CLV_IMP, DEX_IMP, DEY_IMP, INX_IMP, INY_IMP, NOP_IMP, PHA_IMP, PHP_IMP, PLA_IMP,
351
                        PLP_IMP, RTI_IMP, RTS_IMP, SEC_IMP, SED_IMP, SEI_IMP, TAX_IMP, TAY_IMP, TSX_IMP, TXA_IMP, TXS_IMP, TYA_IMP: begin
352
                                implied = 1'b1;
353
                        end
354
                        ASL_ACC, LSR_ACC, ROL_ACC, ROR_ACC: begin
355
                                accumulator = 1'b1;
356
                        end
357
                        ADC_IMM, AND_IMM, CMP_IMM, CPX_IMM, CPY_IMM, EOR_IMM, LDA_IMM, LDX_IMM, LDY_IMM, ORA_IMM, SBC_IMM: begin
358
                                immediate = 1'b1;
359
                        end
360
                        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,
361
                        LSR_ZPG, ORA_ZPG, ROL_ZPG, ROR_ZPG, SBC_ZPG, STA_ZPG, STX_ZPG, STY_ZPG: begin
362
                                zero_page = 1'b1;
363
                        end
364
                        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,
365
                        SBC_ZPX, STA_ZPX, LDX_ZPY, STX_ZPY, STY_ZPX: begin
366
                                zero_page_indexed = 1'b1;
367
                        end
368
                        BCC_REL, BCS_REL, BEQ_REL, BMI_REL, BNE_REL, BPL_REL, BVC_REL, BVS_REL: begin
369
                                relative = 1'b1;
370
                        end
371
                        ADC_ABS, AND_ABS, ASL_ABS, BIT_ABS, CMP_ABS, CPX_ABS, CPY_ABS, DEC_ABS, EOR_ABS, INC_ABS, JMP_ABS, JSR_ABS, LDA_ABS,
372
                        LDX_ABS, LDY_ABS, LSR_ABS, ORA_ABS, ROL_ABS, ROR_ABS, SBC_ABS, STA_ABS, STX_ABS, STY_ABS: begin
373
                                absolute = 1'b1;
374
                        end
375
                        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,
376
                        SBC_ABX, STA_ABX, ADC_ABY, AND_ABY, CMP_ABY, EOR_ABY, LDA_ABY, LDX_ABY, ORA_ABY, SBC_ABY, STA_ABY: begin
377
                                absolute_indexed = 1'b1;
378
                        end
379
                        ADC_IDX, AND_IDX, CMP_IDX, EOR_IDX, LDA_IDX, ORA_IDX, SBC_IDX, STA_IDX, ADC_IDY, AND_IDY, CMP_IDY, EOR_IDY, LDA_IDY,
380
                        ORA_IDY, SBC_IDY, STA_IDY: begin // all these opcodes are 8'hX1; TODO: optimize this
381
                                indirect = 1'b1;
382
                        end
383 71 creep
                        default: begin
384 77 creep
                                $write("\nunknown OPCODE!!!!! 0x%h\n", ir);
385 71 creep
                                $finish();
386
                        end
387 70 creep
                endcase
388 71 creep
 
389
                case (ir)
390
                        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,
391
                        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,
392
                        DEC_ABX: begin
393
                                read_modify_write = 1'b1;
394
                        end
395
                        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
396
                                write = 1'b1;
397
                        end
398
                        default: begin // this should work fine since the previous case statement will detect the unknown/undocumented/unsupported opcodes
399
                                read = 1'b1;
400
                        end
401
                endcase
402 61 creep
 
403 71 creep
                if (ir == JMP_ABS || ir == JMP_IND) begin // the opcodes are 8'h4C and 8'h6C
404 70 creep
                        jump = 1'b1;
405
                end
406 63 creep
        end // no way
407 61 creep
endmodule
408
 
409
 
410
 

powered by: WebSVN 2.1.0

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