Skip to content

Commit

Permalink
simulator: Compilation fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
agkaminski committed May 8, 2018
1 parent df94826 commit f7e06d8
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 107 deletions.
4 changes: 2 additions & 2 deletions simulator/addrmode.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ static argtype_t modeRelative(cpustate_t *cpu, u8 *args, cycles_t *cycles)
addr = cpu->pc;
addr += rel;

arg[0] = addr & 0xff;
arg[1] = (addr >> 8) & 0xff;
args[0] = addr & 0xff;
args[1] = (addr >> 8) & 0xff;

DEBUG("Relative mode, args: 0x%02x%02x = pc + rel: 0x%02x", args[1], args[0], rel);

Expand Down
1 change: 1 addition & 0 deletions simulator/addrmode.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define _ADDRMODE_H_

#include "types.h"
#include "decoder.h"

typedef enum { arg_none, arg_byte, arg_addr } argtype_t;

Expand Down
26 changes: 13 additions & 13 deletions simulator/alu.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void alu_flags(u16 result, u8 *flags, u8 mask)
}

if (mask & flag_zero) {
if (result & 0xff == 0)
if ((result & 0xff) == 0)
*flags |= flag_zero;
else
*flags &= ~flag_zero;
Expand All @@ -35,7 +35,7 @@ u8 alu_add(u8 a, u8 b, u8 *flags)
if (*flags & flag_carry)
++result;

if (flags & flag_bcd) {
if (*flags & flag_bcd) {
if ((a & 0xf) + (b & 0xf) > 9)
result += 0x06;

Expand All @@ -46,7 +46,7 @@ u8 alu_add(u8 a, u8 b, u8 *flags)
alu_flags(result, flags, flag_carry | flag_sign | flag_zero);

if ((a ^ result) & (b ^ result) & 0x80)
*flags |= flag_ovfl;
*flags |= flag_ovrf;

return result & 0xff;
}
Expand All @@ -60,7 +60,7 @@ u8 alu_sub(u8 a, u8 b, u8 *flags)
if (!(*flags & flag_carry))
++result;

if (flags & flag_bcd) {
if (*flags & flag_bcd) {
if ((a & 0xf) + (b & 0xf) > 9)
result += 0x06;

Expand All @@ -71,7 +71,7 @@ u8 alu_sub(u8 a, u8 b, u8 *flags)
alu_flags(result, flags, flag_carry | flag_sign | flag_zero);

if ((a ^ result) & (b ^ result) & 0x80)
*flags |= flag_ovfl;
*flags |= flag_ovrf;

return result & 0xff;
}
Expand Down Expand Up @@ -137,15 +137,15 @@ u8 alu_rol(u8 a, u8 b, u8 *flags)

result = a << 1;

if (flags & flag_carry)
if (*flags & flag_carry)
result |= 1;

alu_flags(result, flags, flag_sign | flag_zero);

if (a & 0x80)
flags |= flag_carry;
*flags |= flag_carry;
else
flags &= ~flag_carry;
*flags &= ~flag_carry;

return result;
}
Expand All @@ -156,15 +156,15 @@ u8 alu_ror(u8 a, u8 b, u8 *flags)

result = a >> 1;

if (flags & flag_carry)
if (*flags & flag_carry)
result |= 0x80;

alu_flags(result, flags, flag_sign | flag_zero);

if (a & 0x80)
flags |= flag_carry;
*flags |= flag_carry;
else
flags &= ~flag_carry;
*flags &= ~flag_carry;

return result;
}
Expand Down Expand Up @@ -200,9 +200,9 @@ u8 alu_bit(u8 a, u8 b, u8 *flags)
alu_flags(result, flags, flag_sign | flag_zero);

if (result & 0x40)
flags |= flag_ovfl;
*flags |= flag_ovrf;
else
flags &= ~flag_ovfl;
*flags &= ~flag_ovrf;

return result;
}
Expand Down
5 changes: 3 additions & 2 deletions simulator/bus.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bus.h"
#include "error.h"

Expand Down Expand Up @@ -79,7 +80,7 @@ static void bus_treeCleanup(bustree_t *root)
free(root->entry);

if (root != &bustree_root)
free(root)
free(root);
}

static bustree_t *bus_treeFind(bustree_t *root, u16 addr)
Expand All @@ -88,7 +89,7 @@ static bustree_t *bus_treeFind(bustree_t *root, u16 addr)
return NULL;

if (root->entry->begin >= addr && root->entry->end <= addr)
return root->entry;
return root;

if (root->entry->end > addr)
return bus_treeFind(root->left, addr);
Expand Down
28 changes: 8 additions & 20 deletions simulator/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,24 @@ struct {
int rst_state;
int step;
int run;
int end;

mutex_t mutex;
cond_t cond;
} core_global;

enum { STATE_STALL, STATE_NORMAL, STATE_NMI, STATE_IRQ, STATE_RST, STATE_END };

