Skip to content

Commit

Permalink
it works again
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlie Somerville committed Jan 3, 2011
1 parent 368cd92 commit af344b3
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 6 deletions.
Binary file modified 6502
Binary file not shown.
1 change: 1 addition & 0 deletions Makefile
Expand Up @@ -3,6 +3,7 @@ CFLAGS=-g -Wall -std=c99 -Iinc

all:
$(CC) $(CFLAGS) src/*.c -o 6502
util/build

clean:
rm 6502
3 changes: 2 additions & 1 deletion inc/addressing_modes.h
Expand Up @@ -7,8 +7,9 @@ typedef unsigned short(*addrmode_t)(cpu_t*);

#define ADDRMODE(mode) unsigned short addrmode_##mode(cpu_t* cpu)

ADDRMODE(none);
ADDRMODE(implied);
ADDRMODE(imm8);
ADDRMODE(imm16);
ADDRMODE(relative);

#endif
5 changes: 5 additions & 0 deletions inc/cpu.h
Expand Up @@ -14,6 +14,8 @@
#define SET_FLAG(state,flag) (state)->regs.flags |= (flag)
#define CLEAR_FLAG(state,flag) (state)->regs.flags &= ~(flag);

#define FLAG_IF(state,flag,condition) if(condition) { SET_FLAG(state,flag); } else { CLEAR_FLAG(state,flag); }

typedef struct reg
{
unsigned short pc;
Expand Down Expand Up @@ -69,4 +71,7 @@ void cpu_mmap(cpu_t* cpu, mmapseg_t* segment);
unsigned char cpu_peek(cpu_t* cpu, unsigned short address);
void cpu_poke(cpu_t* cpu, unsigned short address, unsigned char val);

unsigned short cpu_peek_16(cpu_t* cpu, unsigned short address);
void cpu_poke_16(cpu_t* cpu, unsigned short address, unsigned short val);

#endif
3 changes: 3 additions & 0 deletions inc/instructions.h
Expand Up @@ -8,5 +8,8 @@ typedef void(*ins_t)(cpu_t*, unsigned short);
#define INS(name) void ins_##name(cpu_t* cpu, unsigned short param)

INS(brk);
INS(lda);
INS(ldx);
INS(rti);

#endif
2 changes: 2 additions & 0 deletions inc/opcodes.h
@@ -1,6 +1,8 @@
#ifndef OPCODES_H
#define OPCODES_H

#define OP(i,a) (ins_##i), (addrmode_##a)

extern void* opcodes[];

#endif
10 changes: 9 additions & 1 deletion src/addressing_modes.c
Expand Up @@ -2,7 +2,7 @@
#include <vm.h>
#include <cpu.h>

ADDRMODE(none)
ADDRMODE(implied)
{
return 0;
}
Expand All @@ -16,3 +16,11 @@ ADDRMODE(imm16)
{
return vm_next_16(cpu);
}

ADDRMODE(relative)
{
signed char offset = (signed char)vm_next_8(cpu);
return cpu->regs.pc + offset;
}


13 changes: 13 additions & 0 deletions src/cpu.c
Expand Up @@ -105,4 +105,17 @@ void cpu_poke(cpu_t* cpu, unsigned short address, unsigned char val)
cpu->mem[address] = val;
}

unsigned short cpu_peek_16(cpu_t* cpu, unsigned short address)
{
unsigned char lsb = cpu_peek(cpu, address);
unsigned char msb = cpu_peek(cpu, address + 1);

return msb << 8 | lsb;
}

void cpu_poke_16(cpu_t* cpu, unsigned short address, unsigned short val)
{
cpu_poke(cpu, address, val & 255);
cpu_poke(cpu, address + 1, val >> 8);
}

22 changes: 22 additions & 0 deletions src/instructions.c
Expand Up @@ -5,3 +5,25 @@ INS(brk)
{
cpu_brk(cpu);
}

INS(lda)
{
cpu->regs.a = param & 255;

FLAG_IF(cpu, FZERO, cpu->regs.a == 0);
FLAG_IF(cpu, FNEG, cpu->regs.a & 128);
}

INS(ldx)
{
cpu->regs.x = param & 255;

FLAG_IF(cpu, FZERO, cpu->regs.x == 0);
FLAG_IF(cpu, FNEG, cpu->regs.x & 128);
}

INS(rti)
{
cpu->regs.flags = cpu_pop_8(cpu);
cpu->regs.pc = cpu_pop_16(cpu);
}
8 changes: 4 additions & 4 deletions src/opcodes.c
Expand Up @@ -6,7 +6,7 @@
void* opcodes[] = {

// 00
ins_brk, addrmode_none,
OP(brk, implied),
NULL, NULL,
NULL, NULL,
NULL, NULL,
Expand Down Expand Up @@ -86,7 +86,7 @@ void* opcodes[] = {
NULL, NULL,
NULL, NULL,
// 40
NULL, NULL,
OP(rti, implied),
NULL, NULL,
NULL, NULL,
NULL, NULL,
Expand Down Expand Up @@ -208,7 +208,7 @@ void* opcodes[] = {
// A0
NULL, NULL,
NULL, NULL,
NULL, NULL,
OP(ldx, imm8),
NULL, NULL,
// A4
NULL, NULL,
Expand All @@ -217,7 +217,7 @@ void* opcodes[] = {
NULL, NULL,
// A8
NULL, NULL,
NULL, NULL,
OP(lda, imm8),
NULL, NULL,
NULL, NULL,
// AC
Expand Down
Binary file modified test.img
Binary file not shown.
3 changes: 3 additions & 0 deletions util/test.asm
Expand Up @@ -4,6 +4,9 @@
lda #$BE
ldx #$EF

hi:
jmp hi

nmi:
rti
reset:
Expand Down
Binary file modified util/test.o
Binary file not shown.

0 comments on commit af344b3

Please sign in to comment.