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

Subversion Repositories t6507lp

[/] [t6507lp/] [trunk/] [rtl/] [verilog/] [t6507lp_alu.v] - Blame information for rev 148

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

Line No. Rev Author Line
1 141 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 ALU                                                           ////
10
////                                                                    ////
11
//// To Do:                                                             ////
12
//// - Search for TODO                                                  ////
13
////                                                                    ////
14
//// Author(s):                                                         ////
15
//// - Gabriel Oshiro Zardo, gabrieloshiro@gmail.com                    ////
16
//// - Samuel Nascimento Pagliarini (creep), snpagliarini@gmail.com     ////
17
////                                                                    ////
18
////////////////////////////////////////////////////////////////////////////
19
////                                                                    ////
20
//// Copyright (C) 2001 Authors and OPENCORES.ORG                       ////
21
////                                                                    ////
22
//// This source file may be used and distributed without               ////
23
//// restriction provided that this copyright statement is not          ////
24
//// removed from the file and that any derivative work contains        ////
25
//// the original copyright notice and the associated disclaimer.       ////
26
////                                                                    ////
27
//// This source file is free software; you can redistribute it         ////
28
//// and/or modify it under the terms of the GNU Lesser General         ////
29
//// Public License as published by the Free Software Foundation;       ////
30
//// either version 2.1 of the License, or (at your option) any         ////
31
//// later version.                                                     ////
32
////                                                                    ////
33
//// This source is distributed in the hope that it will be             ////
34
//// useful, but WITHOUT ANY WARRANTY; without even the implied         ////
35
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR            ////
36
//// PURPOSE. See the GNU Lesser General Public License for more        ////
37
//// details.                                                           ////
38
////                                                                    ////
39
//// You should have received a copy of the GNU Lesser General          ////
40
//// Public License along with this source; if not, download it         ////
41
//// from http://www.opencores.org/lgpl.shtml                           ////
42
////                                                                    ////
43
////////////////////////////////////////////////////////////////////////////
44
 
45
`include "timescale.v"
46
 
47
// TODO: verify code identation
48
 
49
module t6507lp_alu( clk, reset_n, alu_enable, alu_result, alu_status, alu_opcode, alu_a, alu_x, alu_y );
50
 
51
input wire       clk;
52
input wire       reset_n;
53
input wire       alu_enable;
54
input wire [7:0] alu_opcode;
55
input wire [7:0] alu_a;
56
output reg [7:0] alu_result;
57
output reg [7:0] alu_status;
58
output reg [7:0] alu_x;
59
output reg [7:0] alu_y;
60
 
61
reg [7:0] A;
62
reg [7:0] X;
63
reg [7:0] Y;
64
 
65
reg [7:0] STATUS;
66
reg [7:0] result;
67
reg [7:0] bcd1;
68
reg [7:0] bcd2;
69
 
70
`include "t6507lp_package.v"
71
 
72
always @ (posedge clk or negedge reset_n)
73
begin
74
        if (reset_n == 0) begin
75 148 gabrielosh
                $display("RESTART");
76 141 creep
                alu_result <= 0;
77
                alu_status[C] <= 0;
78
                alu_status[N] <= 0;
79
                alu_status[V] <= 0;
80 148 gabrielosh
                alu_status[5] <= 1;
81 141 creep
                alu_status[Z] <= 1;
82
                alu_status[I] <= 0;
83
                alu_status[B] <= 0;
84
                alu_status[D] <= 0;
85
                A <= 0;
86
                X <= 0;
87
                Y <= 0;
88
                alu_x <= 0;
89
                alu_y <= 0;
90
        end
91
        else if ( alu_enable == 1 ) begin
92 148 gabrielosh
                //$display("A = %h result = %h", A, result);
93
                //$display("V = %b C = %b D = %b", STATUS[V], STATUS[C], STATUS[D]);
94
 
95 141 creep
                case (alu_opcode)
96
                        ADC_IMM, ADC_ZPG, ADC_ZPX, ADC_ABS, ADC_ABX, ADC_ABY, ADC_IDX, ADC_IDY,
