mirror of
https://github.com/ArthurFerreira2/reinette.git
synced 2024-12-27 02:32:42 +00:00
the Apple 1 Emulator
This commit is contained in:
parent
f0e878b341
commit
ac7501c83b
70
reinette.c
70
reinette.c
@ -159,7 +159,7 @@ static void IND(){ // INDirect - JMP ($ABCD) with page-boundary wraparound bug
|
|||||||
|
|
||||||
static void IDX(){ // InDexed indirect X
|
static void IDX(){ // InDexed indirect X
|
||||||
uint16_t vector1 = ((readMem(reg.PC++) + reg.X) & 0xFF);
|
uint16_t vector1 = ((readMem(reg.PC++) + reg.X) & 0xFF);
|
||||||
ope.address = readMem(vector1&0x00FF) | (readMem((vector1+1) & 0x00FF) << 8);
|
ope.address = readMem(vector1 & 0x00FF)|(readMem((vector1+1) & 0x00FF) << 8);
|
||||||
ope.value = readMem(ope.address);
|
ope.value = readMem(ope.address);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -219,7 +219,8 @@ static void LDA(){ // LoaD Accumulator
|
|||||||
|
|
||||||
static void LDX(){ // LoaD X
|
static void LDX(){ // LoaD X
|
||||||
reg.X = ope.value;
|
reg.X = ope.value;
|
||||||
setSZ(reg.X);}
|
setSZ(reg.X);
|
||||||
|
}
|
||||||
|
|
||||||
static void LDY(){ // LoaD Y
|
static void LDY(){ // LoaD Y
|
||||||
reg.Y = ope.value;
|
reg.Y = ope.value;
|
||||||
@ -362,20 +363,20 @@ static void RTI(){ // ReTurn from Interrupt
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void CMP(){ // Compare with A
|
static void CMP(){ // Compare with A
|
||||||
setSZ((reg.A - ope.value) & 0xFF);
|
setSZ(reg.A - ope.value);
|
||||||
if (reg.A >= (ope.value & 0xFF)) reg.SR |= CARRY;
|
if (reg.A >= ope.value) reg.SR |= CARRY;
|
||||||
else reg.SR &= ~CARRY;
|
else reg.SR &= ~CARRY;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void CPX(){ // Compare with X
|
static void CPX(){ // Compare with X
|
||||||
setSZ((reg.X - ope.value) & 0xFF);
|
setSZ(reg.X - ope.value);
|
||||||
if (reg.X >= (ope.value & 0xFF)) reg.SR |= CARRY;
|
if (reg.X >= ope.value) reg.SR |= CARRY;
|
||||||
else reg.SR &= ~CARRY;
|
else reg.SR &= ~CARRY;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void CPY(){ // Compare with Y
|
static void CPY(){ // Compare with Y
|
||||||
setSZ((reg.Y - ope.value) & 0xFF);
|
setSZ(reg.Y - ope.value);
|
||||||
if (reg.Y >= (ope.value & 0xFF)) reg.SR |= CARRY;
|
if (reg.Y >= ope.value) reg.SR |= CARRY;
|
||||||
else reg.SR &= ~CARRY;
|
else reg.SR &= ~CARRY;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -395,61 +396,43 @@ static void EOR(){ // Exclusive Or with A
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void BIT(){ // BIT with A - http://www.6502.org/tutorials/vflag.html
|
static void BIT(){ // BIT with A - http://www.6502.org/tutorials/vflag.html
|
||||||
if (!(reg.A & ope.value)) reg.SR |= ZERO;
|
if (reg.A & ope.value) reg.SR &= ~ZERO;
|
||||||
else reg.SR &= ~ZERO;
|
else reg.SR |= ZERO;
|
||||||
reg.SR = (reg.SR & 0x3F) | (ope.value & 0xC0); // update SIGN & OVERFLOW
|
reg.SR = (reg.SR & 0x3F) | (ope.value & 0xC0); // update SIGN & OVERFLOW
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void makeUpdates(uint8_t val){
|
||||||
|
if (ope.setAcc) reg.A = val;
|
||||||
|
else writeMem(ope.address, val);
|
||||||
|
ope.setAcc = false;
|
||||||
|
setSZ(val);
|
||||||
|
}
|
||||||
|
|
||||||
static void ASL(){ // Arithmetic Shift Left
|
static void ASL(){ // Arithmetic Shift Left
|
||||||
uint16_t result = (ope.value << 1);
|
uint16_t result = (ope.value << 1);
|
||||||
if (result & 0xFF00) reg.SR |= CARRY;
|
if (result & 0xFF00) reg.SR |= CARRY;
|
||||||
else reg.SR &= ~CARRY;
|
else reg.SR &= ~CARRY;
|
||||||
result &= 0xFF;
|
makeUpdates((uint8_t)(result & 0xFF));
|
||||||
if (ope.setAcc){
|
|
||||||
reg.A = result;
|
|
||||||
ope.setAcc = false;
|
|
||||||
}
|
|
||||||
else writeMem(ope.address, result);
|
|
||||||
setSZ(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void LSR(){ // Logical Shift Right
|
static void LSR(){ // Logical Shift Right
|
||||||
uint8_t result8;
|
|
||||||
if (ope.value & 1) reg.SR |= CARRY;
|
if (ope.value & 1) reg.SR |= CARRY;
|
||||||
else reg.SR &= ~CARRY;
|
else reg.SR &= ~CARRY;
|
||||||
result8 = (ope.value >> 1) & 0xFF;
|
makeUpdates((uint8_t)((ope.value >> 1) & 0xFF));
|
||||||
if (ope.setAcc){
|
|
||||||
reg.A = result8;
|
|
||||||
ope.setAcc = false;
|
|
||||||
}
|
|
||||||
else writeMem(ope.address, result8);
|
|
||||||
setSZ(result8);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ROL(){ // ROtate Left
|
static void ROL(){ // ROtate Left
|
||||||
uint16_t result = ((ope.value << 1) | (reg.SR & CARRY));
|
uint16_t result = ((ope.value << 1) | (reg.SR & CARRY));
|
||||||
if (result & 0x100) reg.SR |= CARRY;
|
if (result & 0x100) reg.SR |= CARRY;
|
||||||
else reg.SR &= ~CARRY;
|
else reg.SR &= ~CARRY;
|
||||||
result &= 0xFF;
|
makeUpdates((uint8_t)(result & 0xFF));
|
||||||
if (ope.setAcc){
|
|
||||||
reg.A = result;
|
|
||||||
ope.setAcc = false;
|
|
||||||
}
|
|
||||||
else writeMem(ope.address, result);
|
|
||||||
setSZ(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ROR(){ // ROtate Right
|
static void ROR(){ // ROtate Right
|
||||||
uint16_t result = (ope.value >> 1) | ((reg.SR & CARRY) << 7);
|
uint16_t result = (ope.value >> 1) | ((reg.SR & CARRY) << 7);
|
||||||
if (ope.value & 0x1) reg.SR |= CARRY;
|
if (ope.value & 0x1) reg.SR |= CARRY;
|
||||||
else reg.SR &= ~CARRY;
|
else reg.SR &= ~CARRY;
|
||||||
result &= 0xFF;
|
makeUpdates((uint8_t)(result & 0xFF));
|
||||||
if (ope.setAcc){
|
|
||||||
reg.A = result;
|
|
||||||
ope.setAcc = false;
|
|
||||||
}
|
|
||||||
else writeMem(ope.address, result);
|
|
||||||
setSZ(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ADC(){ // ADd with Carry
|
static void ADC(){ // ADd with Carry
|
||||||
@ -542,19 +525,19 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
// main loop
|
// main loop
|
||||||
while(1){
|
while(1){
|
||||||
for (i=0; i<100; i++){ // executes 100 instructions before a kbd scan
|
for (i=0; i<100; i++){ // execute 100 instructions before a kbd scan
|
||||||
opcode = readMem(reg.PC++); // FETCH and increment the Program Counter
|
opcode = readMem(reg.PC++); // FETCH and increment the Program Counter
|
||||||
addressing[opcode](); // DECODE operands against the addressing mode
|
addressing[opcode](); // DECODE operands against the addressing mode
|
||||||
instruction[opcode](); // EXEC the instruction
|
instruction[opcode](); // EXEC the instruction
|
||||||
}
|
}
|
||||||
|
|
||||||
// keyboard controller
|
// keyboard controller
|
||||||
ch = getch(); // non blocking keybd read from ncurses
|
if (!keyRdy){ // only if not already a key in wait
|
||||||
if (ch != ERR){
|
if ((ch = getch()) != ERR){ // non blocking keybd read from ncurses
|
||||||
key = (uint8_t)ch; // getch() returns an int
|
key = (uint8_t)ch; // getch() returns an int
|
||||||
if (key == 0x12) reset(); // CTRL-R, reset
|
if (key == 0x12) reset(); // CTRL-R, reset
|
||||||
else if (key == 0x02) BRK(); // CTRL-B, break
|
else if (key == 0x02) BRK(); // CTRL-B, break
|
||||||
else if (!keyRdy){ // only if not already a key in wait
|
else {
|
||||||
if (key == 0x0A) key = 0x0D; // LF (\n) to CR (\r)
|
if (key == 0x0A) key = 0x0D; // LF (\n) to CR (\r)
|
||||||
if ((key == 0x7F) || (key == 0x08)) key = 0x5F; // DEL and BS to _
|
if ((key == 0x7F) || (key == 0x08)) key = 0x5F; // DEL and BS to _
|
||||||
if ((key >= 0x61) && (key <= 0x7A)) key &= 0xDF; // to upper case
|
if ((key >= 0x61) && (key <= 0x7A)) key &= 0xDF; // to upper case
|
||||||
@ -562,4 +545,5 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user