static core_reset(cpustate_t *cpu, cycles_t *cycles)
static void core_reset(cpustate_t *cpu, cycles_t *cycles)
{
exec_rst(cpu, cycles);
}

static core_nmi(cpustate_t *cpu, cycles_t *cycles)
static void core_nmi(cpustate_t *cpu, cycles_t *cycles)
{
exec_nmi(cpu, cycles);
}

static core_irq(cpustate_t *cpu, cycles_t *cycles)
static void core_irq(cpustate_t *cpu, cycles_t *cycles)
{
exec_irq(cpu, cycles);
}
Expand All @@ -48,13 +47,13 @@ static void core_normal(cpustate_t *cpu, cycles_t *cycles)
u8 args[2];

instruction = decode(core_nextpc(cpu));
argtype = addrmode_getArgs(cpu, args, instruction.mode, &cycles);
exec_execute(cpu, instruction.opcode, argtype, args, &cycles);
argtype = addrmode_getArgs(cpu, args, instruction.mode, cycles);
exec_execute(cpu, instruction.opcode, argtype, args, cycles);

*cycles += 1;
}

static void core_thread(void *arg)
static void *core_thread(void *arg)
{
cycles_t cycles;
int lastNmi;
Expand Down Expand Up @@ -87,10 +86,6 @@ static void core_thread(void *arg)
core_global.state = STATE_NORMAL;
break;
}
else if (core_global.end != 0) {
core_global.state = STATE_END;
break;
}
else {
core_global.state = STATE_STALL;
}
Expand Down Expand Up @@ -138,6 +133,8 @@ static void core_thread(void *arg)

thread_sleep(TIME_PER_CYCLE * cycles);
}

return NULL;
}


Expand Down Expand Up @@ -204,14 +201,6 @@ void core_setMode(coremode_t mode)
unlock(&core_global.mutex);
}

void core_kill(void)
{
lock(&core_global.mutex);
core_global.end = 1;
thread_signal(&core_global.cond);
unlock(&core_global.mutex);
}