97
                        AND_IMM, AND_ZPG, AND_ZPX, AND_ABS, AND_ABX, AND_ABY, AND_IDX, AND_IDY,
98
                        ASL_ACC, EOR_IMM, EOR_ZPG, EOR_ZPX, EOR_ABS, EOR_ABX, EOR_ABY, EOR_IDX,
99
                        EOR_IDY, LSR_ACC, ORA_IMM, ORA_ZPG, ORA_ZPX, ORA_ABS, ORA_ABX, ORA_ABY,
100
                        ORA_IDX, ORA_IDY, ROL_ACC, ROR_ACC, SBC_IMM, SBC_ZPG, SBC_ZPX, SBC_ABS,
101
                        SBC_ABX, SBC_ABY, SBC_IDX, SBC_IDY, LDA_IMM, LDA_ZPG, LDA_ZPX, LDA_ABS,
102
                        LDA_ABX, LDA_ABY, LDA_IDX, LDA_IDY, PLA_IMP, TXA_IMP, TYA_IMP :
103
                        begin
104 148 gabrielosh
                                $display("A = %h result = %h", A, result);
105
                                //$display("V = %b C = %b D = %b", STATUS[V], STATUS[C], STATUS[D]);
106 141 creep
                                A          <= result;
107
                                alu_result <= result;
108
                                alu_status <= STATUS;
109
                        end
110
                        LDX_IMM, LDX_ZPG, LDX_ZPY, LDX_ABS, LDX_ABY, TAX_IMP, TSX_IMP, INX_IMP, DEX_IMP :
111
                        begin
112
                                X          <= result;
113
                                alu_x      <= result;
114
                                alu_status <= STATUS;
115
                        end
116
                        TXS_IMP :
117
                        begin
118 148 gabrielosh
                                X          <= result;
119
                                alu_x      <= result;
120 141 creep
                        end
121
                        LDY_IMM, LDY_ZPG, LDY_ZPX, LDY_ABS, LDY_ABX, TAY_IMP, INY_IMP, DEY_IMP :
122
                        begin
123
                                Y          <= result;
124
                                alu_y      <= result;
125
                                alu_status <= STATUS;
126
                        end
127 148 gabrielosh
                        CMP_IMM, CMP_ZPG, CMP_ZPX, CMP_ABS, CMP_ABX, CMP_ABY, CMP_IDX, CMP_IDY,
128
                        CPX_IMM, CPX_ZPG, CPX_ABS, CPY_IMM, CPY_ZPG, CPY_ABS, PHP_IMP :
129 141 creep
                        begin
130
                                alu_status <= STATUS;
131
                        end
132
                        SEC_IMP :
133
                        begin
134
                                alu_status[C] <= 1;
135
                        end
136
                        SED_IMP :
137
                        begin
138
                                alu_status[D] <= 1;
139
                        end
140
                        SEI_IMP :
141
                        begin
142
                                alu_status[I] <= 1;
143
                        end
144
                        CLC_IMP :
145
                        begin
146
                                alu_status[C] <= 0;
147
                        end
148
                        CLD_IMP :
149
                        begin
150
                                alu_status[D] <= 0;
151
                        end
152
                        CLI_IMP :
153
                        begin
154
                                alu_status[I] <= 0;
155
                        end
156
                        CLV_IMP :
157
                        begin
158
                                alu_status[V] <= 0;
159
                        end
160
                        BRK_IMP :
161
                        begin
162
                                alu_status[B] <= 0;
163
                        end
164
                        PLP_IMP, RTI_IMP :
165
                        begin
166
                                alu_status <= alu_a;
167
                        end
168
                        BIT_ZPG, BIT_ABS :
169
                        begin
170
                                alu_status[Z] <= STATUS[Z];
171
                                alu_status[V] <= alu_a[6];
172
                                alu_status[N] <= alu_a[7];
173
                        end
174 148 gabrielosh
                        INC_ZPG, INC_ZPX, INC_ABS, INC_ABX, DEC_ZPG, DEC_ZPX, DEC_ABS, DEC_ABX,
