Line 62... |
Line 62... |
reg [7:0] X;
|
reg [7:0] X;
|
reg [7:0] Y;
|
reg [7:0] Y;
|
|
|
reg [7:0] STATUS;
|
reg [7:0] STATUS;
|
reg [7:0] result;
|
reg [7:0] result;
|
reg [7:0] bcd1;
|
reg [7:0] op1;
|
reg [7:0] bcd2;
|
reg [7:0] op2;
|
|
|
`include "t6507lp_package.v"
|
`include "t6507lp_package.v"
|
|
|
always @ (posedge clk or negedge reset_n)
|
always @ (posedge clk or negedge reset_n)
|
begin
|
begin
|
if (reset_n == 0) begin
|
if (reset_n == 0) begin
|
//$display("RESTART");
|
|
alu_result <= 0;
|
alu_result <= 0;
|
alu_status[C] <= 0;
|
alu_status[C] <= 0;
|
alu_status[N] <= 0;
|
alu_status[N] <= 0;
|
alu_status[V] <= 0;
|
alu_status[V] <= 0;
|
alu_status[5] <= 1;
|
alu_status[5] <= 1;
|
Line 87... |
Line 86... |
Y <= 0;
|
Y <= 0;
|
alu_x <= 0;
|
alu_x <= 0;
|
alu_y <= 0;
|
alu_y <= 0;
|
end
|
end
|
else if ( alu_enable == 1 ) begin
|
else if ( alu_enable == 1 ) begin
|
//$display("A = %h result = %h", A, result);
|
|
//$display("V = %b C = %b D = %b", STATUS[V], STATUS[C], STATUS[D]);
|
|
|
|
case (alu_opcode)
|
case (alu_opcode)
|
ADC_IMM, ADC_ZPG, ADC_ZPX, ADC_ABS, ADC_ABX, ADC_ABY, ADC_IDX, ADC_IDY,
|
ADC_IMM, ADC_ZPG, ADC_ZPX, ADC_ABS, ADC_ABX, ADC_ABY, ADC_IDX, ADC_IDY,
|
AND_IMM, AND_ZPG, AND_ZPX, AND_ABS, AND_ABX, AND_ABY, AND_IDX, AND_IDY,
|
AND_IMM, AND_ZPG, AND_ZPX, AND_ABS, AND_ABX, AND_ABY, AND_IDX, AND_IDY,
|
ASL_ACC, EOR_IMM, EOR_ZPG, EOR_ZPX, EOR_ABS, EOR_ABX, EOR_ABY, EOR_IDX,
|
ASL_ACC, EOR_IMM, EOR_ZPG, EOR_ZPX, EOR_ABS, EOR_ABX, EOR_ABY, EOR_IDX,
|
EOR_IDY, LSR_ACC, ORA_IMM, ORA_ZPG, ORA_ZPX, ORA_ABS, ORA_ABX, ORA_ABY,
|
EOR_IDY, LSR_ACC, ORA_IMM, ORA_ZPG, ORA_ZPX, ORA_ABS, ORA_ABX, ORA_ABY,
|
ORA_IDX, ORA_IDY, ROL_ACC, ROR_ACC, SBC_IMM, SBC_ZPG, SBC_ZPX, SBC_ABS,
|
ORA_IDX, ORA_IDY, ROL_ACC, ROR_ACC, SBC_IMM, SBC_ZPG, SBC_ZPX, SBC_ABS,
|
SBC_ABX, SBC_ABY, SBC_IDX, SBC_IDY, LDA_IMM, LDA_ZPG, LDA_ZPX, LDA_ABS,
|
SBC_ABX, SBC_ABY, SBC_IDX, SBC_IDY, LDA_IMM, LDA_ZPG, LDA_ZPX, LDA_ABS,
|
LDA_ABX, LDA_ABY, LDA_IDX, LDA_IDY, PLA_IMP, TXA_IMP, TYA_IMP :
|
LDA_ABX, LDA_ABY, LDA_IDX, LDA_IDY, PLA_IMP, TXA_IMP, TYA_IMP :
|
begin
|
begin
|
//$display("A = %h result = %h", A, result);
|
|
//$display("V = %b C = %b D = %b", STATUS[V], STATUS[C], STATUS[D]);
|
|
A <= result;
|
A <= result;
|
alu_result <= result;
|
alu_result <= result;
|
alu_status <= STATUS;
|
alu_status <= STATUS;
|
end
|
end
|
LDX_IMM, LDX_ZPG, LDX_ZPY, LDX_ABS, LDX_ABY, TAX_IMP, TSX_IMP, INX_IMP, DEX_IMP :
|
LDX_IMM, LDX_ZPG, LDX_ZPY, LDX_ABS, LDX_ABY, TAX_IMP, TSX_IMP, INX_IMP, DEX_IMP :
|
Line 190... |
Line 184... |
endcase
|
endcase
|
end
|
end
|
end
|
end
|
|
|
always @ (*) begin
|
always @ (*) begin
|
bcd1 = A;
|
op1 = A;
|
bcd2 = alu_a;
|
op2 = alu_a;
|
result = alu_result;
|
result = alu_result;
|
STATUS[N] = alu_status[N];
|
STATUS[N] = alu_status[N];
|
STATUS[C] = alu_status[C];
|
STATUS[C] = alu_status[C];
|
STATUS[V] = alu_status[V];
|
STATUS[V] = alu_status[V];
|
STATUS[B] = alu_status[B];
|
STATUS[B] = alu_status[B];
|
Line 225... |
Line 219... |
CLD_IMP: begin
|
CLD_IMP: begin
|
STATUS[D] = 1'b0;
|
STATUS[D] = 1'b0;
|
end
|
end
|
|
|
// CLI - Clear Interrupt Disable
|
// CLI - Clear Interrupt Disable
|
// TODO: verify if this should be supported by 6507
|
|
CLI_IMP: begin
|
CLI_IMP: begin
|
STATUS[I] = 1'b0;
|
STATUS[I] = 1'b0;
|
end
|
end
|
|
|
// CLV - Clear Overflow Flag
|
// CLV - Clear Overflow Flag
|
Line 315... |
Line 308... |
|
|
// ADC - Add with carry
|
// ADC - Add with carry
|
ADC_IMM, ADC_ZPG, ADC_ZPX, ADC_ABS, ADC_ABX, ADC_ABY, ADC_IDX, ADC_IDY : begin
|
ADC_IMM, ADC_ZPG, ADC_ZPX, ADC_ABS, ADC_ABX, ADC_ABY, ADC_IDX, ADC_IDY : begin
|
if (alu_status[D] == 1) begin
|
if (alu_status[D] == 1) begin
|
if (A[3:0] > 9) begin
|
if (A[3:0] > 9) begin
|
bcd1 = A + 6; // A = A - 10 and A = A + 16
|
op1 = A + 6; // A = A - 10 and A = A + 16
|
end
|
end
|
if (bcd1[7:4] > 9) begin
|
if (op1[7:4] > 9) begin
|
bcd1 = bcd1[7:4] + 6; // A = A - 10 and A = A + 16
|
op1 = op1[7:4] + 6; // A = A - 10 and A = A + 16
|
end
|
end
|
if (alu_a[3:0] > 9) begin
|
if (alu_a[3:0] > 9) begin
|
bcd2 = alu_a + 6;
|
op2 = alu_a + 6;
|
end
|
end
|
if (bcd2[7:4] > 9) begin
|
if (op2[7:4] > 9) begin
|
bcd2 = bcd2[7:4] + 6; // A = A - 10 and A = A + 16
|
op2 = op2[7:4] + 6; // A = A - 10 and A = A + 16
|
end
|
end
|
end
|
end
|
//$display("op1 = %h op2 = %h result = %h", bcd1, bcd2, result);
|
{STATUS[C],result} = op1 + op2 + alu_status[C];
|
//$display("V = %b C = %b D = %b", STATUS[V], STATUS[C], STATUS[D]);
|
if ((op1[7] == op2[7]) && (op1[7] != result[7]))
|
{STATUS[C],result} = bcd1 + bcd2 + alu_status[C];
|
|
if ((bcd1[7] == bcd2[7]) && (bcd1[7] != alu_result[7]))
|
|
STATUS[V] = 1;
|
STATUS[V] = 1;
|
else
|
else
|
STATUS[V] = 0;
|
STATUS[V] = 0;
|
|
|
if (alu_status[D] == 1) begin
|
if (alu_status[D] == 1) begin
|
Line 344... |
Line 335... |
if (result[7:4] > 9) begin
|
if (result[7:4] > 9) begin
|
result = result[7:4] + 6; // A = A - 10 and A = A + 16
|
result = result[7:4] + 6; // A = A - 10 and A = A + 16
|
STATUS[C] = 1;
|
STATUS[C] = 1;
|
end
|
end
|
end
|
end
|
//$display("op1 = %h op2 = %h result = %h", bcd1, bcd2, result);
|
|
//$display("V = %b C = %b D = %b", STATUS[V], STATUS[C], STATUS[D]);
|
|
end
|
end
|
|
|
// AND - Logical AND
|
// AND - Logical AND
|
AND_IMM, AND_ZPG, AND_ZPX, AND_ABS, AND_ABX, AND_ABY, AND_IDX, AND_IDY : begin
|
AND_IMM, AND_ZPG, AND_ZPX, AND_ABS, AND_ABX, AND_ABY, AND_IDX, AND_IDY : begin
|
result = A & alu_a;
|
result = A & alu_a;
|
Line 384... |
Line 373... |
|
|
// SBC - Subtract with Carry
|
// SBC - Subtract with Carry
|
SBC_IMM, SBC_ZPG, SBC_ZPX, SBC_ABS, SBC_ABX, SBC_ABY, SBC_IDX, SBC_IDY : begin
|
SBC_IMM, SBC_ZPG, SBC_ZPX, SBC_ABS, SBC_ABX, SBC_ABY, SBC_IDX, SBC_IDY : begin
|
if (alu_status[D] == 1) begin
|
if (alu_status[D] == 1) begin
|
if (A[3:0] > 9) begin
|
if (A[3:0] > 9) begin
|
bcd1 = A + 6; // A = A - 10 and A = A + 16
|
op1 = A + 6; // A = A - 10 and A = A + 16
|
end
|
end
|
if (bcd1[7:4] > 9) begin
|
if (op1[7:4] > 9) begin
|
bcd1 = bcd1[7:4] + 6; // A = A - 10 and A = A + 16
|
op1 = op1[7:4] + 6; // A = A - 10 and A = A + 16
|
end
|
end
|
if (alu_a[3:0] > 9) begin
|
if (alu_a[3:0] > 9) begin
|
bcd2 = alu_a + 6;
|
op2 = alu_a + 6;
|
end
|
end
|
if (bcd2[7:4] > 9) begin
|
if (op2[7:4] > 9) begin
|
bcd2 = bcd2[7:4] + 6; // A = A - 10 and A = A + 16
|
op2 = op2[7:4] + 6; // A = A - 10 and A = A + 16
|
end
|
end
|
end
|
end
|
|
|
{STATUS[C],result} = bcd1 - bcd2 - ~alu_status[C];
|
{STATUS[C],result} = op1 - op2 - ~alu_status[C];
|
if ((bcd1[7] == bcd2[7]) && (bcd1[7] != alu_result[7]))
|
|
|
if ((op1[7] == op2[7]) && (op1[7] != result[7]))
|
STATUS[V] = 1;
|
STATUS[V] = 1;
|
else
|
else
|
STATUS[V] = 0;
|
STATUS[V] = 0;
|
end
|
end
|
|
|
Line 426... |
Line 416... |
{result,STATUS[C]} = {1'b0,alu_a};
|
{result,STATUS[C]} = {1'b0,alu_a};
|
end
|
end
|
|
|
// ROL - Rotate Left
|
// ROL - Rotate Left
|
ROL_ACC : begin
|
ROL_ACC : begin
|
{STATUS[C],result} = {A,alu_status[C]}; //TODO: does it really work?
|
{STATUS[C],result} = {A,alu_status[C]};
|
end
|
end
|
ROL_ZPG, ROL_ZPX, ROL_ABS, ROL_ABX : begin
|
ROL_ZPG, ROL_ZPX, ROL_ABS, ROL_ABX : begin
|
{STATUS[C],result} = {alu_a,alu_status[C]};
|
{STATUS[C],result} = {alu_a,alu_status[C]};
|
end
|
end
|
|
|