2016-12-19 01:18:45 +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.
|
|
|
|
|
|
|
|
#include <ctype.h>
|
2016-12-19 01:46:51 +00:00
|
|
|
#include <getopt.h>
|
2016-12-19 01:18:45 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "mem.h"
|
|
|
|
#include "pia.h"
|
|
|
|
#include "tty.h"
|
|
|
|
#include "one.h"
|
|
|
|
|
|
|
|
static void ewm_one_pia_callback(struct ewm_pia_t *pia, void *obj, uint8_t ddr, uint8_t v) {
|
|
|
|
struct ewm_one_t *one = (struct ewm_one_t*) obj;
|
2016-12-26 03:23:37 +00:00
|
|
|
if (one->model == EWM_ONE_MODEL_APPLE1) {
|
|
|
|
v &= 0x7f;
|
|
|
|
}
|
2016-12-19 01:18:45 +00:00
|
|
|
ewm_tty_write(one->tty, v);
|
|
|
|
}
|
|
|
|
|
2017-01-07 13:52:56 +00:00
|
|
|
static int ewm_one_init(struct ewm_one_t *one, int model, SDL_Window *window, SDL_Renderer *renderer) {
|
2016-12-19 01:18:45 +00:00
|
|
|
memset(one, 0, sizeof(struct ewm_one_t));
|
2016-12-19 01:46:51 +00:00
|
|
|
one->model = model;
|
|
|
|
switch (model) {
|
|
|
|
case EWM_ONE_MODEL_APPLE1:
|
2016-12-19 01:18:45 +00:00
|
|
|
one->cpu = cpu_create(EWM_CPU_MODEL_6502);
|
|
|
|
cpu_add_ram(one->cpu, 0x0000, 8 * 1024 - 1);
|
2016-12-19 01:33:04 +00:00
|
|
|
cpu_add_rom_file(one->cpu, 0xff00, "rom/apple1.rom");
|
2017-01-07 13:52:56 +00:00
|
|
|
one->tty = ewm_tty_create(window, renderer);
|
2016-12-19 01:18:45 +00:00
|
|
|
one->pia = ewm_pia_create(one->cpu);
|
|
|
|
one->pia->callback = ewm_one_pia_callback;
|
|
|
|
one->pia->callback_obj = one;
|
|
|
|
break;
|
2016-12-19 01:46:51 +00:00
|
|
|
case EWM_ONE_MODEL_REPLICA1:
|
2016-12-19 01:18:45 +00:00
|
|
|
one->cpu = cpu_create(EWM_CPU_MODEL_65C02);
|
|
|
|
cpu_add_ram(one->cpu, 0x0000, 32 * 1024 - 1);
|
2016-12-19 01:33:04 +00:00
|
|
|
cpu_add_rom_file(one->cpu, 0xe000, "rom/krusader.rom");
|
2017-01-07 13:52:56 +00:00
|
|
|
one->tty = ewm_tty_create(window, renderer);
|
2016-12-19 01:18:45 +00:00
|
|
|
one->pia = ewm_pia_create(one->cpu);
|
|
|
|
one->pia->callback = ewm_one_pia_callback;
|
|
|
|
one->pia->callback_obj = one;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-07 13:52:56 +00:00
|
|
|
struct ewm_one_t *ewm_one_create(int model, SDL_Window *window, SDL_Renderer *renderer) {
|
|
|
|
struct ewm_one_t *one = (struct ewm_one_t*) malloc(sizeof(struct ewm_one_t));
|
|
|
|
if (ewm_one_init(one, model, window, renderer) != 0) {
|
|
|
|
free(one);
|
|
|
|
one = NULL;
|
|
|
|
}
|
|
|
|
return one;
|
|
|
|
}
|
|
|
|
|
2016-12-19 01:18:45 +00:00
|
|
|
void ewm_one_destroy(struct ewm_one_t *one) {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ewm_one_keydown(struct ewm_one_t *one, uint8_t key) {
|
|
|
|
ewm_pia_set_ina(one->pia, key | 0x80);
|
|
|
|
ewm_pia_set_irqa1(one->pia);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ewm_one_poll_event(struct ewm_one_t *one, SDL_Window *window) {
|
|
|
|
SDL_Event event;
|
|
|
|
while (SDL_PollEvent(&event) != 0) {
|
|
|
|
switch (event.type) {
|
|
|
|
case SDL_QUIT:
|
|
|
|
return false;
|
2016-12-19 03:19:44 +00:00
|
|
|
case SDL_WINDOWEVENT:
|
|
|
|
one->tty->screen_dirty = true;
|
|
|
|
break;
|
2016-12-19 01:18:45 +00:00
|
|
|
case SDL_KEYDOWN:
|
|
|
|
if (event.key.keysym.mod & KMOD_CTRL) {
|
|
|
|
if (event.key.keysym.sym >= SDLK_a && event.key.keysym.sym <= SDLK_z) {
|
|
|
|
ewm_one_keydown(one, event.key.keysym.sym - SDLK_a);
|
|
|
|
} else {
|
|
|
|
// TODO Implement control codes 1b - 1f
|
|
|
|
}
|
|
|
|
} else if (event.key.keysym.mod & KMOD_GUI) {
|
|
|
|
switch (event.key.keysym.sym) {
|
|
|
|
case SDLK_ESCAPE:
|
|
|
|
cpu_reset(one->cpu);
|
|
|
|
break;
|
|
|
|
case SDLK_RETURN:
|
|
|
|
if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) {
|
|
|
|
SDL_SetWindowFullscreen(window, 0);
|
|
|
|
} else {
|
|
|
|
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (event.key.keysym.mod == KMOD_NONE) {
|
|
|
|
switch (event.key.keysym.sym) {
|
|
|
|
case SDLK_RETURN:
|
|
|
|
ewm_one_keydown(one, 0x0d); // CR
|
|
|
|
break;
|
|
|
|
case SDLK_TAB:
|
|
|
|
ewm_one_keydown(one, 0x09); // HT
|
|
|
|
case SDLK_DELETE:
|
|
|
|
ewm_one_keydown(one, 0x7f); // DEL
|
|
|
|
break;
|
|
|
|
case SDLK_LEFT:
|
|
|
|
ewm_one_keydown(one, 0x08); // BS
|
|
|
|
break;
|
|
|
|
case SDLK_RIGHT:
|
|
|
|
ewm_one_keydown(one, 0x15); // NAK
|
|
|
|
break;
|
|
|
|
case SDLK_UP:
|
|
|
|
ewm_one_keydown(one, 0x0b); // VT
|
|
|
|
break;
|
|
|
|
case SDLK_DOWN:
|
|
|
|
ewm_one_keydown(one, 0x0a); // LF
|
|
|
|
break;
|
|
|
|
case SDLK_ESCAPE:
|
|
|
|
ewm_one_keydown(one, 0x1b); // ESC
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SDL_TEXTINPUT:
|
|
|
|
if (strlen(event.text.text) == 1) {
|
|
|
|
ewm_one_keydown(one, toupper(event.text.text[0]));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ewm_one_step_cpu(struct ewm_one_t *one, int cycles) {
|
|
|
|
while (true) {
|
|
|
|
int ret = cpu_step(one->cpu);
|
|
|
|
if (ret < 0) {
|
2016-12-29 04:13:05 +00:00
|
|
|
// These only happen in strict mode
|
2016-12-19 01:18:45 +00:00
|
|
|
switch (ret) {
|
|
|
|
case EWM_CPU_ERR_UNIMPLEMENTED_INSTRUCTION:
|
|
|
|
fprintf(stderr, "CPU: Exited because of unimplemented instructions 0x%.2x at 0x%.4x\n",
|
|
|
|
mem_get_byte(one->cpu, one->cpu->state.pc), one->cpu->state.pc);
|
|
|
|
break;
|
|
|
|
case EWM_CPU_ERR_STACK_OVERFLOW:
|
|
|
|
fprintf(stderr, "CPU: Exited because of stack overflow at 0x%.4x\n", one->cpu->state.pc);
|
|
|
|
break;
|
|
|
|
case EWM_CPU_ERR_STACK_UNDERFLOW:
|
|
|
|
fprintf(stderr, "CPU: Exited because of stack underflow at 0x%.4x\n", one->cpu->state.pc);
|
|
|
|
break;
|
|
|
|
}
|
2016-12-29 04:13:05 +00:00
|
|
|
return false;
|
2016-12-19 01:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cycles -= ret;
|
|
|
|
if (cycles <= 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-14 13:46:57 +00:00
|
|
|
#define EWM_ONE_OPT_HELP (0)
|
|
|
|
#define EWM_ONE_OPT_MODEL (1)
|
|
|
|
#define EWM_ONE_OPT_MEMORY (2)
|
|
|
|
#define EWM_ONE_OPT_TRACE (3)
|
|
|
|
#define EWM_ONE_OPT_STRICT (4)
|
2016-12-19 01:46:51 +00:00
|
|
|
|
|
|
|
static struct option one_options[] = {
|
2017-03-14 13:46:57 +00:00
|
|
|
{ "help", no_argument, NULL, EWM_ONE_OPT_HELP },
|
2016-12-28 21:40:16 +00:00
|
|
|
{ "model", required_argument, NULL, EWM_ONE_OPT_MODEL },
|
2016-12-26 16:03:47 +00:00
|
|
|
{ "memory", required_argument, NULL, EWM_ONE_OPT_MEMORY },
|
2016-12-28 21:40:16 +00:00
|
|
|
{ "trace", optional_argument, NULL, EWM_ONE_OPT_TRACE },
|
2016-12-29 04:13:05 +00:00
|
|
|
{ "strict", no_argument, NULL, EWM_ONE_OPT_STRICT },
|
2016-12-26 16:03:47 +00:00
|
|
|
{ NULL, 0, NULL, 0 }
|
2016-12-19 01:46:51 +00:00
|
|
|
};
|
|
|
|
|
2017-03-14 13:46:57 +00:00
|
|
|
static void usage() {
|
|
|
|
fprintf(stderr, "Usage: ewm one [options]\n");
|
|
|
|
fprintf(stderr, " --model <model> model to emulate (default: apple1)\n");
|
|
|
|
fprintf(stderr, " --memory <region> add memory region (ram|rom:address:path)\n");
|
|
|
|
fprintf(stderr, " --trace <file> trace cpu to file\n");
|
|
|
|
fprintf(stderr, " --strict run emulator in strict mode\n");
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
fprintf(stderr, "Supported models:\n");
|
|
|
|
fprintf(stderr, " apple1 Classic Apple 1, 6502, 8KB RAM, Woz Monitor\n");
|
|
|
|
fprintf(stderr, " replica1 Replica 1, 65C02, 48KB RAM, KRUSADER\n");
|
|
|
|
}
|
|
|
|
|
2016-12-19 01:18:45 +00:00
|
|
|
int ewm_one_main(int argc, char **argv) {
|
|
|
|
// Parse Apple 1 specific options
|
2016-12-19 01:46:51 +00:00
|
|
|
int model = EWM_ONE_MODEL_DEFAULT;
|
2016-12-26 16:03:47 +00:00
|
|
|
struct ewm_memory_option_t *extra_memory = NULL;
|
2016-12-28 21:40:16 +00:00
|
|
|
char *trace_path = NULL;
|
2016-12-29 04:13:05 +00:00
|
|
|
bool strict = false;
|
2016-12-19 01:46:51 +00:00
|
|
|
|
2016-12-26 22:03:12 +00:00
|
|
|
int ch;
|
2016-12-19 01:46:51 +00:00
|
|
|
while ((ch = getopt_long_only(argc, argv, "", one_options, NULL)) != -1) {
|
|
|
|
switch (ch) {
|
2017-03-14 13:46:57 +00:00
|
|
|
case EWM_ONE_OPT_HELP: {
|
|
|
|
usage();
|
|
|
|
exit(0);
|
|
|
|
}
|
2016-12-26 16:03:47 +00:00
|
|
|
case EWM_ONE_OPT_MODEL: {
|
2016-12-19 01:46:51 +00:00
|
|
|
if (strcmp(optarg, "apple1") == 0) {
|
|
|
|
model = EWM_ONE_MODEL_APPLE1;
|
|
|
|
} else if (strcmp(optarg, "replica1") == 0) {
|
|
|
|
model = EWM_ONE_MODEL_REPLICA1;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Unknown --model specified\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
2016-12-26 16:03:47 +00:00
|
|
|
}
|
|
|
|
case EWM_ONE_OPT_MEMORY: {
|
|
|
|
struct ewm_memory_option_t *m = parse_memory_option(optarg);
|
|
|
|
if (m == NULL) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
m->next = extra_memory;
|
|
|
|
extra_memory = m;
|
|
|
|
break;
|
|
|
|
}
|
2016-12-28 21:40:16 +00:00
|
|
|
case EWM_ONE_OPT_TRACE: {
|
|
|
|
trace_path = optarg ? optarg : "/dev/stderr";
|
|
|
|
break;
|
|
|
|
}
|
2016-12-29 04:13:05 +00:00
|
|
|
case EWM_ONE_OPT_STRICT: {
|
|
|
|
strict = true;
|
|
|
|
break;
|
|
|
|
}
|
2017-03-14 13:46:57 +00:00
|
|
|
default: {
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
2016-12-19 01:46:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-19 01:18:45 +00:00
|
|
|
// Setup SDL
|
|
|
|
|
|
|
|
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTS) < 0) {
|
|
|
|
fprintf(stderr, "Failed to initialize SDL: %s\n", SDL_GetError());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Window *window = SDL_CreateWindow("EWM v0.1 / Apple 1", 400, 60, 280*3, 192*3, SDL_WINDOW_SHOWN);
|
|
|
|
if (window == NULL) {
|
|
|
|
fprintf(stderr, "Failed create window: %s\n", SDL_GetError());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-01-07 13:52:56 +00:00
|
|
|
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
|
2016-12-19 01:18:45 +00:00
|
|
|
if (renderer == NULL) {
|
|
|
|
fprintf(stderr, "Failed to create renderer: %s\n", SDL_GetError());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-01-07 13:52:56 +00:00
|
|
|
SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
|
|
|
|
|
2016-12-19 01:18:45 +00:00
|
|
|
SDL_RenderSetLogicalSize(renderer, 280*3, 192*3);
|
|
|
|
|
|
|
|
// Create the machine
|
|
|
|
|
2017-01-07 13:52:56 +00:00
|
|
|
struct ewm_one_t *one = ewm_one_create(model, window, renderer);
|
2016-12-19 01:18:45 +00:00
|
|
|
if (one == NULL) {
|
|
|
|
fprintf(stderr, "Failed to create ewm_one_t\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-12-26 16:03:47 +00:00
|
|
|
// Add extra memory, if any
|
|
|
|
|
|
|
|
if (extra_memory != NULL) {
|
|
|
|
if (cpu_add_memory_from_options(one->cpu, extra_memory) != 0) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-29 04:13:05 +00:00
|
|
|
cpu_strict(one->cpu, strict);
|
2016-12-28 21:40:16 +00:00
|
|
|
cpu_trace(one->cpu, trace_path);
|
|
|
|
|
2016-12-19 01:18:45 +00:00
|
|
|
cpu_reset(one->cpu);
|
|
|
|
|
|
|
|
// Main loop
|
|
|
|
|
|
|
|
SDL_StartTextInput();
|
|
|
|
|
2017-01-09 03:18:38 +00:00
|
|
|
uint32_t ticks = SDL_GetTicks();
|
|
|
|
uint32_t phase = 1;
|
2016-12-19 01:18:45 +00:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (!ewm_one_poll_event(one, window)) { // TODO Move window into one
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is very basic throttling that does bursts of CPU cycles.
|
|
|
|
|
2017-01-09 03:18:38 +00:00
|
|
|
if ((SDL_GetTicks() - ticks) >= (1000 / EWM_ONE_FPS)) {
|
|
|
|
if (!ewm_one_step_cpu(one, EWM_ONE_CPS / EWM_ONE_FPS)) {
|
2016-12-19 01:18:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-01-09 03:18:38 +00:00
|
|
|
if (one->tty->screen_dirty || (phase == 0) || ((phase % (EWM_ONE_FPS / 4)) == 0)) {
|
2016-12-19 01:18:45 +00:00
|
|
|
SDL_SetRenderDrawColor(one->tty->renderer, 0, 0, 0, 255);
|
|
|
|
SDL_RenderClear(one->tty->renderer);
|
2017-01-09 03:18:38 +00:00
|
|
|
|
|
|
|
ewm_tty_refresh(one->tty, phase, EWM_ONE_FPS);
|
2016-12-19 01:18:45 +00:00
|
|
|
one->tty->screen_dirty = false;
|
2017-01-09 03:18:38 +00:00
|
|
|
|
2016-12-19 01:18:45 +00:00
|
|
|
SDL_RenderPresent(one->tty->renderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
ticks = SDL_GetTicks();
|
2017-01-09 03:18:38 +00:00
|
|
|
|
|
|
|
phase += 1;
|
|
|
|
if (phase == EWM_ONE_FPS) {
|
|
|
|
phase = 0;
|
|
|
|
}
|
2016-12-19 01:18:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy SDL
|
|
|
|
|
|
|
|
SDL_DestroyWindow(window);
|
|
|
|
SDL_DestroyRenderer(renderer);
|
|
|
|
SDL_Quit();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|