mirror of
https://github.com/pnx/m16vm
synced 2026-06-16 03:44:55 +02:00
move /src/as to /as and /src to /vm
This commit is contained in:
parent
8ef4c7edcd
commit
d73e2ab710
25 changed files with 6 additions and 6 deletions
160
vm/cpu.c
Normal file
160
vm/cpu.c
Normal file
|
|
@ -0,0 +1,160 @@
|
|||
/* cpu.c
|
||||
*
|
||||
* Copyright (C) 2012,2014-2015 Henrik Hautakoski <henrik.hautakoski@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include "cpu.h"
|
||||
#include "mm.h"
|
||||
#include "syscall.h"
|
||||
#include "instr_decode.h"
|
||||
|
||||
/* CPU flags */
|
||||
#define CPU_FLAGS_HALT (1<<0)
|
||||
|
||||
#ifdef M16_DEBUG_INSTR
|
||||
#define debug(...) fprintf(stderr, __VA_ARGS__)
|
||||
#else
|
||||
#define debug(...)
|
||||
#endif /* M16_DEBUG_INSTR */
|
||||
|
||||
static void execute(struct cpu_state *state, struct instr *instr) {
|
||||
|
||||
switch(instr->opcode) {
|
||||
case OP_NOOP :
|
||||
debug("noop\n");
|
||||
/* Do nothing */
|
||||
break;
|
||||
case OP_ADD :
|
||||
debug("add\tr%i r%i r%i\n", instr->r.rs, instr->r.r0, instr->r.r1);
|
||||
state->reg[instr->r.rs] = state->reg[instr->r.r0] + state->reg[instr->r.r1];
|
||||
break;
|
||||
case OP_MOVL :
|
||||
debug("movl\tr%i #%i\n", instr->i.rs, instr->i.imm);
|
||||
state->reg[instr->r.rs] = (state->reg[instr->r.rs] & 0xFF00) | instr->i.imm;
|
||||
break;
|
||||
case OP_MOVH :
|
||||
debug("movh\tr%i #%i\n", instr->i.rs, instr->i.imm);
|
||||
state->reg[instr->r.rs] = (state->reg[instr->r.rs] & 0x00FF) | (((uint16_t) instr->i.imm) << 8);
|
||||
break;
|
||||
case OP_LW :
|
||||
debug("lw\tr%i r%i #%i\n", instr->ri.rs, instr->ri.r0, instr->ri.offset);
|
||||
state->reg[instr->r.rs] = mm_lw(state->reg[instr->ri.r0] + instr->ri.offset);
|
||||
break;
|
||||
case OP_SW :
|
||||
debug("sw\tr%i r%i #%i\n", instr->ri.rs, instr->ri.r0, instr->ri.offset);
|
||||
mm_sw(state->reg[instr->ri.rs] + instr->ri.offset, state->reg[instr->ri.r0]);
|
||||
break;
|
||||
case OP_JMP :
|
||||
debug("jmp\t#%i\n", instr->j.addr);
|
||||
cpu_set_pc(state, instr->j.addr);
|
||||
break;
|
||||
case OP_JR :
|
||||
debug("jr\tr%i(#%i)\n", instr->i.rs, instr->i.imm);
|
||||
cpu_set_pc(state, state->reg[instr->r.rs] + instr->i.imm);
|
||||
break;
|
||||
case OP_BEQ :
|
||||
debug("beq\tr%i r%i #%i\n", instr->ri.rs, instr->ri.r0, instr->ri.offset);
|
||||
// Compare rs, r0
|
||||
if (state->reg[instr->ri.rs] == state->reg[instr->ri.r0])
|
||||
cpu_set_pc(state, state->pc + instr->ri.offset);
|
||||
break;
|
||||
case OP_INT :
|
||||
debug("int %i(#%i)\n", instr->r.rs, instr->i.imm);
|
||||
vm_syscall(instr->i.rs, instr->i.imm, state->reg);
|
||||
break;
|
||||
default :
|
||||
fprintf(stderr, "Invalid instruction (%.2X)\n", instr->opcode);
|
||||
state->flags |= CPU_FLAGS_HALT;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void cpu_init(struct cpu_state *state)
|
||||
{
|
||||
state->pc = 0;
|
||||
state->flags = 0;
|
||||
memset(state->reg, 0, sizeof(state->reg[0]) * CPU_NUM_REGS);
|
||||
|
||||
state->instr_mem = NULL;
|
||||
state->instr_cnt = 0;
|
||||
}
|
||||
|
||||
int cpu_instr_load(struct cpu_state *state, void *ptr, unsigned len) {
|
||||
|
||||
if (len % 2) {
|
||||
fprintf(stderr, "Error: Instruction length must be a multiple of 2\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
state->instr_mem = ptr;
|
||||
state->instr_cnt = len;
|
||||
return len;
|
||||
}
|
||||
|
||||
void cpu_instr_unload(struct cpu_state *state) {
|
||||
|
||||
state->instr_mem = NULL;
|
||||
state->instr_cnt = 0;
|
||||
}
|
||||
|
||||
void cpu_set_pc(struct cpu_state *state, uint16_t addr) {
|
||||
|
||||
if (addr > state->instr_cnt / 2) {
|
||||
fprintf(stderr, "Runtime error: Invalid instruction address %ui\n", addr);
|
||||
state->flags |= CPU_FLAGS_HALT;
|
||||
return;
|
||||
}
|
||||
state->pc = addr;
|
||||
}
|
||||
|
||||
static unsigned char* instr_fetch(struct cpu_state *state) {
|
||||
|
||||
if (state->pc + 1 >= state->instr_cnt >> 1)
|
||||
state->flags |= CPU_FLAGS_HALT;
|
||||
|
||||
return state->instr_mem + (state->pc++ << 1);
|
||||
}
|
||||
|
||||
int cpu_tick(struct cpu_state *state) {
|
||||
|
||||
struct instr instr;
|
||||
unsigned char* next;
|
||||
|
||||
if (state->flags & CPU_FLAGS_HALT) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Fetch next instruction
|
||||
next = instr_fetch(state);
|
||||
|
||||
// decode instruction.
|
||||
instr_decode(next, &instr);
|
||||
|
||||
// Execute it.
|
||||
execute(state, &instr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
55
vm/cpu.h
Normal file
55
vm/cpu.h
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
/* cpu.h
|
||||
*
|
||||
* Copyright (C) 2012,2014-2015 Henrik Hautakoski <henrik.hautakoski@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
#ifndef CPU_H
|
||||
#define CPU_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "instr.h"
|
||||
|
||||
#define CPU_NUM_REGS 16
|
||||
|
||||
#define CPU_FLAGS_HALT (1<<0)
|
||||
|
||||
struct cpu_state {
|
||||
// Registers r0, r15
|
||||
int16_t reg[CPU_NUM_REGS];
|
||||
|
||||
// Program counter
|
||||
uint16_t pc;
|
||||
|
||||
// flags
|
||||
unsigned char flags;
|
||||
|
||||
// Instruction
|
||||
unsigned char *instr_mem;
|
||||
unsigned long instr_cnt;
|
||||
};
|
||||
|
||||
void cpu_init(struct cpu_state *state);
|
||||
|
||||
int cpu_instr_load(struct cpu_state *state, void *ptr, unsigned len);
|
||||
|
||||
void cpu_instr_unload(struct cpu_state *state);
|
||||
|
||||
void cpu_set_pc(struct cpu_state *state, uint16_t addr);
|
||||
|
||||
int cpu_tick(struct cpu_state *state);
|
||||
|
||||
#endif /* CPU_H */
|
||||
52
vm/instr_decode.c
Normal file
52
vm/instr_decode.c
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
/* instr_decode.c
|
||||
*
|
||||
* Copyright (C) 2012,2014 Henrik Hautakoski <henrik.hautakoski@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
#include "instr_decode.h"
|
||||
|
||||
void instr_decode(unsigned char *instr, struct instr *out) {
|
||||
|
||||
out->opcode = *instr >> 4;
|
||||
|
||||
if (out->opcode == OP_NOOP)
|
||||
return;
|
||||
|
||||
// J-Type
|
||||
if (out->opcode == OP_JMP) {
|
||||
|
||||
out->j.addr = ((*instr & 0xF) << 8) + *(instr + 1);
|
||||
|
||||
// if MSB (bit 12) is set
|
||||
// perform 2s complement by setting bit 13-16 to 1
|
||||
/*
|
||||
if (out->j.addr & 0x0800)
|
||||
out->j.addr |= 0xF000; */
|
||||
} else {
|
||||
out->r.rs = *instr & 0xF;
|
||||
|
||||
// I-Type
|
||||
if (out->opcode == OP_MOVL || out->opcode == OP_MOVH || out->opcode == OP_INT) {
|
||||
out->i.imm = *(instr + 1);
|
||||
}
|
||||
// R/RI-Type
|
||||
else {
|
||||
out->r.r0 = *(instr + 1) >> 4;
|
||||
out->r.r1 = *(instr + 1) & 0xF;
|
||||
}
|
||||
}
|
||||
}
|
||||
27
vm/instr_decode.h
Normal file
27
vm/instr_decode.h
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/* instr_decode.h
|
||||
*
|
||||
* Copyright (C) 2012,2014 Henrik Hautakoski <henrik.hautakoski@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
#ifndef INSTR_DECODE_H
|
||||
#define INSTR_DECODE_H
|
||||
|
||||
#include <instr.h>
|
||||
|
||||
void instr_decode(unsigned char *nibble, struct instr *instr);
|
||||
|
||||
#endif /* INSTR_DECODE_H */
|
||||
69
vm/mm.c
Normal file
69
vm/mm.c
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
/* mm.c
|
||||
*
|
||||
* Copyright (C) 2012,2014 Henrik Hautakoski <henrik.hautakoski@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "mm.h"
|
||||
|
||||
#ifndef MEM_SIZE
|
||||
/* Set a default memory size. (2^16) */
|
||||
#define MEM_SIZE 65536
|
||||
#endif
|
||||
|
||||
#define EXCEPTION_STRING "Runtime exception: Memory address '%u' is out of bounds.\n"
|
||||
|
||||
int16_t* base_addr = NULL;
|
||||
uint8_t *memory = NULL;
|
||||
|
||||
static void check_bounds(uint16_t addr) {
|
||||
|
||||
if (addr > MEM_SIZE) {
|
||||
fprintf(stderr, EXCEPTION_STRING, addr);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
void mm_init() {
|
||||
|
||||
base_addr = malloc(MEM_SIZE);
|
||||
memory = (uint8_t*) base_addr;
|
||||
}
|
||||
|
||||
void mm_exit() {
|
||||
|
||||
if (base_addr) {
|
||||
free(base_addr);
|
||||
base_addr = NULL;
|
||||
memory = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void mm_sw(uint16_t addr, int16_t value) {
|
||||
|
||||
check_bounds(addr);
|
||||
|
||||
base_addr[addr] = value;
|
||||
}
|
||||
|
||||
int16_t mm_lw(uint16_t addr) {
|
||||
|
||||
check_bounds(addr);
|
||||
|
||||
return base_addr[addr];
|
||||
}
|
||||
37
vm/mm.h
Normal file
37
vm/mm.h
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
/* mm.h
|
||||
*
|
||||
* Copyright (C) 2012,2014 Henrik Hautakoski <henrik.hautakoski@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
#ifndef MM_H
|
||||
#define MM_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
extern uint8_t *memory;
|
||||
|
||||
void mm_init();
|
||||
|
||||
void mm_exit();
|
||||
|
||||
/* Save a word to memory. */
|
||||
void mm_sw(uint16_t addr, int16_t value);
|
||||
|
||||
/* Load a word from memory. */
|
||||
int16_t mm_lw(uint16_t addr);
|
||||
|
||||
#endif /* MM_H */
|
||||
45
vm/program.c
Normal file
45
vm/program.c
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include "program.h"
|
||||
|
||||
int program_loadfromfile(struct program *prog, const char *filename) {
|
||||
|
||||
struct stat st;
|
||||
ssize_t rc = -1;
|
||||
int fd;
|
||||
|
||||
fd = open(filename, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
fprintf(stderr, "Could not open file %s: %s\n",
|
||||
filename, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (fstat(fd, &st) < 0)
|
||||
goto close_fd;
|
||||
|
||||
prog->len = st.st_size;
|
||||
prog->instr = malloc(prog->len);
|
||||
if (prog->instr == NULL)
|
||||
goto close_fd;
|
||||
|
||||
rc = read(fd, prog->instr, prog->len);
|
||||
if (rc < 0)
|
||||
goto free_mem;
|
||||
return rc;
|
||||
free_mem: free(prog->instr);
|
||||
close_fd: close(fd);
|
||||
return rc;
|
||||
}
|
||||
|
||||
void program_free(struct program *prog) {
|
||||
|
||||
free(prog->instr);
|
||||
prog->len = 0;
|
||||
}
|
||||
32
vm/program.h
Normal file
32
vm/program.h
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
/* program.h
|
||||
*
|
||||
* Copyright (C) 2012,2014 Henrik Hautakoski <henrik.hautakoski@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
#ifndef LOADER_H
|
||||
#define LOADER_H
|
||||
|
||||
struct program {
|
||||
unsigned char *instr;
|
||||
unsigned len;
|
||||
};
|
||||
|
||||
int program_loadfromfile(struct program *prog, const char *filename);
|
||||
|
||||
void program_free(struct program *prog);
|
||||
|
||||
#endif /* LOADER_H */
|
||||
44
vm/syscall.c
Normal file
44
vm/syscall.c
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/* syscall.h
|
||||
*
|
||||
* Copyright (C) 2012,2014-2015 Henrik Hautakoski <henrik.hautakoski@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include "syscall.h"
|
||||
|
||||
void vm_syscall(int16_t number, int8_t op, uint16_t* regs) {
|
||||
|
||||
if (number == SYS_WRITE) {
|
||||
vm_syscall_write(op, regs[15]);
|
||||
}
|
||||
}
|
||||
|
||||
void vm_syscall_write(int8_t op, int16_t value) {
|
||||
|
||||
switch(op) {
|
||||
case SYSW_INT16 :
|
||||
printf("%i", value);
|
||||
break;
|
||||
case SYSW_INT8 :
|
||||
printf("%i", (int8_t) value);
|
||||
break;
|
||||
case SYSW_CHAR :
|
||||
default:
|
||||
printf("%c", (unsigned char) value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
36
vm/syscall.h
Normal file
36
vm/syscall.h
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/* syscall.h
|
||||
*
|
||||
* Copyright (C) 2012,2014-2015 Henrik Hautakoski <henrik.hautakoski@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#ifndef SYSCALL_H
|
||||
#define SYSCALL_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define SYS_WRITE 10
|
||||
|
||||
#define SYSW_INT16 0x0
|
||||
#define SYSW_INT8 0x1
|
||||
#define SYSW_CHAR 0x2
|
||||
|
||||
void vm_syscall(int16_t number, int8_t op, uint16_t* regs);
|
||||
|
||||
void vm_syscall_write(int8_t op, int16_t value);
|
||||
|
||||
#endif /* SYSCALL_H */
|
||||
96
vm/vm.c
Normal file
96
vm/vm.c
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
/* vm.c
|
||||
*
|
||||
* Copyright (C) 2012,2014 Henrik Hautakoski <henrik.hautakoski@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include "program.h"
|
||||
#include "mm.h"
|
||||
#include "cpu.h"
|
||||
|
||||
#ifdef M16_DEBUG_MEM
|
||||
void print_memory() {
|
||||
|
||||
int i = 0;
|
||||
|
||||
printf("\n");
|
||||
|
||||
for(i = 0; i < 32; i++) {
|
||||
printf("%.2X ", memory[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
#else
|
||||
#define print_memory()
|
||||
#endif /* ! M16_DEBUG_MEM */
|
||||
|
||||
#ifdef M16_DEBUG_REG
|
||||
void print_regs(int16_t *regs) {
|
||||
|
||||
int i = 0;
|
||||
|
||||
printf("\n");
|
||||
|
||||
for(i = 0; i < CPU_NUM_REGS; i++) {
|
||||
printf("r%i = %i\n", i, regs[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
#else
|
||||
#define print_regs(regs)
|
||||
#endif /* ! M16_DEBUG_REG */
|
||||
|
||||
void run(struct program *prog) {
|
||||
|
||||
struct cpu_state state;
|
||||
int rc;
|
||||
|
||||
mm_init();
|
||||
cpu_init(&state);
|
||||
|
||||
cpu_instr_load(&state, prog->instr, prog->len);
|
||||
|
||||
do {
|
||||
rc = cpu_tick(&state);
|
||||
} while(rc == 0);
|
||||
|
||||
print_regs(state.reg);
|
||||
|
||||
print_memory();
|
||||
|
||||
mm_exit();
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
struct program prog = { 0 };
|
||||
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "usage: %s <file>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (program_loadfromfile(&prog, argv[1]) < 0)
|
||||
return 1;
|
||||
|
||||
// Execute the program.
|
||||
run(&prog);
|
||||
|
||||
program_free(&prog);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue