From 776c16cc205cb146bb3dff0589ef4389b6ae4dac Mon Sep 17 00:00:00 2001 From: neauoire Date: Fri, 29 Jan 2021 12:14:37 -0800 Subject: [PATCH] Basic parts --- .gitignore | 4 ++- README.md | 8 ++++-- build.sh | 13 ++++----- program.usm | 1 + uxn.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++----- uxnasm.c | 49 +++++++++++++++++++++++++++++++++ 6 files changed, 136 insertions(+), 18 deletions(-) create mode 100644 program.usm create mode 100644 uxnasm.c diff --git a/.gitignore b/.gitignore index 28b1921..ed1e496 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ *png~ *gif~ *bmp~ -uxn \ No newline at end of file +uxn +uxnasm +*.rom \ No newline at end of file diff --git a/README.md b/README.md index 5eb2276..fe58e50 100644 --- a/README.md +++ b/README.md @@ -48,11 +48,15 @@ VALUE OPCODE EXPLANATION ### PPU +### Assembly + +``` +2 2 + $ef +``` + ### Assembler ### Emulator - SDL Layer - -### Assembly diff --git a/build.sh b/build.sh index 3ca187c..edc664c 100755 --- a/build.sh +++ b/build.sh @@ -1,12 +1,15 @@ #!/bin/bash # format code +clang-format -i uxnasm.c clang-format -i uxn.c # remove old +rm -f ./uxnasm rm -f ./uxn # debug(slow) +cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined uxnasm.c -o uxnasm cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined uxn.c -o uxn # build(fast) @@ -15,12 +18,6 @@ cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werr # Size echo "Size: $(du -sk ./uxn)" -# Install -if [ -d "$HOME/bin" ] && [ -e ./uxn ] -then - cp ./uxn $HOME/bin - echo "Installed: $HOME/bin" -fi - # run -./uxn +./uxnasm program.usm program.rom +# ./uxn program.rom diff --git a/program.usm b/program.usm new file mode 100644 index 0000000..0ad5dda --- /dev/null +++ b/program.usm @@ -0,0 +1 @@ +2 2 + . \ No newline at end of file diff --git a/uxn.c b/uxn.c index dd11941..b788bb3 100644 --- a/uxn.c +++ b/uxn.c @@ -11,28 +11,93 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE. */ -typedef unsigned char Uint8; #define STACK_DEPTH 256 +#define ECHO 1 -Uint8 data[STACK_DEPTH]; +typedef unsigned char Uint8; + +typedef struct { + +} Computer; + +Uint8 sptr; +Uint8 stack[STACK_DEPTH]; Uint8 address[STACK_DEPTH]; +Uint8 memory[STACK_DEPTH]; void -stackprint(Uint8 *stack) +stackprint(Uint8 *s, Uint8 len) { int i; - for(i = 0; i < STACK_DEPTH; ++i) { + for(i = 0; i < len; ++i) { if(i % 16 == 0) printf("\n"); - printf("%02x ", stack[i]); + if(sptr == i) + printf("[%02x]", s[i]); + else + printf(" %02x ", s[i]); } printf("\n"); } +void +op_push(Uint8 *s, Uint8 v) +{ + s[sptr++] = v; +} + +void +op_pop(Uint8 *s) +{ + s[sptr--] = 0x00; +} + +void +reset(Computer *cpu) +{ +} + +int +disk(Computer *cpu, FILE *f) +{ + int i; + unsigned short buffer[256]; + reset(cpu); + if(!fread(buffer, sizeof(buffer), 1, f)) + return 0; + /* + + for(i = 0; i < 128; i++) { + cpu->memory[i * 2] |= (buffer[i] >> 8) & 0xFF; + cpu->memory[i * 2 + 1] |= buffer[i] & 0xFF; + } + */ + return 1; +} + +void +run(Computer *cpu, int debug) +{ +} + int main(int argc, char *argv[]) { - printf("hello\n"); - stackprint(data); + FILE *f; + Computer cpu; + if(argc < 2) + return error("No input."); + if(!(f = fopen(argv[1], "rb"))) + return error("Missing input."); + if(!disk(&cpu, f)) + return error("Unreadable input."); + run(&cpu, ECHO); + /* program */ + op_push(stack, 0xef); + op_pop(stack); + op_push(stack, 0x02); + op_push(stack, 0x03); + /* print result */ + stackprint(stack, 0x40); return 0; } diff --git a/uxnasm.c b/uxnasm.c new file mode 100644 index 0000000..c0ca6ff --- /dev/null +++ b/uxnasm.c @@ -0,0 +1,49 @@ +#include + +/* +Copyright (c) 2021 Devine Lu Linvega + +Permission to use, copy, modify, and distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE. +*/ + +#define BUFLEN 256 + +typedef unsigned char Uint8; +typedef unsigned char Uint16; + +unsigned short program[BUFLEN]; + +void +pass1(FILE *f) +{ + int instrid = 0; + char line[BUFLEN]; + while(fgets(line, BUFLEN, f)) { + printf("%s\n", line); + } +} + +int +error(char *name) +{ + printf("Error: %s\n", name); + return 0; +} + +int +main(int argc, char *argv[]) +{ + FILE *f; + if(argc < 3) + return error("No input."); + if(!(f = fopen(argv[1], "r"))) + return error("Missing input."); + pass1(f); + fwrite(program, sizeof(program), 1, fopen(argv[2], "wb")); + return 0; +}