mirror of
https://github.com/jborza/emu6502.git
synced 2025-02-19 07:30:57 +00:00
testing machine code, ORA
This commit is contained in:
parent
8ddf9bf8ff
commit
b7b199129d
91
cpu.c
91
cpu.c
@ -8,12 +8,18 @@ void* unimplemented_instruction(State6502* state) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void set_NV_flags(State6502* state, word value) {
|
||||
//if(value)
|
||||
void set_NV_flags(State6502* state, byte value) {
|
||||
//TODO implement V flag
|
||||
if (value) {
|
||||
state->flags.z = 0;
|
||||
}
|
||||
else {
|
||||
state->flags.z = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void clear_flags(State6502* state) {
|
||||
memcpy(&state->flags, &state->a,1);
|
||||
memcpy(&state->flags, &state->a, 1);
|
||||
}
|
||||
|
||||
void clear_state(State6502* state) {
|
||||
@ -50,24 +56,87 @@ void print_memory(State6502* state, word offset) {
|
||||
|
||||
//bitwise or with accumulator
|
||||
void ORA(State6502 * state, byte operand) {
|
||||
word result = state->a | operand;
|
||||
byte result = state->a | operand;
|
||||
set_NV_flags(state, result);
|
||||
state->a = result & 0xff;
|
||||
state->a = result;
|
||||
}
|
||||
|
||||
//load accumulator
|
||||
void LDA(State6502* state, byte operand) {
|
||||
state->a = operand;
|
||||
set_NV_flags(state, state->a);
|
||||
}
|
||||
|
||||
word pop_word(State6502 * state) {
|
||||
byte low = pop_byte(state);
|
||||
byte high = pop_byte(state);
|
||||
word result = (high << 8) | low;
|
||||
return result;
|
||||
}
|
||||
|
||||
word get_word(State6502 * state, word address) {
|
||||
return state->memory[address] << 8 | state->memory[address + 1];
|
||||
}
|
||||
|
||||
int emulate_6502_op(State6502 * state) {
|
||||
byte* opcode = &state->memory[state->pc++];
|
||||
switch (*opcode) {
|
||||
case 0x00: break; //NOP
|
||||
case BRK: state->running = 0;
|
||||
state->flags.b = 1;
|
||||
break; //BRK
|
||||
case NOP: break; //NOP
|
||||
case ORA_IND_X: //ORA, indirect, x
|
||||
unimplemented_instruction(state);
|
||||
break;
|
||||
case ORA_ZP: //ORA, zero page
|
||||
byte zp = pop_byte(state);
|
||||
ORA(state, state->memory[zp]);
|
||||
{
|
||||
byte address = pop_byte(state);
|
||||
ORA(state, state->memory[address]);
|
||||
break;
|
||||
case 0x09: //ORA, immediate
|
||||
|
||||
|
||||
}
|
||||
case ORA_IND_Y:
|
||||
//The value in Y is added to the address at the little-endian address stored at the two-byte pair of the specified address (LSB) and the specified address plus one (MSB).
|
||||
//The value at the sum address is used to perform the computation. Indeed addressing mode actually repeats exactly the accumulator register's digits.
|
||||
// Example
|
||||
//The value $03 in Y is added to the address $C235 at addresses $002A and $002B for a sum of $C238. The value $2F at $C238 is shifted right (yielding $17) and written back to $C238.
|
||||
//word address = pop_word(state) + state->y;
|
||||
//word value = get_word(state, address);
|
||||
//ORA(state, state->memory[value]);
|
||||
unimplemented_instruction(state);
|
||||
break;
|
||||
case ORA_IMM:
|
||||
ORA(state, pop_byte(state));
|
||||
break;
|
||||
case ORA_ZP_X:
|
||||
{
|
||||
byte address = pop_byte(state) + state->x;
|
||||
ORA(state, state->memory[address]);
|
||||
break;
|
||||
}
|
||||
case ORA_ABS:
|
||||
{
|
||||
word address = pop_word(state);
|
||||
ORA(state, state->memory[address]);
|
||||
break;
|
||||
}
|
||||
case ORA_ABS_X:
|
||||
{
|
||||
word address = pop_word(state) + state->x;
|
||||
ORA(state, state->memory[address]);
|
||||
break;
|
||||
}
|
||||
case ORA_ABS_Y:
|
||||
{
|
||||
word address = pop_word(state) + state->y;
|
||||
ORA(state, state->memory[address]);
|
||||
break;
|
||||
}
|
||||
case LDA_IMM:
|
||||
{
|
||||
LDA(state,pop_byte(state));
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
unimplemented_instruction(state); break;
|
||||
}
|
||||
|
3
disassembler.h
Normal file
3
disassembler.h
Normal file
@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
#include <stdio.h>
|
||||
void disassemble_6502(byte* buffer, word pc);
|
44
emu6502.c
44
emu6502.c
@ -4,10 +4,50 @@
|
||||
#include <stdio.h>
|
||||
#include "state.h"
|
||||
#include "cpu.h"
|
||||
#include "disassembler.h"
|
||||
|
||||
byte* read_game() {
|
||||
FILE* file = fopen("..\\bins\\tetris.bin", "r");
|
||||
byte* buffer = malloc(0x800);
|
||||
fread(buffer, 1, 0x800, file);
|
||||
fclose(file);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void print_all(State6502* state) {
|
||||
print_state(state);
|
||||
print_memory(state, 0);
|
||||
print_memory(state, 0x20);
|
||||
print_memory(state, 0x40);
|
||||
print_memory(state, 0x80);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("Hello World!\n");
|
||||
printf("Hello World!\n");
|
||||
State6502 state;
|
||||
emulate_6502_op(&state);
|
||||
print_state(&state);
|
||||
clear_state(&state);
|
||||
byte* memory = read_game();
|
||||
//state.memory = read_game();
|
||||
state.memory = malloc(4096);
|
||||
state.memory[0] = 0xEA;
|
||||
state.memory[1] = 0x05; //ORA $a0
|
||||
state.memory[2] = 0xA0;
|
||||
state.memory[3] = 0xEA; //NOP
|
||||
state.memory[4] = 0x09; //ORA #$ff
|
||||
state.memory[5] = 0xff;
|
||||
state.memory[6] = 0xA9; //LDA
|
||||
state.memory[7] = 0x00; //0
|
||||
state.memory[8] = 0x0D; //ORA $1234
|
||||
state.memory[9] = 0x34;
|
||||
state.memory[10] = 0x12;
|
||||
state.memory[0xA0] = 0x13;
|
||||
state.memory[0x1234] = 0xAA;
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
print_all(&state);
|
||||
disassemble_6502(state.memory, state.pc);
|
||||
emulate_6502_op(&state);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user