Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Golf #186

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ MEM_BENCH_SOURCES=$(CPU_SOURCES) mem_bench.c
MEM_BENCH_OBJECTS=$(MEM_BENCH_SOURCES:.c=.o)
MEM_BENCH_LIBS=$(LUA_LIBS)

CODEGOLF_TEST=codegolf_test
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)

clean:
Expand All @@ -103,5 +114,11 @@ $(CPU_BENCH): $(CPU_BENCH_OBJECTS)
$(MEM_BENCH): $(MEM_BENCH_OBJECTS)
$(CC) $(LDFLAGS) $(MEM_BENCH_OBJECTS) $(MEM_BENCH_LIBS) -o $@

$(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 changes: 71 additions & 0 deletions src/codegolf_basic.c
Original file line number Diff line number Diff line change
@@ -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;
}
54 changes: 54 additions & 0 deletions src/codegolf_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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 <stdio.h>

#include "cpu.h"
#include "mem.h"

//
// This implements the "Testing your emulator" part of the code golf at
// https://codegolf.stackexchange.com/questions/12844/emulate-a-mos-6502-cpu
//

int main() {
struct cpu_t *cpu = cpu_create(EWM_CPU_MODEL_6502);
cpu_add_ram(cpu, 0x0000, 0x3FFF);
cpu_add_ram_file(cpu, 0x4000, "rom/AllSuiteA.bin");
cpu_reset(cpu);
cpu->state.pc = 0x4000;

while (true) {
int ret = cpu_step(cpu);
if (ret < 0) {
fprintf(stderr, "Unexpected error %d\n", ret);
return 1;
}

if (cpu->state.pc == 0x45c0) {
fprintf(stderr, "Test finished with status 0x%.2x\n", mem_get_byte(cpu, 0x0210));
break;
}
}

return 0;
}
Loading