175
                        ASL_ZPG, ASL_ZPX, ASL_ABS, ASL_ABX, LSR_ZPG, LSR_ZPX, LSR_ABS, LSR_ABX,
176
                        ROL_ZPG, ROL_ZPX, ROL_ABS, ROL_ABX, ROR_ZPG, ROR_ZPX, ROR_ABS, ROR_ABX :
177 141 creep
                        begin
178
                                alu_result <= result;
179
                                alu_status <= STATUS;
180
                        end
181
                        default : begin
182
                                //$display("ERROR");
183
                        end
184
                endcase
185
        end
186
end
187
 
188
always @ (*) begin
189
        bcd1      = A;
190
        bcd2      = alu_a;
191
        result    = alu_result;
192
        STATUS[C] = alu_status[C];
193
        STATUS[V] = alu_status[V];
194
        STATUS[B] = alu_status[B];
195
        STATUS[I] = alu_status[I];
196
        STATUS[D] = alu_status[D];
197
 
198
        case (alu_opcode)
199
                // BIT - Bit Test
200
                BIT_ZPG, BIT_ABS: begin
201
                        result = A & alu_a;
202
                end
203
 
204
                // BRK - Force Interrupt
205
                BRK_IMP: begin
206
                        STATUS[B] = 1'b1;
207
                end
208
 
209
                // CLC - Clear Carry Flag
210
                CLC_IMP: begin
211
                        STATUS[C] = 1'b0;
212
                end
213
 
214
                // CLD - Clear Decimal Flag
215
                CLD_IMP: begin
216
                        STATUS[D] = 1'b0;
217
                end
218
 
219
                // CLI - Clear Interrupt Disable
220
                // TODO: verify if this should be supported by 6507
221
                CLI_IMP: begin
222
                        STATUS[I] = 1'b0;
223
                end
224
 
225
                // CLV - Clear Overflow Flag
226
                CLV_IMP: begin
227
                        STATUS[V] = 1'b0;
228
                end
229
 
230
                // NOP - No Operation
231
                //NOP_IMP: begin
232
                        // Do nothing :-D
233
                //end
234
 
235
                // PLP - Pull Processor Status Register
236
                PLP_IMP, RTI_IMP: begin
237
                        STATUS = alu_a;
238
                end
239
 
240
                // STA - Store Accumulator
241
                // PHA - Push A
242
                // TAX - Transfer Accumulator to X
243
                // TAY - Transfer Accumulator to Y
244
                TAX_IMP, TAY_IMP, PHA_IMP, STA_ZPG, STA_ZPX, STA_ABS, STA_ABX, STA_ABY, STA_IDX, STA_IDY : begin
245
                        result = A;
246
                end
247
 
248
                // STX - Store X Register
249
                // TXA - Transfer X to Accumulator
250
                // TXS - Transfer X to Stack pointer
251
                STX_ZPG, STX_ZPY, STX_ABS, TXA_IMP, TXS_IMP : begin
252
                        result = X;
253
                end
254
 
255
                // STY - Store Y Register
256
                // TYA - Transfer Y to Accumulator
257
                STY_ZPG, STY_ZPX, STY_ABS, TYA_IMP : begin
258
                        result = Y;
259
                end
260
 
261
                // SEC - Set Carry Flag
262
                SEC_IMP: begin
263
                        STATUS[C] = 1'b1;
264
                end
265
 
266
                // SED - Set Decimal Flag
267
                SED_IMP: begin
268
                        STATUS[D] = 1'b1;
269
                end
270
 
271
                // SEI - Set Interrupt Disable
272
                SEI_IMP: begin
273
                        STATUS[I] = 1'b1;
274
                end
275
 
276
                // INC - Increment memory
277
                INC_ZPG, INC_ZPX, INC_ABS, INC_ABX : begin
278
                        result = alu_a + 1;
279
                end
280
 
281
                // INX - Increment X Register
282
                INX_IMP: begin
283
                        result = X + 1;
284
                end
285
 
286
                // INY - Increment Y Register
287
                INY_IMP : begin
288
                        result = Y + 1;
289
                end
290
 
291
                // DEC - Decrement memory
292
                DEC_ZPG, DEC_ZPX, DEC_ABS, DEC_ABX : begin
