Basic cli handling

This commit is contained in:
d0p1 🏳️‍⚧️ 2024-02-29 13:01:58 +01:00
parent d53f1cbfe0
commit 262a211da9
14 changed files with 514 additions and 292 deletions

View file

@ -1,4 +1,7 @@
AC_INIT([65oo2], [1.0]) AC_INIT([65∞2], [1.0],
[https://git.cute.engineering/d0p1/65oo2/issues],
[65oo2],
[https://git.cute.engineering/d0p1/65oo2])
AC_CANONICAL_HOST AC_CANONICAL_HOST
@ -25,6 +28,9 @@ AM_PROG_CC_C_O
PKG_PROG_PKG_CONFIG PKG_PROG_PKG_CONFIG
AC_CHECK_TOOLS(WINDRES, [windres], :) AC_CHECK_TOOLS(WINDRES, [windres], :)
PKG_CHECK_MODULES([SDL2], [sdl2])
SDL2_LIBS=${SDL2_LIBS/-mwindows/}
AC_ARG_ENABLE([ata-vga], AC_ARG_ENABLE([ata-vga],
[AS_HELP_STRING([--enable-ata-vga], [enable VGA (default=yes)])], [AS_HELP_STRING([--enable-ata-vga], [enable VGA (default=yes)])],
[WANT_ATA_VGA=$enableval], [WANT_ATA_VGA=yes]) [WANT_ATA_VGA=$enableval], [WANT_ATA_VGA=yes])
@ -40,13 +46,12 @@ AC_ARG_ENABLE(jit,
[WANT_JIT=$enableval], [WANT_JIT=no]) [WANT_JIT=$enableval], [WANT_JIT=no])
AC_CHECK_INCLUDES_DEFAULT AC_CHECK_INCLUDES_DEFAULT
AC_CHECK_HEADERS([libgen.h])
AC_C_CONST AC_C_CONST
AM_CONDITIONAL([USE_ATA_VGA], test "$WANT_ATA_VGA" = "yes") AM_CONDITIONAL([USE_ATA_VGA], test "$WANT_ATA_VGA" = "yes")
if test "$WANT_ATA_VGA" = "yes"; then if test "$WANT_ATA_VGA" = "yes"; then
AC_DEFINE([ATA_VGA], [1], [Enable VGA device]) AC_DEFINE([ATA_VGA], [1], [Enable VGA device])
PKG_CHECK_MODULES([SDL2], [sdl2])
fi fi
AM_CONDITIONAL([USE_ATA_ETHERNET], test "$WANT_ATA_ETHERNET" = "yes") AM_CONDITIONAL([USE_ATA_ETHERNET], test "$WANT_ATA_ETHERNET" = "yes")

View file

@ -1,7 +1,11 @@
#include "config.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdarg.h> #include <stdarg.h>
#ifdef HAVE_LIBGEN_H
# include <libgen.h>
#endif /* HAVE_LIBGEN_H */
#include "lib/op.h" #include "lib/op.h"
#define OP_ADDR_MODE (OP_ADDR_IMPL | OP_ADDR_REL | OP_ADDR_IND \ #define OP_ADDR_MODE (OP_ADDR_IMPL | OP_ADDR_REL | OP_ADDR_IND \
@ -227,7 +231,14 @@ decode(void)
switch (opcode_addr[opcode] & OP_ADDR_MODE) switch (opcode_addr[opcode] & OP_ADDR_MODE)
{ {
case OP_ADDR_IMPL: case OP_ADDR_IMPL:
if (opcode_addr[opcode] & OP_ADDR_A)
{
fprintf(output, "%s A\n", opcode_str[opcode]);
}
else
{
fprintf(output, "%s\n", opcode_str[opcode]); fprintf(output, "%s\n", opcode_str[opcode]);
}
break; break;
case OP_ADDR_REL: case OP_ADDR_REL:
relative = read16(); relative = read16();
@ -249,19 +260,70 @@ decode(void)
} while (!feof(input)); } while (!feof(input));
} }
void
usage(int retcode)
{
if (retcode == EXIT_FAILURE)
{
fprintf(stderr,
"Try '%s -h' for more information.\n", prg_name);
}
else
{
printf("Usage: %s [-hV] [INPUT] [OUTPUT]\n", prg_name);
printf("Disassemble INPUT to OUTPUT\n\n");
printf("With no INPUT read standard input.\n");
printf("With no OUTPUT write standard output.\n\n");
printf("\t-h\tdisplay this help and exit\n");
printf("\t-V\toutput version information\n");
printf("\nReport bugs to <%s>\n", PACKAGE_BUGREPORT);
}
exit(retcode);
}
void
version(void)
{
printf("%s version %s\n", PACKAGE_NAME, PACKAGE_VERSION);
exit(EXIT_SUCCESS);
}
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
#ifdef HAVE_LIBGEN_H
prg_name = basename(argv[0]);
#else
prg_name = argv[0]; prg_name = argv[0];
#endif /* HAVE_LIBGEN_H */
input = stdin; input = stdin;
output = stdout; output = stdout;
while ((argc > 1) && (argv[1][0] == '-'))
{
switch (argv[1][1])
{
case 'h':
usage(EXIT_SUCCESS);
break;
case 'V':
version();
break;
default:
usage(EXIT_FAILURE);
break;
}
argv++;
argc--;
}
if (argc > 2) if (argc > 2)
{ {
output = fopen(argv[2], "w"); output = fopen(argv[2], "w");
if (output == NULL) fatal("can't open %s.", argv[2]); if (output == NULL) fatal("can't open %s.", argv[2]);
} }
if (argc > 1) if (argc > 1)
{ {
input = fopen(argv[1], "rb"); input = fopen(argv[1], "rb");
@ -269,6 +331,5 @@ main(int argc, char **argv)
} }
decode(); decode();
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }

