2015-01-14 00:42:14 +00:00
|
|
|
// The MIT License (MIT)
|
|
|
|
//
|
|
|
|
// Copyright (c) 2015 Stefan Arentz - http://github.com/st3fan/ewm
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
// SOFTWARE.
|
|
|
|
|
|
|
|
#ifndef CPU_H
|
|
|
|
#define CPU_H
|
|
|
|
|
2016-11-17 03:05:08 +00:00
|
|
|
#include <stdbool.h>
|
2015-01-14 00:42:14 +00:00
|
|
|
#include <stdint.h>
|
2016-11-22 20:55:35 +00:00
|
|
|
#include <stdio.h>
|
2015-01-14 00:42:14 +00:00
|
|
|
|
2016-11-24 03:17:24 +00:00
|
|
|
#define EWM_CPU_MODEL_6502 0
|
|
|
|
#define EWM_CPU_MODEL_65C02 1
|
|
|
|
|
2016-11-16 20:43:10 +00:00
|
|
|
#define EWM_CPU_ERR_UNIMPLEMENTED_INSTRUCTION (-1)
|
|
|
|
#define EWM_CPU_ERR_STACK_OVERFLOW (-2)
|
|
|
|
#define EWM_CPU_ERR_STACK_UNDERFLOW (-3)
|
|
|
|
|
2016-11-15 02:13:51 +00:00
|
|
|
#define EWM_VECTOR_NMI 0xfffa
|
|
|
|
#define EWM_VECTOR_RES 0xfffc
|
|
|
|
#define EWM_VECTOR_IRQ 0xfffe
|
|
|
|
|
2016-11-24 03:17:24 +00:00
|
|
|
struct cpu_instruction_t;
|
|
|
|
|
2015-01-14 00:42:14 +00:00
|
|
|
struct cpu_state_t {
|
2016-11-13 20:29:49 +00:00
|
|
|
uint8_t a, x, y, s, sp;
|
2015-01-14 00:42:14 +00:00
|
|
|
uint16_t pc;
|
|
|
|
uint8_t n, v, b, d, i, z, c;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct cpu_t {
|
2016-11-24 03:17:24 +00:00
|
|
|
int model;
|
2016-11-18 21:50:31 +00:00
|
|
|
struct cpu_state_t state;
|
|
|
|
FILE *trace;
|
|
|
|
bool strict;
|
|
|
|
struct mem_t *mem;
|
2016-11-24 03:17:24 +00:00
|
|
|
struct cpu_instruction_t *instructions;
|
2015-01-14 00:42:14 +00:00
|
|
|
};
|
|
|
|
|
2016-12-05 08:49:38 +00:00
|
|
|
#define MEM_FLAGS_READ 0x01
|
|
|
|
#define MEM_FLAGS_WRITE 0x02
|
2016-11-15 01:10:42 +00:00
|
|
|
|
|
|
|
typedef uint8_t (*mem_read_handler_t)(struct cpu_t *cpu, struct mem_t *mem, uint16_t addr);
|
|
|
|
typedef void (*mem_write_handler_t)(struct cpu_t *cpu, struct mem_t *mem, uint16_t addr, uint8_t b);
|
|
|
|
|
|
|
|
struct mem_t {
|
2016-12-05 08:49:38 +00:00
|
|
|
bool enabled;
|
|
|
|
uint8_t flags;
|
2016-11-15 01:10:42 +00:00
|
|
|
uint16_t start;
|
2016-11-22 20:55:35 +00:00
|
|
|
uint16_t end;
|
2016-11-15 01:10:42 +00:00
|
|
|
void *obj;
|
|
|
|
mem_read_handler_t read_handler;
|
|
|
|
mem_write_handler_t write_handler;
|
|
|
|
struct mem_t *next;
|
|
|
|
};
|
2016-11-13 20:29:49 +00:00
|
|
|
|
|
|
|
// Private. How do we keep them private?
|
|
|
|
void _cpu_push_byte(struct cpu_t *cpu, uint8_t b);
|
|
|
|
void _cpu_push_word(struct cpu_t *cpu, uint16_t w);
|
|
|
|
uint8_t _cpu_pull_byte(struct cpu_t *cpu);
|
|
|
|
uint16_t _cpu_pull_word(struct cpu_t *cpu);
|
|
|
|
|
2016-11-17 03:05:08 +00:00
|
|
|
uint8_t _cpu_stack_free(struct cpu_t *cpu);
|
|
|
|
uint8_t _cpu_stack_used(struct cpu_t *cpu);
|
|
|
|
|
2016-11-14 01:07:54 +00:00
|
|
|
// Private. How do we keep them private?
|
|
|
|
uint8_t _cpu_get_status(struct cpu_t *cpu);
|
|
|
|
void _cpu_set_status(struct cpu_t *cpu, uint8_t status);
|
|
|
|
|
2016-11-24 03:17:24 +00:00
|
|
|
void cpu_setup();
|
|
|
|
|
|
|
|
void cpu_init(struct cpu_t *cpu, int model);
|
2016-11-18 21:50:31 +00:00
|
|
|
void cpu_shutdown(struct cpu_t *cpu);
|
2016-11-15 01:10:42 +00:00
|
|
|
|
2016-11-30 02:05:55 +00:00
|
|
|
struct mem_t *cpu_add_mem(struct cpu_t *cpu, struct mem_t *mem);
|
|
|
|
struct mem_t *cpu_add_ram(struct cpu_t *cpu, uint16_t start, uint16_t end);
|
|
|
|
struct mem_t *cpu_add_ram_data(struct cpu_t *cpu, uint16_t start, uint16_t end, uint8_t *data);
|
|
|
|
struct mem_t *cpu_add_ram_file(struct cpu_t *cpu, uint16_t start, char *path);
|
|
|
|
struct mem_t *cpu_add_rom_data(struct cpu_t *cpu, uint16_t start, uint16_t end, uint8_t *data);
|
|
|
|
struct mem_t *cpu_add_rom_file(struct cpu_t *cpu, uint16_t start, char *path);
|
|
|
|
struct mem_t *cpu_add_iom(struct cpu_t *cpu, uint16_t start, uint16_t end, void *obj, mem_read_handler_t read_handler, mem_write_handler_t write_handler);
|
2015-01-14 00:42:14 +00:00
|
|
|
|
2016-11-17 03:05:08 +00:00
|
|
|
void cpu_strict(struct cpu_t *cpu, bool strict);
|
2016-11-18 21:50:31 +00:00
|
|
|
int cpu_trace(struct cpu_t *cpu, char *path);
|
2015-01-14 00:42:14 +00:00
|
|
|
|
|
|
|
void cpu_reset(struct cpu_t *cpu);
|
2016-11-17 03:05:08 +00:00
|
|
|
int cpu_irq(struct cpu_t *cpu);
|
|
|
|
int cpu_nmi(struct cpu_t *cpu);
|
2015-01-14 00:42:14 +00:00
|
|
|
|
2016-11-16 20:43:10 +00:00
|
|
|
int cpu_run(struct cpu_t *cpu);
|
|
|
|
int cpu_boot(struct cpu_t *cpu);
|
|
|
|
int cpu_step(struct cpu_t *cpu);
|
2015-01-14 00:42:14 +00:00
|
|
|
|
|
|
|
uint16_t cpu_memory_get_word(struct cpu_t *cpu, uint16_t addr);
|
|
|
|
uint8_t cpu_memory_get_byte(struct cpu_t *cpu, uint16_t addr);
|
|
|
|
|
|
|
|
void cpu_memory_set_word(struct cpu_t *cpu, uint16_t addr, uint16_t v);
|
|
|
|
void cpu_memory_set_byte(struct cpu_t *cpu, uint16_t addr, uint8_t v);
|
|
|
|
|
|
|
|
#endif
|