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

Subversion Repositories t6507lp

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

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

Line No. Rev Author Line
1 141 creep
////////////////////////////////////////////////////////////////////////////
2 152 gabrielosh
////                                                                    ////
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 141 creep
////////////////////////////////////////////////////////////////////////////
19 152 gabrielosh
////                                                                    ////
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 141 creep
////////////////////////////////////////////////////////////////////////////
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 152 gabrielosh
reg [7:0] op1;
68
reg [7:0] op2;
69 161 gabrielosh
reg [7:0] bcdl;
70
reg [7:0] bcdh;
71 164 gabrielosh
reg [7:0] bcdh2;
72
reg [7:0] AL;
73
reg [7:0] AH;
74 141 creep
 
75
`include "t6507lp_package.v"
76
 
77
always @ (posedge clk or negedge reset_n)
78
begin
79
        if (reset_n == 0) begin
80
                alu_result <= 0;
81
                alu_status[C] <= 0;
82
                alu_status[N] <= 0;
83
                alu_status[V] <= 0;
84 148 gabrielosh
                alu_status[5] <= 1;
85 141 creep
                alu_status[Z] <= 1;
86
                alu_status[I] <= 0;
87
                alu_status[B] <= 0;
88
                alu_status[D] <= 0;
89
                A <= 0;
90
                X <= 0;
91
                Y <= 0;
92
                alu_x <= 0;
93
                alu_y <= 0;
94
        end
95
        else if ( alu_enable == 1 ) begin
96
                case (alu_opcode)
97
                        ADC_IMM, ADC_ZPG, ADC_ZPX, ADC_ABS, ADC_ABX, ADC_ABY, ADC_IDX, ADC_IDY,
98
                        AND_IMM, AND_ZPG, AND_ZPX, AND_ABS, AND_ABX, AND_ABY, AND_IDX, AND_IDY,
99
                        ASL_ACC, EOR_IMM, EOR_ZPG, EOR_ZPX, EOR_ABS, EOR_ABX, EOR_ABY, EOR_IDX,
100
                        EOR_IDY, LSR_ACC, ORA_IMM, ORA_ZPG, ORA_ZPX, ORA_ABS, ORA_ABX, ORA_ABY,
101
                        ORA_IDX, ORA_IDY, ROL_ACC, ROR_ACC, SBC_IMM, SBC_ZPG, SBC_ZPX, SBC_ABS,
102
                        SBC_ABX, SBC_ABY, SBC_IDX, SBC_IDY, LDA_IMM, LDA_ZPG, LDA_ZPX, LDA_ABS,
103
                        LDA_ABX, LDA_ABY, LDA_IDX, LDA_IDY, PLA_IMP, TXA_IMP, TYA_IMP :
104
                        begin
105
                                A          <= result;
106
                                alu_result <= result;
107
                                alu_status <= STATUS;
108
                        end
109
                        LDX_IMM, LDX_ZPG, LDX_ZPY, LDX_ABS, LDX_ABY, TAX_IMP, TSX_IMP, INX_IMP, DEX_IMP :
110
                        begin
111
                                X          <= result;
112
                                alu_x      <= result;
113
                                alu_status <= STATUS;
114
                        end
115
                        TXS_IMP :
116
                        begin
117 148 gabrielosh
                                X          <= result;
118
                                alu_x      <= result;
119 141 creep
                        end
120
                        LDY_IMM, LDY_ZPG, LDY_ZPX, LDY_ABS, LDY_ABX, TAY_IMP, INY_IMP, DEY_IMP :
121
                        begin
122
                                Y          <= result;
123
                                alu_y      <= result;
124
                                alu_status <= STATUS;
125
                        end
126 148 gabrielosh
                        CMP_IMM, CMP_ZPG, CMP_ZPX, CMP_ABS, CMP_ABX, CMP_ABY, CMP_IDX, CMP_IDY,
127 165 gabrielosh
                        CPX_IMM, CPX_ZPG, CPX_ABS, CPY_IMM, CPY_ZPG, CPY_ABS :
128 141 creep
                        begin
129
                                alu_status <= STATUS;
130
                        end
131 158 gabrielosh
                        PHA_IMP :
132
                        begin
133
                                alu_result <= result;
134
                        end
135 141 creep
                        SEC_IMP :
136
                        begin
137
                                alu_status[C] <= 1;
138
                        end
139
                        SED_IMP :
140
                        begin
141
                                alu_status[D] <= 1;
142
                        end
143
                        SEI_IMP :
144
                        begin
145
                                alu_status[I] <= 1;
146
                        end
147
                        CLC_IMP :
148
                        begin
149
                                alu_status[C] <= 0;
150
                        end
151
                        CLD_IMP :
152
                        begin
153
                                alu_status[D] <= 0;
154
                        end
155
                        CLI_IMP :
156
                        begin
157
                                alu_status[I] <= 0;
158
                        end
159
                        CLV_IMP :
160
                        begin
161
                                alu_status[V] <= 0;
162
                        end
163
                        BRK_IMP :
164
                        begin
165 154 gabrielosh
                                alu_status[B] <= 1;
166 141 creep
                        end
167 171 creep
                        PLP_IMP, RTI_IMP :
168 141 creep
                        begin
169 150 gabrielosh
                                alu_status[C] <= alu_a[C];
170
                                alu_status[Z] <= alu_a[Z];
171
                                alu_status[I] <= alu_a[I];
172
                                alu_status[D] <= alu_a[D];
173
                                alu_status[B] <= alu_a[B];
174
                                alu_status[V] <= alu_a[V];
175
                                alu_status[N] <= alu_a[N];
176 141 creep
                        end
177
                        BIT_ZPG, BIT_ABS :
178
                        begin
179
                                alu_status[Z] <= STATUS[Z];
180
                                alu_status[V] <= alu_a[6];
181
                                alu_status[N] <= alu_a[7];
182
                        end
183 148 gabrielosh
                        INC_ZPG, INC_ZPX, INC_ABS, INC_ABX, DEC_ZPG, DEC_ZPX, DEC_ABS, DEC_ABX,
184
                        ASL_ZPG, ASL_ZPX, ASL_ABS, ASL_ABX, LSR_ZPG, LSR_ZPX, LSR_ABS, LSR_ABX,
185
                        ROL_ZPG, ROL_ZPX, ROL_ABS, ROL_ABX, ROR_ZPG, ROR_ZPX, ROR_ABS, ROR_ABX :
186 141 creep
                        begin
187
                                alu_result <= result;
188
                                alu_status <= STATUS;
189
                        end
190 165 gabrielosh
                        PHP_IMP : begin
191
                        end
192 141 creep
                        default : begin
193
                                //$display("ERROR");
194
                        end
195
                endcase
196
        end
197
end
198
 
199
always @ (*) begin
200 152 gabrielosh
        op1      = A;
201
        op2      = alu_a;
202 150 gabrielosh
        result    = alu_result;
203
        STATUS[N] = alu_status[N];
204
        STATUS[C] = alu_status[C];
205
        STATUS[V] = alu_status[V];
206
        STATUS[B] = alu_status[B];
207
        STATUS[I] = alu_status[I];
208
        STATUS[D] = alu_status[D];
209
        STATUS[Z] = alu_status[Z];
210
        STATUS[N] = alu_status[N];
211 151 gabrielosh
        STATUS[5] = 1;
212 141 creep
 
213 171 creep
        bcdl = 0;
214
        bcdh = 0;
215
        bcdh2 = 0;
216
        AL = 0;
217
        AH = 0;
218
 
219
 
220 141 creep
        case (alu_opcode)
221
                // BIT - Bit Test
222
                BIT_ZPG, BIT_ABS: begin
223
                        result = A & alu_a;
224
                end
225
 
226
                // BRK - Force Interrupt
227
                BRK_IMP: begin
228
                        STATUS[B] = 1'b1;
229
                end
230
 
231
                // CLC - Clear Carry Flag
232
                CLC_IMP: begin
233
                        STATUS[C] = 1'b0;
234
                end
235
 
236
                // CLD - Clear Decimal Flag
237
                CLD_IMP: begin
238
                        STATUS[D] = 1'b0;
239
                end
240
 
241
                // CLI - Clear Interrupt Disable
242
                CLI_IMP: begin
243
                        STATUS[I] = 1'b0;
244
                end
245
 
246
                // CLV - Clear Overflow Flag
247
                CLV_IMP: begin
248
                        STATUS[V] = 1'b0;
249
                end
250
 
251
                // NOP - No Operation
252
                //NOP_IMP: begin
253
                        // Do nothing :-D
254
                //end
255
 
256
                // PLP - Pull Processor Status Register
257 171 creep
                PLP_IMP, RTI_IMP: begin
258 141 creep
                        STATUS = alu_a;
259
                end
260 157 gabrielosh
 
261
                PLA_IMP : begin
262
                        result = alu_a;
263
                end
264 141 creep
 
265
                // STA - Store Accumulator
266
                // PHA - Push A
267
                // TAX - Transfer Accumulator to X
268
                // TAY - Transfer Accumulator to Y
269
                TAX_IMP, TAY_IMP, PHA_IMP, STA_ZPG, STA_ZPX, STA_ABS, STA_ABX, STA_ABY, STA_IDX, STA_IDY : begin
270
                        result = A;
271
                end
272
 
273
                // STX - Store X Register
274
                // TXA - Transfer X to Accumulator
275
                // TXS - Transfer X to Stack pointer
276
                STX_ZPG, STX_ZPY, STX_ABS, TXA_IMP, TXS_IMP : begin
277
                        result = X;
278
                end
279
 
280
                // STY - Store Y Register
281
                // TYA - Transfer Y to Accumulator
282
                STY_ZPG, STY_ZPX, STY_ABS, TYA_IMP : begin
283
                        result = Y;
284
                end
285
 
286
                // SEC - Set Carry Flag
287
                SEC_IMP: begin
288
                        STATUS[C] = 1'b1;
289
                end
290
 
291
                // SED - Set Decimal Flag
292
                SED_IMP: begin
293
                        STATUS[D] = 1'b1;
294
                end
295
 
296
                // SEI - Set Interrupt Disable
297
                SEI_IMP: begin
298
                        STATUS[I] = 1'b1;
299
                end
300
 
301
                // INC - Increment memory
302
                INC_ZPG, INC_ZPX, INC_ABS, INC_ABX : begin
303
                        result = alu_a + 1;
304
                end
305
 
306
                // INX - Increment X Register
307
                INX_IMP: begin
308
                        result = X + 1;
309
                end
310
 
311
                // INY - Increment Y Register
312
                INY_IMP : begin
313
                        result = Y + 1;
314
                end
315
 
316
                // DEC - Decrement memory
317
                DEC_ZPG, DEC_ZPX, DEC_ABS, DEC_ABX : begin
318
                        result = alu_a - 1;
319
                end
320
 
321
                // DEX - Decrement X register
322
                DEX_IMP: begin
323
                        result = X - 1;
324
                end
325
 
326
                // DEY - Decrement Y Register
327
                DEY_IMP: begin
328
                        result = Y - 1;
329
                end
330
 
331
                // ADC - Add with carry
332 162 gabrielosh
                // TODO: verify synthesis for % operand
333 141 creep
                ADC_IMM, ADC_ZPG, ADC_ZPX, ADC_ABS, ADC_ABX, ADC_ABY, ADC_IDX, ADC_IDY : begin
334
                        if (alu_status[D] == 1) begin
335 171 creep
                                //$display("MODO DECIMAL");
336 164 gabrielosh
                                AL = A[3:0] + alu_a[3:0] + alu_status[C];
337
                                AH = A[7:4] + alu_a[7:4];
338 171 creep
                                //$display("AL = %d", AL);
339
                                //$display("AH = %d", AH);
340 164 gabrielosh
                                if (AL > 9) begin
341
                                        bcdh = AH + (AL / 10);
342
                                        bcdl = AL % 10;
343 141 creep
                                end
344 171 creep
                                else begin
345
                                        bcdh = AH;
346
                                        bcdl = AL;
347
                                end
348
 
349
                                // ok
350
 
351
                                if (bcdh > 9) begin
352 161 gabrielosh
                                        STATUS[C] = 1;
353 164 gabrielosh
                                        bcdh2 = bcdh % 10;
354 161 gabrielosh
                                end
355 171 creep
                                else begin
356
                                        STATUS[C] = 0;
357
                                        bcdh2 = bcdh;
358
                                end
359
                                //$display("bcdh2 = %d", bcdh2);
360
                                //$display("bcdl = %d", bcdl);
361 164 gabrielosh
                                result = {bcdh2[3:0],bcdl[3:0]};
362 141 creep
                        end
363 165 gabrielosh
                        else begin
364 171 creep
                                //$display("MODO NORMAL");
365 161 gabrielosh
                                {STATUS[C],result} = op1 + op2 + alu_status[C];
366 165 gabrielosh
                        end
367 162 gabrielosh
 
368 152 gabrielosh
                        if ((op1[7] == op2[7]) && (op1[7] != result[7]))
369 141 creep
                                STATUS[V] = 1;
370
                        else
371
                                STATUS[V] = 0;
372
                end
373
 
374
                // AND - Logical AND
375
                AND_IMM, AND_ZPG, AND_ZPX, AND_ABS, AND_ABX, AND_ABY, AND_IDX, AND_IDY : begin
376
                        result = A & alu_a;
377
                end
378
 
379
                // CMP - Compare
380
                CMP_IMM, CMP_ZPG, CMP_ZPX, CMP_ABS, CMP_ABX, CMP_ABY, CMP_IDX, CMP_IDY : begin
381
                        result = A - alu_a;
382
                        STATUS[C] = (A >= alu_a) ? 1 : 0;
383
                end
384
 
385
                // EOR - Exclusive OR
386
                EOR_IMM, EOR_ZPG, EOR_ZPX, EOR_ABS, EOR_ABX, EOR_ABY, EOR_IDX, EOR_IDY : begin
387 156 gabrielosh
                        result = A ^ alu_a;
388 158 gabrielosh
                        //$display("op1 ^ op2 = result");
389
                        //$display("%d  ^ %d  = %d", op1, op2, result);
390 141 creep
                end
391
 
392
                // LDA - Load Accumulator
393
                // LDX - Load X Register
394
                // LDY - Load Y Register
395
                // TSX - Transfer Stack Pointer to X
396
                LDA_IMM, LDA_ZPG, LDA_ZPX, LDA_ABS, LDA_ABX, LDA_ABY, LDA_IDX, LDA_IDY,
397
                LDX_IMM, LDX_ZPG, LDX_ZPY, LDX_ABS, LDX_ABY,
398
                LDY_IMM, LDY_ZPG, LDY_ZPX, LDY_ABS, LDY_ABX,
399
                TSX_IMP : begin
400
                        result = alu_a;
401
                end
402
 
403
                // ORA - Logical OR
404
                ORA_IMM, ORA_ZPG, ORA_ZPX, ORA_ABS, ORA_ABX, ORA_ABY, ORA_IDX, ORA_IDY : begin
405
                        result = A | alu_a;
406
                end
407
 
408
                // SBC - Subtract with Carry
409
                SBC_IMM, SBC_ZPG, SBC_ZPX, SBC_ABS, SBC_ABX, SBC_ABY, SBC_IDX, SBC_IDY : begin
410 162 gabrielosh
/*                      if (alu_status[D] == 1) begin
411 161 gabrielosh
                                bcdl = A[3:0] + alu_a[3:0] + alu_status[C];
412
                                bcdh = A[7:4] + alu_a[7:4];
413
                                if (bcdl > 9) begin
414
                                        bcdl = bcdl - 10; // A = A - 10 and A = A + 16
415
                                        bcdh = bcdh + 1; // A = A - 10 and A = A + 16
416 141 creep
                                end
417 161 gabrielosh
                                if (bcdh > 9) begin
418
                                        STATUS[C] = 1;
419
                                        bcdh = bcdh - 10;
420 141 creep
                                end
421 161 gabrielosh
                                result = {bcdh[3:0],bcdl[3:0]};
422 141 creep
                        end
423 161 gabrielosh
                        else begin
424
                                {STATUS[C],result} = op1 - op2 - ~alu_status[C];
425
                        end
426 152 gabrielosh
                        if ((op1[7] == op2[7]) && (op1[7] != result[7]))
427 141 creep
                                STATUS[V] = 1;
428
                        else
429
                                STATUS[V] = 0;
430 162 gabrielosh
                                if (alu_status[D] == 1) begin
431
                                bcdl = A[3:0] + alu_a[3:0] + alu_status[C];
432
                                bcdh = A[7:4] + alu_a[7:4];
433
                                if (bcdl > 9) begin
434
                                        bcdh = bcdh + bcdl[5:4];
435
                                        bcdl = bcdl % 10;
436
                                end
437
                                if (bcdh > 9) begin
438
                                        STATUS[C] = 1;
439
                                        bcdh = bcdh % 10;
440
                                end
441
                        end
442
                        else
443
                                {STATUS[C],result} = op1 + op2 + alu_status[C];
444
 
445
                        if ((op1[7] == op2[7]) && (op1[7] != result[7]))
446
                                STATUS[V] = 1;
447
                        else
448
                                STATUS[V] = 0;
449
*/
450
                        if (alu_status[D] == 1) begin
451 171 creep
                                bcdl = A[3:0] - alu_a[3:0] - ~alu_status[C];
452 163 gabrielosh
                                bcdh = A[7:4] - alu_a[7:4];
453 162 gabrielosh
                                if (bcdl > 9) begin
454
                                        bcdh = bcdh + bcdl[5:4];
455
                                        bcdl = bcdl % 10;
456
                                end
457
                                if (bcdh > 9) begin
458
                                        STATUS[C] = 1;
459
                                        bcdh = bcdh % 10;
460
                                end
461 163 gabrielosh
                                result = {bcdh[3:0],bcdl[3:0]};
462 162 gabrielosh
                        end
463
                        else
464 166 gabrielosh
                                {STATUS[C],result} = op1 - op2 - ( 1 - alu_status[C] );
465 162 gabrielosh
 
466
                        if ((op1[7] == op2[7]) && (op1[7] != result[7]))
467
                                STATUS[V] = 1;
468
                        else
469
                                STATUS[V] = 0;
470
 
471 141 creep
                end
472
 
473
                // ASL - Arithmetic Shift Left
474
                ASL_ACC : begin
475 145 gabrielosh
                        //{STATUS[C],result} = A << 1;
476
                        {STATUS[C],result} = {A,1'b0};
477 141 creep
                end
478
                ASL_ZPG, ASL_ZPX, ASL_ABS, ASL_ABX : begin
479 145 gabrielosh
                        //{STATUS[C],result} = alu_a << 1;
480
                        {STATUS[C],result} = {alu_a,1'b0};
481 141 creep
                end
482
 
483
                // LSR - Logical Shift Right
484
                LSR_ACC: begin
485 145 gabrielosh
                        //{result, STATUS[C]} = A >> 1;
486
                        {result,STATUS[C]} = {1'b0,A};
487 141 creep
                end
488
                LSR_ZPG, LSR_ZPX, LSR_ABS, LSR_ABX : begin
489 145 gabrielosh
                        //{result, STATUS[C]} = alu_a >> 1;
490
                        {result,STATUS[C]} = {1'b0,alu_a};
491 141 creep
                end
492
 
493
                // ROL - Rotate Left
494
                ROL_ACC : begin
495 152 gabrielosh
                        {STATUS[C],result} = {A,alu_status[C]};
496 141 creep
                end
497
                ROL_ZPG, ROL_ZPX, ROL_ABS, ROL_ABX : begin
498
                        {STATUS[C],result} = {alu_a,alu_status[C]};
499
                end
500
 
501 152 gabrielosh
                // ROR - Rotate Right
502 141 creep
                ROR_ACC : begin
503
                        {result,STATUS[C]} = {alu_status[C],A};
504
                end
505
                ROR_ZPG, ROR_ZPX, ROR_ABS, ROR_ABX : begin
506
                        {result, STATUS[C]} = {alu_status[C], alu_a};
507
                end
508
 
509
                // CPX - Compare X Register
510
                CPX_IMM, CPX_ZPG, CPX_ABS : begin
511
                        result = X - alu_a;
512
                        STATUS[C] = (X >= alu_a) ? 1 : 0;
513
                end
514
 
515
                // CPY - Compare Y Register
516
                CPY_IMM, CPY_ZPG, CPY_ABS : begin
517
                        result = Y - alu_a;
518
                        STATUS[C] = (Y >= alu_a) ? 1 : 0;
519
                end
520
 
521
                default: begin // NON-DEFAULT OPCODES FALL HERE
522 142 gabrielosh
                end
523 141 creep
        endcase
524 142 gabrielosh
        STATUS[Z] = (result == 0) ? 1 : 0;
525
        STATUS[N] = result[7];
526 141 creep
end
527
 
528
endmodule
529
 

powered by: WebSVN 2.1.0

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