uxn/uxn.c

101 lines
1.6 KiB
C
Raw Normal View History

2021-01-29 14:17:59 -05:00
#include <stdio.h>
2021-01-29 14:35:59 -05:00
/*
2021-01-29 14:17:59 -05:00
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.
*/
2021-01-29 14:35:59 -05:00
#define STACK_DEPTH 256
2021-01-29 15:14:37 -05:00
#define ECHO 1
typedef unsigned char Uint8;
2021-01-29 16:59:16 -05:00
typedef unsigned char Uint16;
2021-01-29 15:14:37 -05:00
typedef struct {
2021-01-29 14:35:59 -05:00
2021-01-29 15:14:37 -05:00
} Computer;
Uint8 sptr;
Uint8 stack[STACK_DEPTH];
2021-01-29 14:35:59 -05:00
Uint8 address[STACK_DEPTH];
2021-01-29 16:59:16 -05:00
Uint16 memory[STACK_DEPTH];
2021-01-29 14:35:59 -05:00
void
2021-01-29 16:59:16 -05:00
echo(Uint8 *s, Uint8 len, char *name)
2021-01-29 14:35:59 -05:00
{
int i;
2021-01-29 16:59:16 -05:00
printf("%s\n", name);
2021-01-29 15:14:37 -05:00
for(i = 0; i < len; ++i) {
2021-01-29 14:35:59 -05:00
if(i % 16 == 0)
printf("\n");
2021-01-29 15:14:37 -05:00
if(sptr == i)
printf("[%02x]", s[i]);
else
printf(" %02x ", s[i]);
2021-01-29 14:35:59 -05:00
}
printf("\n");
}
2021-01-29 15:14:37 -05:00
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;
2021-01-29 16:59:16 -05:00
2021-01-29 15:14:37 -05:00
for(i = 0; i < 128; i++) {
cpu->memory[i * 2] |= (buffer[i] >> 8) & 0xFF;
cpu->memory[i * 2 + 1] |= buffer[i] & 0xFF;
}
2021-01-29 16:59:16 -05:00
2021-01-29 15:14:37 -05:00
return 1;
}
void
run(Computer *cpu, int debug)
{
}
2021-01-29 14:17:59 -05:00
int
main(int argc, char *argv[])
{
2021-01-29 15:14:37 -05:00
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);
/* print result */
2021-01-29 16:59:16 -05:00
echo(stack, 0x40, "stack");
echo(memory, 0x40, "memory");
2021-01-29 14:17:59 -05:00
return 0;
}