View file

@ -12,7 +12,7 @@ const uint8_t opcode_addr[OP_MAX] = {
/* */ 0xFF, /* */ 0xFF,
/* PHP */ OP_ADDR_IMPL, /* PHP */ OP_ADDR_IMPL,
/* ORA */ OP_ADDR_IMM, /* ORA */ OP_ADDR_IMM,
/* ASL */ OP_ADDR_IMPL, /* ASL */ OP_ADDR_IMPL | OP_ADDR_A,
/* */ 0xFF, /* */ 0xFF,
/* */ 0xFF, /* */ 0xFF,
/* ORA */ OP_ADDR_ABS, /* ORA */ OP_ADDR_ABS,
@ -48,7 +48,7 @@ const uint8_t opcode_addr[OP_MAX] = {
/* */ 0xFF, /* */ 0xFF,
/* PLP */ OP_ADDR_IMPL, /* PLP */ OP_ADDR_IMPL,
/* AND */ OP_ADDR_IMM, /* AND */ OP_ADDR_IMM,
/* ROL */ OP_ADDR_IMPL, /* ROL */ OP_ADDR_IMPL | OP_ADDR_A,
/* */ 0xFF, /* */ 0xFF,
/* BIT */ OP_ADDR_ABS, /* BIT */ OP_ADDR_ABS,
/* AND */ OP_ADDR_ABS, /* AND */ OP_ADDR_ABS,
@ -84,7 +84,7 @@ const uint8_t opcode_addr[OP_MAX] = {
/* */ 0xFF, /* */ 0xFF,
/* PHA */ OP_ADDR_IMPL, /* PHA */ OP_ADDR_IMPL,
/* EOR */ OP_ADDR_IMM, /* EOR */ OP_ADDR_IMM,
/* LSR */ OP_ADDR_IMPL, /* LSR */ OP_ADDR_IMPL | OP_ADDR_A,
/* */ 0xFF, /* */ 0xFF,
/* JMP */ OP_ADDR_ABS, /* JMP */ OP_ADDR_ABS,
/* EOR */ OP_ADDR_ABS, /* EOR */ OP_ADDR_ABS,
@ -112,7 +112,7 @@ const uint8_t opcode_addr[OP_MAX] = {
/* 6- */ /* 6- */
/* RTS */ OP_ADDR_IMPL, /* RTS */ OP_ADDR_IMPL,
/* ADC */ OP_ADDR_IND | OP_ADDR_X, /* ADC */ OP_ADDR_IND | OP_ADDR_X,
/* */ 0xFF, /* PER */ OP_ADDR_IMPL,
/* */ 0xFF, /* */ 0xFF,
/* */ 0xFF, /* */ 0xFF,
/* ADC */ 0xFF, /* ADC */ 0xFF,
@ -120,7 +120,7 @@ const uint8_t opcode_addr[OP_MAX] = {
/* */ 0xFF, /* */ 0xFF,
/* PLA */ OP_ADDR_IMPL, /* PLA */ OP_ADDR_IMPL,
/* ADC */ OP_ADDR_IMM, /* ADC */ OP_ADDR_IMM,
/* ROR */ OP_ADDR_IMPL, /* ROR */ OP_ADDR_IMPL | OP_ADDR_A,
/* */ 0xFF, /* */ 0xFF,
/* JMP */ OP_ADDR_IND, /* JMP */ OP_ADDR_IND,
/* ADC */ OP_ADDR_ABS, /* ADC */ OP_ADDR_ABS,
@ -291,13 +291,13 @@ const uint8_t opcode_addr[OP_MAX] = {
}; };
const char *opcode_str[OP_MAX] = { const char *opcode_str[OP_MAX] = {
"BRK", "ORA", NULL, NULL, NULL, "ORA", "ASL", NULL, "PHP", "ORA", "ASLA", NULL, NULL, "ORA", "ASL", NULL, "BRK", "ORA", NULL, NULL, NULL, "ORA", "ASL", NULL, "PHP", "ORA", "ASL", NULL, NULL, "ORA", "ASL", NULL,
"BPL", "ORA", NULL, NULL, NULL, "ORA", "ASL", NULL, "CLC", "ORA", NULL, NULL, NULL, "ORA", "ASL", NULL, "BPL", "ORA", NULL, NULL, NULL, "ORA", "ASL", NULL, "CLC", "ORA", NULL, NULL, NULL, "ORA", "ASL", NULL,
"JSR", "AND", NULL, NULL, "BIT", "AND", "ROL", NULL, "PLP", "AND", "ROLA", NULL, "BIT", "AND", "ROL", NULL, "JSR", "AND", NULL, NULL, "BIT", "AND", "ROL", NULL, "PLP", "AND", "ROL", NULL, "BIT", "AND", "ROL", NULL,
"BMI", "AND", NULL, NULL, NULL, "AND", "ROL", NULL, "SEC", "AND", NULL, NULL, NULL, "AND", "ROL", NULL, "BMI", "AND", NULL, NULL, NULL, "AND", "ROL", NULL, "SEC", "AND", NULL, NULL, NULL, "AND", "ROL", NULL,
"RTI", "EOR", NULL, NULL, NULL, "EOR", "LSR", NULL, "PHA", "EOR", "LSRA", NULL, "JMP", "EOR", "LSR", NULL, "RTI", "EOR", NULL, NULL, NULL, "EOR", "LSR", NULL, "PHA", "EOR", "LSR", NULL, "JMP", "EOR", "LSR", NULL,
"BVC", "EOR", NULL, NULL, NULL, "EOR", "LSR", NULL, "CLI", "EOR", NULL, NULL, NULL, "EOR", "LSR", NULL, "BVC", "EOR", NULL, NULL, NULL, "EOR", "LSR", NULL, "CLI", "EOR", NULL, NULL, NULL, "EOR", "LSR", NULL,
"RTS", "ADC", NULL, NULL, NULL, "ADC", "ROR", NULL, "PLA", "ADC", "RORA", NULL, "JMP", "ADC", "ROR", NULL, "RTS", "ADC", "PER", NULL, NULL, "ADC", "ROR", NULL, "PLA", "ADC", "ROR", NULL, "JMP", "ADC", "ROR", NULL,
"BVS", "ADC", NULL, NULL, NULL, "ADC", "ROR", NULL, "SEI", "ADC", NULL, NULL, NULL, "ADC", "ROR", NULL, "BVS", "ADC", NULL, NULL, NULL, "ADC", "ROR", NULL, "SEI", "ADC", NULL, NULL, NULL, "ADC", "ROR", NULL,
NULL, "STA", NULL, NULL, "STY", "STA", "STX", NULL, "DEY", NULL, "TXA", NULL, "STY", "STA", "STX", NULL, NULL, "STA", NULL, NULL, "STY", "STA", "STX", NULL, "DEY", NULL, "TXA", NULL, "STY", "STA", "STX", NULL,
"BCC", "STA", NULL, NULL, "STY", "STA", "STX", NULL, "TYA", "STA", "TXS", NULL, NULL, "STA", NULL, NULL, "BCC", "STA", NULL, NULL, "STY", "STA", "STX", NULL, "TYA", "STA", "TXS", NULL, NULL, "STA", NULL, NULL,

View file

@ -12,6 +12,7 @@
# define OP_ADDR_ABS 1 << 3 # define OP_ADDR_ABS 1 << 3
# define OP_ADDR_X 1 << 4 # define OP_ADDR_X 1 << 4
# define OP_ADDR_Y 1 << 5 # define OP_ADDR_Y 1 << 5
# define OP_ADDR_A 1 << 6
# define OP_BRK_impl 0x00 # define OP_BRK_impl 0x00
# define OP_ORA_x_ind 0x01 # define OP_ORA_x_ind 0x01

View file

@ -7,5 +7,5 @@ noinst_LIBRARIES = $(LIBJIT) $(LIB_ATA)
bin_PROGRAMS = vm bin_PROGRAMS = vm
vm_SOURCES = main.c vm_SOURCES = main.c
vm_CFLAGS =-I$(top_srcdir) vm_CFLAGS =-I$(top_srcdir) $(SDL2_CFLAGS)
vm_LDADD = ../lib/lib65oo2.a $(LIBJIT) $(LIB_ATA) $(RES) vm_LDADD = ../lib/lib65oo2.a $(LIBJIT) $(LIB_ATA) $(RES) $(SDL2_LIBS)

View file

@ -5,3 +5,7 @@ libata_a_SOURCES = ata/ata.c ata/rtc.c
if USE_ATA_VGA if USE_ATA_VGA
libata_a_SOURCES += ata/vga.c libata_a_SOURCES += ata/vga.c
endif endif
if USE_ATA_ETHERNET
libata_a_SOURCES += ata/ethernet.c
endif

View file

@ -1,4 +1,4 @@
#include "isa.h" #include "ata.h"
static const uint8_t mac_addr[6] = { static const uint8_t mac_addr[6] = {
0x53, 0x54, 0x41, 0x4C, 0x49, 0x4E 0x53, 0x54, 0x41, 0x4C, 0x49, 0x4E

View file

@ -7,7 +7,7 @@
#define REG_WKDAY 0x06 #define REG_WKDAY 0x06
#define REG_DAY 0x07 #define REG_DAY 0x07
#define REG_YEAR 0x09 #define REG_YEAR 0x09
#define REG_CENTURY 0x032 #define REG_CENTURY 0x32
#define REG_STATUSA 0x0A #define REG_STATUSA 0x0A
#define REG_STATUSB 0x0B #define REG_STATUSB 0x0B

16
vm/bus.h Normal file
View file

@ -0,0 +1,16 @@
#ifndef VM_BUS_H
# define VM_BUS_H 1
# include <stddef.h>
# include <stdint.h>
typedef int (*BusRead)(uint32_t addr, void *data, size_t sz);
typedef int (*BusWrite)(uint32_t addr, const void *data, size_t sz);
typedef struct
{
BusRead read;
BusWrite write;
} Bus;
#endif /* !VM_BUS_H */

23
vm/cpu.c Normal file
View file

@ -0,0 +1,23 @@
#include <stdlib.h>
#include "cpu.h"
void
invalid_opcode(Cpu *cpu)
{
exit(EXIT_FAILURE);
}
void
cpu_execute(Cpu *cpu)
{
uint8_t opcode;
switch (opcode)
{
default:
invalid_opcode();
break;
}
cpu->PC += 1;
}

16
vm/cpu.h Normal file
View file

@ -0,0 +1,16 @@
#ifndef VM_CPU_H
# define VM_CPU_H 1
# include <stdint.h>
typedef struct
{
uint32_t PC;
uint32_t A;
uint32_t X;
uint32_t Y;
uint8_t SR;
uint32_t SP;
} Cpu;
#endif /* !VM_CPU_H */

View file

@ -1,18 +1,65 @@
#include "config.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#ifdef HAVE_LIBGEN_H
# include <libgen.h>
#endif /* HAVE_LIBGEN_H */
#include <SDL.h>
static const char *prg_name; static const char *prg_name;
void static void
usage(void) usage(int retcode)
{ {
printf("Usage: %s [-hV] [-b bios]\n"); if (retcode == EXIT_FAILURE)
{
fprintf(stderr,
"Try '%s -h' for more information.\n", prg_name);
}
else
{
printf("Usage: %s [-hV] [-b bios]\n", prg_name);
printf("\t-h\tdisplay this help and exit\n");
printf("\t-V\toutput version information\n");
printf("\nReport bugs to <%s>\n", PACKAGE_BUGREPORT);
}
exit(retcode);
}
static void
version(void)
{
printf("%S version %s\n", PACKAGE_NAME, PACKAGE_VERSION);
exit(EXIT_SUCCESS);
} }
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
#ifdef HAVE_LIBGEN_H
prg_name = basename(argv[0]);
#else
prg_name = argv[0]; prg_name = argv[0];
#endif /* HAVE_LIBGEN_H */
while ((argc > 1) && (argv[1][0] == '-'))
{
switch (argv[1][1])
{
case 'h':
usage(EXIT_SUCCESS);
break;
case 'V':
version();
break;
default:
usage(EXIT_FAILURE);
break;
}
argv++;
argc--;
}
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }

32
vm/net/tap-linux.c Normal file
View file

@ -0,0 +1,32 @@
#include <linux/if_tun.h>
/*
* man 7 netdevice
* man 4 tap
*/
void
tap_new(void)
{
int fd;
struct ifreq req;
fd = open("/dev/net/tun", O_RDWR);
if (fd < 0)
{
return;
}
memset(&req, 0, sizeof(struct ifreq));
req.ifr_flags = IFF_TAP | IFF_NO_PI;
/*
* malloc(IFNAMSIZ);
* tun_alloc(xxxx, IFF_TAP);
*/
strncpy(req.ifr_name, "todo", 5);
if (ioctl(fd, TUNSETIFF, &req) < 0)
{
return;
}
}

17
vm/vm.h Normal file
View file

@ -0,0 +1,17 @@
#ifndef VM_VM_H
# define VM_VM_H 1
# include "cpu.h"
# include "bus.h"
# define VM_MAX_CPU 8
typedef struct
{
int cpu_count;
Cpu cpus[VM_MAX_CPU];
Bus bus;
} Vm;
#endif /* !VM_VM_H */