summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--etc/oemu/emutest.rc5
-rw-r--r--etc/oemu/test-00.s13
-rw-r--r--usr.bin/oasm/emit.c42
-rw-r--r--usr.bin/oasm/include/oasm/emit.h1
-rw-r--r--usr.bin/oasm/include/oasm/lex.h1
-rw-r--r--usr.bin/oasm/lex.c35
-rw-r--r--usr.bin/oasm/parse.c6
-rw-r--r--usr.bin/oemu/cpu.c44
-rw-r--r--usr.bin/oemu/include/oemu/cpu.h13
-rw-r--r--usr.bin/oemu/include/oemu/osmx64.h1
-rw-r--r--usr.bin/oemu/include/oemu/types.h3
11 files changed, 158 insertions, 6 deletions
diff --git a/etc/oemu/emutest.rc b/etc/oemu/emutest.rc
new file mode 100644
index 0000000..39607dd
--- /dev/null
+++ b/etc/oemu/emutest.rc
@@ -0,0 +1,5 @@
+@ Test emulator
+echo Generating /tmp/a.out
+oasm /etc/oemu/test-00.s /tmp/a.out
+echo Running emulator...
+oemu /tmp/a.out
diff --git a/etc/oemu/test-00.s b/etc/oemu/test-00.s
new file mode 100644
index 0000000..b34c099
--- /dev/null
+++ b/etc/oemu/test-00.s
@@ -0,0 +1,13 @@
+mov x0, #1 ! ~ 0x00000000
+mov x1, #2 ! ~ 0x00000004
+mov x2, #3 ! ~ 0x00000008
+ ! X
+mov x2, #20 ! ~ 0x0000000C
+br x2 ! ~ 0x00000010 -----+
+!!!!!!!!!!!!!! X |
+!!!!!!!!!!!!!! X |
+ ! X |
+dec x0 ! ~ 0x00000014 <----+
+inc x1 ! ~ 0x00000018
+add x2, #3 ! ~ 0x0000001C
+hlt ! ~ 0x00000020
diff --git a/usr.bin/oasm/emit.c b/usr.bin/oasm/emit.c
index 0e8ad4e..eb38779 100644
--- a/usr.bin/oasm/emit.c
+++ b/usr.bin/oasm/emit.c
@@ -257,6 +257,45 @@ emit_encode_hlt(struct emit_state *state, struct oasm_token *tok)
return TAILQ_NEXT(tok, link);
}
+/*
+ * Encode a BR instruction
+ *
+ * br [r]
+ *
+ * Returns the next token on success,
+ * otherwise NULL.
+ */
+static struct oasm_token *
+emit_encode_br(struct emit_state *state, struct oasm_token *tok)
+{
+ inst_t curinst;
+ reg_t rd;
+ uint8_t opcode = OSMX64_BR;
+ char *inst_str = "br";
+
+ /* Grab the register */
+ tok = TAILQ_NEXT(tok, link);
+ if (tok == NULL) {
+ return NULL;
+ }
+ if (!tok_is_xreg(tok->type)) {
+ oasm_err("[emit error]: expected register in '%s'\n", inst_str);
+ return NULL;
+ }
+
+ rd = ir_to_reg(tok->type);
+ if (rd == OSMX64_R_BAD) {
+ oasm_err("[emit error]: got bad in register in '%s'\n", inst_str);
+ return NULL;
+ }
+
+ curinst.opcode = opcode;
+ curinst.rd = rd;
+ curinst.unused = 0;
+ emit_bytes(state, &curinst, sizeof(curinst));
+ return TAILQ_NEXT(tok, link);
+}
+
int
emit_osmx64(struct emit_state *state, struct oasm_token *tp)
{
@@ -341,6 +380,9 @@ emit_process(struct oasm_state *oasm, struct emit_state *emit)
case TT_DIV:
curtok = emit_encode_arith(emit, curtok);
break;
+ case TT_BR:
+ curtok = emit_encode_br(emit, curtok);
+ break;
case TT_HLT:
curtok = emit_encode_hlt(emit, curtok);
break;
diff --git a/usr.bin/oasm/include/oasm/emit.h b/usr.bin/oasm/include/oasm/emit.h
index d289adb..dab63d0 100644
--- a/usr.bin/oasm/include/oasm/emit.h
+++ b/usr.bin/oasm/include/oasm/emit.h
@@ -61,6 +61,7 @@
#define OSMX64_SRL 0x12 /* Shift right logical operation */
#define OSMX64_MOV_IMM 0x13 /* Data move operation from IMM */
#define OSMX64_HLT 0x14 /* Halt the processor */
+#define OSMX64_BR 0x15 /* Branch */
/*
* OSMX64 register definitions
diff --git a/usr.bin/oasm/include/oasm/lex.h b/usr.bin/oasm/include/oasm/lex.h
index 6ffaf79..62183e0 100644
--- a/usr.bin/oasm/include/oasm/lex.h
+++ b/usr.bin/oasm/include/oasm/lex.h
@@ -96,6 +96,7 @@ typedef enum {
TT_MUL, /* 'mul' */
TT_DIV, /* 'div' */
TT_HLT, /* 'hlt' */
+ TT_BR, /* 'br' */
/* Register ops */
TT_MOV, /* 'mov' */
diff --git a/usr.bin/oasm/lex.c b/usr.bin/oasm/lex.c
index 7b78d54..a33a570 100644
--- a/usr.bin/oasm/lex.c
+++ b/usr.bin/oasm/lex.c
@@ -34,6 +34,7 @@
#include <oasm/lex.h>
#include <oasm/log.h>
+#define COMMENT '!'
#define is_num(c) ((c) >= '0' && (c) <= '9')
static char putback = '\0';
@@ -47,6 +48,7 @@ static char putback = '\0';
#define S_IMN_INC "inc"
#define S_IMN_DEC "dec"
#define S_IMN_HLT "hlt"
+#define S_IMN_BR "br"
/*
* Returns 0 if a char is counted as a
@@ -190,6 +192,19 @@ token_arith(char *p)
return TT_UNKNOWN;
}
+/*
+ * Control flow instructions
+ */
+static tt_t
+token_cfi(char *p)
+{
+ if (strcmp(p, S_IMN_BR) == 0) {
+ return TT_BR;
+ }
+
+ return TT_UNKNOWN;
+}
+
static tt_t
token_xreg(char *p)
{
@@ -254,6 +269,7 @@ lex_tok(struct oasm_state *state, struct oasm_token *ttp)
{
char *p = NULL;
char c = ' ';
+ short in_comment = 0;
int tmp;
tt_t tok;
@@ -262,13 +278,19 @@ lex_tok(struct oasm_state *state, struct oasm_token *ttp)
}
/*
- * Grab characters. If they are skippable,
- * don't use them.
+ * Grab characters. If they are skippable or
+ * comments, don't use them.
*/
- while (lex_skippable(state, c) == 0) {
+ while (lex_skippable(state, c) == 0 || in_comment) {
if ((c = lex_cin(state)) == 0) {
return -1;
}
+
+ if (c == COMMENT) {
+ in_comment = 1;
+ } else if (c == '\n') {
+ in_comment = 0;
+ }
}
switch (c) {
@@ -293,6 +315,13 @@ lex_tok(struct oasm_state *state, struct oasm_token *ttp)
return 0;
}
+ /* Control flow instruction? */
+ if ((tok = token_cfi(p)) != TT_UNKNOWN) {
+ ttp->type = tok;
+ ttp->raw = p;
+ return 0;
+ }
+
/* Register? */
if ((tok = token_reg(p)) != TT_UNKNOWN) {
ttp->is_reg = 1;
diff --git a/usr.bin/oasm/parse.c b/usr.bin/oasm/parse.c
index 9a4dd0e..e288d6e 100644
--- a/usr.bin/oasm/parse.c
+++ b/usr.bin/oasm/parse.c
@@ -43,6 +43,7 @@ static const char *tokstr[] = {
[ TT_MUL ] = "mul",
[ TT_DIV ] = "div",
[ TT_HLT ] = "hlt",
+ [ TT_BR ] = "br",
[ TT_COMMA ] = ",",
[ TT_INC ] = "inc",
[ TT_DEC ] = "dec",
@@ -112,6 +113,7 @@ parse_reg(struct oasm_state *state, struct oasm_token *tok)
case TT_SUB:
case TT_MUL:
case TT_DIV:
+ case TT_BR:
state->last = tok->type;
break;
default:
@@ -144,6 +146,10 @@ parse_tok(struct oasm_state *state, struct oasm_token *tok)
int error;
switch (tok->type) {
+ case TT_BR:
+ state->last = tok->type;
+ emit_osmx64(&emit_state, tok);
+ break;
case TT_HLT:
state->last = tok->type;
emit_osmx64(&emit_state, tok);
diff --git a/usr.bin/oemu/cpu.c b/usr.bin/oemu/cpu.c
index 58dd134..07ddc7b 100644
--- a/usr.bin/oemu/cpu.c
+++ b/usr.bin/oemu/cpu.c
@@ -198,6 +198,33 @@ cpu_div(struct oemu_cpu *cpu, inst_t *inst)
}
/*
+ * Decode the INST_DIV instruction
+ */
+static void
+cpu_br(struct oemu_cpu *cpu, inst_t *inst)
+{
+ struct cpu_regs *regs = &cpu->regs;
+ imm_t imm;
+ addr_t br_to;
+
+ if (inst->rd > NELEM(regs->xreg)) {
+ printf("bad register operand for 'br'\n");
+ return;
+ }
+
+ /*
+ * If we are branching to the reset vector, might
+ * as well reset all state.
+ */
+ br_to = regs->xreg[inst->rd];
+ if (br_to == 0) {
+ cpu_reset(cpu);
+ }
+
+ regs->ip = br_to;
+}
+
+/*
* Reset a CPU to a default state
*/
void
@@ -205,8 +232,16 @@ cpu_reset(struct oemu_cpu *cpu)
{
struct cpu_regs *regs;
+ /*
+ * When an OSMX64 processor first starts up, it will
+ * initially be executing in supervisor mode with all
+ * of its registeres initialized to zeros.
+ */
regs = &cpu->regs;
regs->ip = 0;
+ regs->sr_state = CPU_SRS_SV;
+ regs->blr = 0x0;
+ regs->ilr = 0x0;
memset(regs->xreg, 0x0, sizeof(regs->xreg));
}
@@ -222,7 +257,8 @@ cpu_regdump(struct oemu_cpu *cpu)
"X6=%p, X7=%p, X8=%p\n"
"X9=%p, X10=%p, X11=%p\n"
"X12=%p, X13=%p, X14=%p\n"
- "X15=%p, IP=%p\n",
+ "X15=%p, IP=%p, SRS=%p\n"
+ "BLR=%p, ILR=%p\n",
regs->xreg[0], regs->xreg[1],
regs->xreg[2], regs->xreg[3],
regs->xreg[4], regs->xreg[5],
@@ -231,7 +267,8 @@ cpu_regdump(struct oemu_cpu *cpu)
regs->xreg[10], regs->xreg[11],
regs->xreg[12], regs->xreg[13],
regs->xreg[14], regs->xreg[15],
- regs->ip
+ regs->ip, regs->sr_state,
+ regs->blr, regs->ilr
);
}
@@ -270,6 +307,9 @@ cpu_kick(struct oemu_cpu *cpu, struct sysmem *mem)
case INST_DIV:
cpu_div(cpu, inst);
break;
+ case INST_BR:
+ cpu_br(cpu, inst);
+ break;
}
/* Is this a halt instruction? */
diff --git a/usr.bin/oemu/include/oemu/cpu.h b/usr.bin/oemu/include/oemu/cpu.h
index 78b92da..882fe93 100644
--- a/usr.bin/oemu/include/oemu/cpu.h
+++ b/usr.bin/oemu/include/oemu/cpu.h
@@ -31,6 +31,7 @@
#define _OEMU_CPU_H_
#include <sys/types.h>
+#include <sys/param.h>
#include <stdint.h>
#include <stddef.h>
#include <oemu/types.h>
@@ -38,6 +39,12 @@
#define MEMORY_SIZE 512
/*
+ * Processor state register
+ */
+#define CPU_SRS_SV BIT(1) /* Supervisor flag */
+#define CPU_SRS_CARRY BIT(2) /* Carry flag */
+
+/*
* System memory
*
* @mem: Data
@@ -53,10 +60,16 @@ struct sysmem {
*
* @xreg: X<n>
* @ip: Instruction pointer
+ * @sr_state: Processor state register
+ * @blr: Branch link register
+ * @ilr: Interrupt link register
*/
struct cpu_regs {
reg_t xreg[16];
reg_t ip;
+ reg_t sr_state;
+ reg_t blr;
+ reg_t ilr;
};
struct oemu_cpu {
diff --git a/usr.bin/oemu/include/oemu/osmx64.h b/usr.bin/oemu/include/oemu/osmx64.h
index e9baae0..b1df5d3 100644
--- a/usr.bin/oemu/include/oemu/osmx64.h
+++ b/usr.bin/oemu/include/oemu/osmx64.h
@@ -48,6 +48,7 @@
#define INST_SRL 0x12 /* Shift right logical operation */
#define INST_MOV_IMM 0x13 /* Data move operation from IMM */
#define INST_HLT 0x14 /* Halt */
+#define INST_BR 0x15 /* Branch */
/* Registers */
#define REG_X0 0x00
diff --git a/usr.bin/oemu/include/oemu/types.h b/usr.bin/oemu/include/oemu/types.h
index 0769c84..caf6e9b 100644
--- a/usr.bin/oemu/include/oemu/types.h
+++ b/usr.bin/oemu/include/oemu/types.h
@@ -33,7 +33,8 @@
#include <sys/types.h>
typedef uint64_t reg_t;
-typedef uintptr_t paddr_t;
+typedef uintptr_t addr_t;
typedef uint16_t imm_t;
+typedef addr_t paddr_t;
#endif /* !_OEMU_TYPES_H_ */