int core_step(void)
{
lock(&core_global.mutex);
Expand All @@ -234,7 +223,6 @@ int core_init(thread_t *thread)
core_global.rst_state = 1;
core_global.step = 0;
core_global.run = 0;
core_global.end = 0;

mutex_init(&core_global.mutex);
thread_condInit(&core_global.cond);
Expand Down
15 changes: 11 additions & 4 deletions simulator/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

#define NDEBUG 0

Expand All @@ -13,8 +14,10 @@

static inline void fatal(const char *fname, int lineno, const char *fcname, const char *msg, ...)
{
va_list va_arg;

fprintf(stderr, RED "[FATAL] [%s] @%d (%s): ", fname, lineno, fcname);
fprintf(stderr, msg, __VA_ARGS__);
fprintf(stderr, msg, va_arg);
fprintf(stderr, "\nSimulation stopped.\n" RESET);
fprintf(stderr, "Press Any Key to Exit...\n");
getchar();
Expand All @@ -23,20 +26,24 @@ static inline void fatal(const char *fname, int lineno, const char *fcname, cons

static inline void warn(const char *fname, int lineno, const char *fcname, const char *msg, ...)
{
va_list va_arg;

fprintf(stderr, YEL "[WARNING] [%s] @%d (%s): ", fname, lineno, fcname);
fprintf(stderr, msg, __VA_ARGS__);
fprintf(stderr, msg, va_arg);
fprintf(stderr, "\n" RESET);
}

static inline void debug(const char *fname, int lineno, const char *fcname, const char *msg, ...)
{
va_list va_arg;

fprintf(stderr, GRN "[DEBUG] [%s] @%d (%s): ", fname, lineno, fcname);
fprintf(stderr, msg, __VA_ARGS__);
fprintf(stderr, msg, va_arg);
fprintf(stderr, "\n" RESET);
}

#define FATAL(msg, ...) fatal(__FILE__, __LINE__, __func__, msg, ##__VA_ARGS__)

#define WARN(msg, ...) warn(__FILE__, __LINE__, __func__, msg, ##__VA_ARGS__)

#define DEBUG(msg, ...) debug(__FILE__, __LINE__, __func__, msg, ##__VA_ARGS__)
Expand Down
21 changes: 12 additions & 9 deletions simulator/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#define RST_VECTOR 0xfffc
#define NMI_VECTOR 0xfffa

typedef void (*exec_func_t)(cpustate_t*, argtype_t, u8*, cycles_t*);

static void exec_push(cpustate_t *cpu, u8 data)
{
u16 addr;
Expand Down Expand Up @@ -404,7 +406,7 @@ static void exec_dec(cpustate_t *cpu, argtype_t argtype, u8 *args, cycles_t *cyc

static void exec_dex(cpustate_t *cpu, argtype_t argtype, u8 *args, cycles_t *cycles)
{
cpu->x = alu_dec(cpu->x, 0, flags);
cpu->x = alu_dec(cpu->x, 0, &cpu->flags);

DEBUG("Performing DEX, result 0x%02x", cpu->x);

Expand All @@ -413,7 +415,7 @@ static void exec_dex(cpustate_t *cpu, argtype_t argtype, u8 *args, cycles_t *cyc

static void exec_dey(cpustate_t *cpu, argtype_t argtype, u8 *args, cycles_t *cycles)
{
cpu->y = alu_dec(cpu->y, 0, flags);
cpu->y = alu_dec(cpu->y, 0, &cpu->flags);

DEBUG("Performing DEY, result 0x%02x", cpu->y);

Expand Down Expand Up @@ -471,7 +473,7 @@ static void exec_inc(cpustate_t *cpu, argtype_t argtype, u8 *args, cycles_t *cyc

static void exec_inx(cpustate_t *cpu, argtype_t argtype, u8 *args, cycles_t *cycles)
{
cpu->x = alu_inc(cpu->x, 0, flags);
cpu->x = alu_inc(cpu->x, 0, &cpu->flags);

DEBUG("Performing INX, result 0x%02x", cpu->x);

Expand All @@ -480,7 +482,7 @@ static void exec_inx(cpustate_t *cpu, argtype_t argtype, u8 *args, cycles_t *cyc

static void exec_iny(cpustate_t *cpu, argtype_t argtype, u8 *args, cycles_t *cycles)
{
cpu->y = alu_inc(cpu->y, 0, flags);
cpu->y = alu_inc(cpu->y, 0, &cpu->flags);

DEBUG("Performing INY, result 0x%02x", cpu->y);

Expand Down Expand Up @@ -535,7 +537,7 @@ static void exec_lda(cpustate_t *cpu, argtype_t argtype, u8 *args, cycles_t *cyc

DEBUG("Performing LDY of 0x%02x", arg);

cpu->a = alu_load(arg, 0, flags);
cpu->a = alu_load(arg, 0, &cpu->flags);
}

static void exec_ldx(cpustate_t *cpu, argtype_t argtype, u8 *args, cycles_t *cycles)
Expand All @@ -555,7 +557,7 @@ static void exec_ldx(cpustate_t *cpu, argtype_t argtype, u8 *args, cycles_t *cyc

DEBUG("Performing LDY of 0x%02x", arg);

cpu->x = alu_load(arg, 0, flags);
cpu->x = alu_load(arg, 0, &cpu->flags);
}

static void exec_ldy(cpustate_t *cpu, argtype_t argtype, u8 *args, cycles_t *cycles)
Expand All @@ -575,7 +577,7 @@ static void exec_ldy(cpustate_t *cpu, argtype_t argtype, u8 *args, cycles_t *cyc

DEBUG("Performing LDY of 0x%02x", arg);

cpu->y = alu_load(arg, 0, flags);
cpu->y = alu_load(arg, 0, &cpu->flags);
}

static void exec_lsr(cpustate_t *cpu, argtype_t argtype, u8 *args, cycles_t *cycles)
Expand Down Expand Up @@ -627,7 +629,7 @@ static void exec_ora(cpustate_t *cpu, argtype_t argtype, u8 *args, cycles_t *cyc
*cycles += 1;
}

cpu->a = alu_ora(cpu->a, arg, &cpu->flags);
cpu->a = alu_or(cpu->a, arg, &cpu->flags);

DEBUG("Performing ORA 0x%02x, Acc, result 0x%02x", arg, cpu->a);
}
Expand Down Expand Up @@ -913,7 +915,7 @@ static void exec_tya(cpustate_t *cpu, argtype_t argtype, u8 *args, cycles_t *cyc
*cycles += 1;
}

static const void (*exec_instr)(cpustate_t*, argtype_t, u8*, cycles_t*)[] = {
static const exec_func_t exec_instr[] = {
exec_adc, exec_and, exec_asl, exec_bcc, exec_bcs, exec_beq, exec_bit, exec_bmi,
exec_bne, exec_bpl, exec_brk, exec_bvc, exec_bvs, exec_clc, exec_cld, exec_cli,
exec_clv, exec_cmp, exec_cpx, exec_cpy, exec_dec, exec_dex, exec_dey, exec_eor,
Expand Down Expand Up @@ -982,6 +984,7 @@ void exec_rst(cpustate_t *cpu, cycles_t *cycles)

cpu->a = 0;
cpu->x = 0;
cpu->y = 0;
cpu->flags = flag_one;
cpu->sp = 0xff;

Expand Down
2 changes: 2 additions & 0 deletions simulator/exec.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define _EXEC_H_

#include "types.h"
#include "decoder.h"
#include "addrmode.h"

void exec_execute(cpustate_t *cpu, opcode_t instruction, argtype_t argtype, u8 *args, cycles_t *cycles);

Expand Down
Loading

0 comments on commit f7e06d8

Please sign in to comment.