Code Golf Part 2 - Basic

This commit is contained in:
Stefan Arentz 2020-02-19 09:15:59 -05:00
parent c642313461
commit cce593d010
3 changed files with 79 additions and 0 deletions

View File

@ -85,6 +85,11 @@ CODEGOLF_TEST_SOURCES=$(CPU_SOURCES) codegolf_test.c
CODEGOLF_TEST_OBJECTS=$(CODEGOLF_TEST_SOURCES:.c=.o)
CODEGOLD_TEST_LIBS=$(LUA_LIBS)
CODEGOLF_BASIC=codegolf_basic
CODEGOLF_BASIC_SOURCES=$(CPU_SOURCES) codegolf_basic.c
CODEGOLF_BASIC_OBJECTS=$(CODEGOLF_BASIC_SOURCES:.c=.o)
CODEGOLD_BASIC_LIBS=$(LUA_LIBS)
all: $(EWM_SOURCES) $(EWM_EXECUTABLE) $(CPU_TEST_SOURCES) $(CPU_TEST_EXECUTABLE) $(SCR_TEST_EXECUTABLE) $(TTY_TEST_EXECUTABLE) $(CPU_BENCH) $(MEM_BENCH)
@ -112,5 +117,8 @@ $(MEM_BENCH): $(MEM_BENCH_OBJECTS)
$(CODEGOLF_TEST): $(CODEGOLF_TEST_OBJECTS)
$(CC) $(LDFLAGS) $(CODEGOLF_TEST_OBJECTS) $(CODEGOLF_TEST_LIBS) -o $@
$(CODEGOLF_BASIC): $(CODEGOLF_BASIC_OBJECTS)
$(CC) $(LDFLAGS) $(CODEGOLF_BASIC_OBJECTS) $(CODEGOLF_BASIC_LIBS) -o $@
.c.o:
$(CC) $(CFLAGS) $< -c -o $@

71
src/codegolf_basic.c Normal file
View File

@ -0,0 +1,71 @@
// The MIT License (MIT)
//
// Copyright (c) 2020 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 <stdint.h>
#include <stdio.h>
#include "fmt.h"
#include "cpu.h"
#include "mem.h"
//
// This implements the "Doing something more interactive with it" part of the code golf at
// https://codegolf.stackexchange.com/questions/12844/emulate-a-mos-6502-cpu
//
uint8_t input_handler(struct cpu_t *cpu, struct mem_t *mem, uint16_t addr) {
return 0;
}
void output_handler(struct cpu_t *cpu, struct mem_t *mem, uint16_t addr, uint8_t b) {
printf("Hello\n");
if (addr == 0xf001) {
printf("Got: %c\n", b);
}
}
int main() {
struct cpu_t *cpu = cpu_create(EWM_CPU_MODEL_6502);
cpu_add_ram(cpu, 0x0000, 0xbfff);
cpu_add_ram_file(cpu, 0xc000, "rom/ehbasic.bin");
cpu_add_iom(cpu, 0xf000, 0xf0ff, NULL, input_handler, output_handler);
cpu_reset(cpu);
cpu->state.pc = 0xc000;
while (true) {
char state[1024];
cpu_format_state(cpu, state);
char ins[1024];
cpu_format_instruction(cpu, ins);
printf("%.4X: %s %s\n", cpu->state.pc, state, ins);
int ret = cpu_step(cpu);
if (ret < 0) {
fprintf(stderr, "Unexpected error %d\n", ret);
return 1;
}
}
return 0;
}

BIN
src/rom/ehbasic.bin Normal file

Binary file not shown.