293
                        result = alu_a - 1;
294
                end
295
 
296
                // DEX - Decrement X register
297
                DEX_IMP: begin
298
                        result = X - 1;
299
                end
300
 
301
                // DEY - Decrement Y Register
302
                DEY_IMP: begin
303
                        result = Y - 1;
304
                end
305
 
306
                // ADC - Add with carry
307
                ADC_IMM, ADC_ZPG, ADC_ZPX, ADC_ABS, ADC_ABX, ADC_ABY, ADC_IDX, ADC_IDY : begin
308
                        if (alu_status[D] == 1) begin
309
                                if (A[3:0] > 9) begin
310
                                        bcd1 = A + 6; // A = A - 10 and A = A + 16
311
                                end
312
                                if (bcd1[7:4] > 9) begin
313
                                        bcd1 = bcd1[7:4] + 6; // A = A - 10 and A = A + 16
314
                                end
315
                                if (alu_a[3:0] > 9) begin
316
                                        bcd2 = alu_a + 6;
317
                                end
318
                                if (bcd2[7:4] > 9) begin
319
                                        bcd2 = bcd2[7:4] + 6; // A = A - 10 and A = A + 16
320
                                end
321
                        end
322 148 gabrielosh
                        $display("op1 = %h op2 = %h result = %h", bcd1, bcd2, result);
323
                        $display("V = %b C = %b D = %b", STATUS[V], STATUS[C], STATUS[D]);
324 141 creep
                        {STATUS[C],result} = bcd1 + bcd2 + alu_status[C];
325
                        if ((bcd1[7] == bcd2[7]) && (bcd1[7] != alu_result[7]))
326
                                STATUS[V] = 1;
327
                        else
328
                                STATUS[V] = 0;
329
 
330
                        if (alu_status[D] == 1) begin
331
                                if (result[3:0] > 9) begin
332
                                        result = result[3:0] + 6; // A = A - 10 and A = A + 16
333
                                end
334
                                if (result[7:4] > 9) begin
335
                                        result = result[7:4] + 6; // A = A - 10 and A = A + 16
336
                                        STATUS[C] = 1;
337
                                end
338
                        end
339 148 gabrielosh
                        $display("op1 = %h op2 = %h result = %h", bcd1, bcd2, result);
340
                        $display("V = %b C = %b D = %b", STATUS[V], STATUS[C], STATUS[D]);
341 141 creep
                end
342
 
343
                // AND - Logical AND
344
                AND_IMM, AND_ZPG, AND_ZPX, AND_ABS, AND_ABX, AND_ABY, AND_IDX, AND_IDY : begin
345
                        result = A & alu_a;
346
                end
347
 
348
                // CMP - Compare
349
                CMP_IMM, CMP_ZPG, CMP_ZPX, CMP_ABS, CMP_ABX, CMP_ABY, CMP_IDX, CMP_IDY : begin
350
                        result = A - alu_a;
351
                        STATUS[C] = (A >= alu_a) ? 1 : 0;
352
                end
353
 
354
                // EOR - Exclusive OR
355
                EOR_IMM, EOR_ZPG, EOR_ZPX, EOR_ABS, EOR_ABX, EOR_ABY, EOR_IDX, EOR_IDY : begin
356
                        result = A ^ alu_a ;
357
                end
358
 
359
                // LDA - Load Accumulator
360
                // LDX - Load X Register
361
                // LDY - Load Y Register
362
                // TSX - Transfer Stack Pointer to X
363
                LDA_IMM, LDA_ZPG, LDA_ZPX, LDA_ABS, LDA_ABX, LDA_ABY, LDA_IDX, LDA_IDY,
364
                LDX_IMM, LDX_ZPG, LDX_ZPY, LDX_ABS, LDX_ABY,
365
                LDY_IMM, LDY_ZPG, LDY_ZPX, LDY_ABS, LDY_ABX,
366
                TSX_IMP : begin
367
                        result = alu_a;
368
                end
369
 
370
                // ORA - Logical OR
371
                ORA_IMM, ORA_ZPG, ORA_ZPX, ORA_ABS, ORA_ABX, ORA_ABY, ORA_IDX, ORA_IDY : begin
372
                        result = A | alu_a;
373
                end
374
 
375
                // SBC - Subtract with Carry
376
                SBC_IMM, SBC_ZPG, SBC_ZPX, SBC_ABS, SBC_ABX, SBC_ABY, SBC_IDX, SBC_IDY : begin
377
                        if (alu_status[D] == 1) begin
378
                                if (A[3:0] > 9) begin
379
                                        bcd1 = A + 6; // A = A - 10 and A = A + 16
380
                                end
381
                                if (bcd1[7:4] > 9) begin
382
                                        bcd1 = bcd1[7:4] + 6; // A = A - 10 and A = A + 16
383
                                end
384
                                if (alu_a[3:0] > 9) begin
385
                                        bcd2 = alu_a + 6;
386
                                end
387
                                if (bcd2[7:4] > 9) begin
388
                                        bcd2 = bcd2[7:4] + 6; // A = A - 10 and A = A + 16
389
                                end
390
                        end
391
 
392
                        {STATUS[C],result} = bcd1 - bcd2 - ~alu_status[C];
393
                        if ((bcd1[7] == bcd2[7]) && (bcd1[7] != alu_result[7]))
394
                                STATUS[V] = 1;
395
                        else
396
                                STATUS[V] = 0;
397
                end
398
 
399
                // ASL - Arithmetic Shift Left
400
                ASL_ACC : begin
401 145 gabrielosh
                        //{STATUS[C],result} = A << 1;
402
                        {STATUS[C],result} = {A,1'b0};
403 141 creep
                end
404
                ASL_ZPG, ASL_ZPX, ASL_ABS, ASL_ABX : begin
405 145 gabrielosh
                        //{STATUS[C],result} = alu_a << 1;
406
                        {STATUS[C],result} = {alu_a,1'b0};
407 141 creep
                end
408
 
409
                // LSR - Logical Shift Right
410
                LSR_ACC: begin
411 145 gabrielosh
                        //{result, STATUS[C]} = A >> 1;
412
                        {result,STATUS[C]} = {1'b0,A};
413 141 creep
                end
414
                LSR_ZPG, LSR_ZPX, LSR_ABS, LSR_ABX : begin
415 145 gabrielosh
                        //{result, STATUS[C]} = alu_a >> 1;
416
                        {result,STATUS[C]} = {1'b0,alu_a};
417 141 creep
                end
418
 
419
                // ROL - Rotate Left
420
                ROL_ACC : begin
421
                        {STATUS[C],result} = {A,alu_status[C]}; //TODO: does it really work?
422
                end
423
                ROL_ZPG, ROL_ZPX, ROL_ABS, ROL_ABX : begin
424
                        {STATUS[C],result} = {alu_a,alu_status[C]};
425
                end
426
 
427
                // ROR - Rotate Right           
428
                ROR_ACC : begin
429
                        {result,STATUS[C]} = {alu_status[C],A};
430
                end
431
                ROR_ZPG, ROR_ZPX, ROR_ABS, ROR_ABX : begin
432
                        {result, STATUS[C]} = {alu_status[C], alu_a};
433
                end
434
 
435
                // CPX - Compare X Register
436
                CPX_IMM, CPX_ZPG, CPX_ABS : begin
437
                        result = X - alu_a;
438
                        STATUS[C] = (X >= alu_a) ? 1 : 0;
439
                end
440
 
441
                // CPY - Compare Y Register
442
                CPY_IMM, CPY_ZPG, CPY_ABS : begin
443
                        result = Y - alu_a;
444
                        STATUS[C] = (Y >= alu_a) ? 1 : 0;
445
                end
446
 
447
                default: begin // NON-DEFAULT OPCODES FALL HERE
448 142 gabrielosh
                end
449 141 creep
        endcase
450 142 gabrielosh
        STATUS[Z] = (result == 0) ? 1 : 0;
451
        STATUS[N] = result[7];
452 141 creep
end
453
 
454
endmodule
455
 

powered by: WebSVN 2.1.0

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