diff --git a/dos/dos.k b/dos/dos.k index 4485a55..20f5c88 100644 --- a/dos/dos.k +++ b/dos/dos.k @@ -1,4 +1,4 @@ -; The OS/K Team licences this file to you under the MIT license. +; The OS/K Team licenses this file to you under the MIT license. ; See the LICENSE file in the project root for more information. ; diff --git a/dos/main.k b/dos/main.k index d6aa8b1..fcce8f3 100644 --- a/dos/main.k +++ b/dos/main.k @@ -1,4 +1,4 @@ -; The OS/K Team licences this file to you under the MIT license. +; The OS/K Team licenses this file to you under the MIT license. ; See the LICENSE file in the project root for more information. ; diff --git a/dos/print.k b/dos/print.k index dc39cc8..b12ddcf 100644 --- a/dos/print.k +++ b/dos/print.k @@ -1,4 +1,4 @@ -; The OS/K Team licences this file to you under the MIT license. +; The OS/K Team licenses this file to you under the MIT license. ; See the LICENSE file in the project root for more information. ; diff --git a/lg/karch.lang b/lg/karch.lang index 8380b7f..fb00d2d 100644 --- a/lg/karch.lang +++ b/lg/karch.lang @@ -1,4 +1,8 @@ + @@ -13,7 +17,7 @@ /\* \*/ - + ^[A-Za-z_][A-Za-z_0-9.]*\: @@ -26,25 +30,25 @@ " " - + ' ' - + include - + ^\s+[a-zA-Z_]{2,} - + disf) @@ -39,14 +39,14 @@ static void scan_param(ctx_t *ctx, acc_t *p) p->mlen = c & 0xF; c = ctx->get(ctx); } - + else if (A_IS_OFF(c)) { p->mem = 1; p->mlen = c & 0xF; p->off = (short)ctx->get(ctx); c = ctx->get(ctx); } - + if (c >= A_IMM16) { // Check for double access code if (p->type != A_REG) @@ -57,26 +57,26 @@ static void scan_param(ctx_t *ctx, acc_t *p) } p->val = c; - + if (p->type == A_IMM32) { p->ilen = 4; p->val = p->val | ((ulong)ctx->get(ctx) << 16); } - + else if (p->type == A_IMM64) { p->ilen = 8; p->val = p->val | ((ulong)ctx->get(ctx) << 16); p->val = p->val | ((ulong)ctx->get(ctx) << 32); p->val = p->val | ((ulong)ctx->get(ctx) << 48); } - + else if (p->type == A_REG) { if (p->val > NREGS) { _except(ctx, E_ILL, "Inexistent REG"); } r = &ctx->r[p->val]; - + if (r->flags & (RES | CTL)) { _except(ctx, E_ACC, "Reserved REG: %s", r->name); } @@ -102,13 +102,13 @@ void disasm(ctx_t *ctx) c = ctx->get(ctx); } } - + else if (!ISINSTR(c)) { _except(ctx, E_ILL, "Bad opcode: 0x%04hX", c); } i = &ctx->i[c]; - + // Scan for parameters if (i->prm1 != NOPRM) { scan_param(ctx, &p1); @@ -120,7 +120,7 @@ void disasm(ctx_t *ctx) #ifdef _NEED_DISASM dumpinstr(ctx, rip, rep, c, &p1, &p2); #else - + do_rep: dumpinstr(ctx, rip, rep, c, &p1, &p2); i->func(ctx, &p1, &p2); diff --git a/pc/dump.c b/pc/dump.c index 6dd89b5..a7b79cf 100644 --- a/pc/dump.c +++ b/pc/dump.c @@ -31,16 +31,16 @@ void dumpfwstack(ctx_t *ctx) void d_log(ctx_t *ctx, char *fmt, ...) { va_list ap; - + va_start(ap, fmt); - + if (ctx->disf) { vfprintf(ctx->disf, fmt, ap); fflush(ctx->disf); } else vlog(fmt, ap); - + va_end(ap); } @@ -53,19 +53,19 @@ void dumpinstr(ctx_t *ctx, ulong rip, uint rep, d_log(ctx, "0x%08lX: %s%s", rip, (rep ? "rep " : ""), i->name); - + if (i->prm1 != NOPRM) p = p1; lp: if (p != 0) { if (p->mem) d_log(ctx, " %c:[", getmempref(p->mlen)); else d_log(ctx, " "); - + if (p->type == A_REG) d_log(ctx, "%s", ctx->r[p->val].name); else d_log(ctx, "%c:0x%lX", getmempref(p->ilen), p->val); - + if (p->mem) { if (p->off) d_log(ctx, "+%hd", p->off); diff --git a/pc/except.c b/pc/except.c index 8619df8..71d8968 100644 --- a/pc/except.c +++ b/pc/except.c @@ -8,11 +8,11 @@ void _except(ctx_t *ctx, int code, char *fmt, ...) va_list ap; log("\nException %d - ", code); - + va_start(ap, fmt); vlog(fmt, ap); va_end(ap); - + log("\n"); dumpregs(ctx); diff --git a/pc/instrs/INSTRS b/pc/instrs/INSTRS index 950ebe4..47efa90 100644 --- a/pc/instrs/INSTRS +++ b/pc/instrs/INSTRS @@ -1,4 +1,4 @@ -# The OS/K Team licences this file to you under the MIT license. +# The OS/K Team licenses this file to you under the MIT license. # See the LICENSE file in the project root for more information. stop diff --git a/pc/instrs/arch_i.py b/pc/instrs/arch_i.py index f51911f..f415a56 100644 --- a/pc/instrs/arch_i.py +++ b/pc/instrs/arch_i.py @@ -1,6 +1,6 @@ #!/usr/bin/python3 -# The OS/K Team licences this file to you under the MIT license. +# The OS/K Team licenses this file to you under the MIT license. # See the LICENSE file in the project root for more information. fi = open("INSTRS") @@ -24,15 +24,15 @@ def getflag(s): if s == "m": return "P_MEM" - + return "__ERROR__" for _, line in enumerate(fi): if line[0] == '#' or line[0] == ' ' or line[0] == '\n': continue - + tok = line.strip().split(' ') - + if len(tok) == 1: name = tok[0] p1 = "NOPRM" @@ -42,12 +42,12 @@ for _, line in enumerate(fi): name = "{}_{}".format(tok[0], tok[1].strip()) p1 = getflag(tok[1]) p2 = "NOPRM" - + elif len(tok) == 3: name = "{}_{}_{}".format(tok[0], tok[1], tok[2].strip()) p1 = getflag(tok[1]) p2 = getflag(tok[2]) - + else: name = "__ERROR__" p1 = "__ERROR__" diff --git a/pc/instrs/arith.c b/pc/instrs/arith.c index 638c864..c3d2503 100644 --- a/pc/instrs/arith.c +++ b/pc/instrs/arith.c @@ -1,4 +1,4 @@ -// The OS/K Team licences this file to you under the MIT license. +// The OS/K Team licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. #include "instrs.h" @@ -31,16 +31,16 @@ IMPL_START_1(mul) ulong t = (u1 * u2); ulong w3 = (t & 0xffffffff); ulong k = (t >> 32); - + v1 >>= 32; t = (v1 * u2) + k; k = (t & 0xffffffff); ulong w1 = (t >> 32); - + v2 >>= 32; t = (u1 * v2) + k; k = (t >> 32); - + ctx->r[RDX].val = (v1 * v2) + w1 + k; ctx->r[RAX].val = (t << 32) + w3; } diff --git a/pc/instrs/instrs.c b/pc/instrs/instrs.c index bbb3ecd..ee60cd3 100644 --- a/pc/instrs/instrs.c +++ b/pc/instrs/instrs.c @@ -1,4 +1,4 @@ -// The OS/K Team licences this file to you under the MIT license. +// The OS/K Team licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. #include "instrs.h" @@ -35,7 +35,7 @@ IMPL_START_1(lea) if (!p2->mem) { _except(ctx, E_ILL, "Bad LEA format"); } - + v1 = (p2->type == A_REG ? ctx->r[p2->val].val : p2->val) + p2->off; } IMPL_OUT; diff --git a/pc/instrs/instrs.h b/pc/instrs/instrs.h index 5f16425..ba84e9c 100644 --- a/pc/instrs/instrs.h +++ b/pc/instrs/instrs.h @@ -1,4 +1,4 @@ -// The OS/K Team licences this file to you under the MIT license. +// The OS/K Team licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. #include "../arch.h" @@ -65,6 +65,3 @@ void i_##name(ctx_t *ctx, acc_t *p1, acc_t *p2) \ #define JUMP(v) \ ctx->r[RIP].val = v + ctx->r[CR1].val - - - diff --git a/pc/instrs/jumps.c b/pc/instrs/jumps.c index 049c3ea..4fbf6fe 100644 --- a/pc/instrs/jumps.c +++ b/pc/instrs/jumps.c @@ -1,4 +1,4 @@ -// The OS/K Team licences this file to you under the MIT license. +// The OS/K Team licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. #include "instrs.h" diff --git a/pc/instrs/logic.c b/pc/instrs/logic.c index 721fa68..045534f 100644 --- a/pc/instrs/logic.c +++ b/pc/instrs/logic.c @@ -1,4 +1,4 @@ -// The OS/K Team licences this file to you under the MIT license. +// The OS/K Team licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. #include "instrs.h" @@ -11,7 +11,7 @@ IMPL_START_2(test) { ulong v = v1 & v2; - + if (v == 0) ctx->r[FLG].val |= ZF; else ctx->r[FLG].val &= ~ZF; } @@ -23,7 +23,7 @@ IMPL_START_2(cmp) ctx->r[FLG].val |= ZF; ctx->r[FLG].val &= ~CF; } - + else if (v1 < v2) { ctx->r[FLG].val &= ~ZF; ctx->r[FLG].val |= CF; diff --git a/pc/instrs/stack.c b/pc/instrs/stack.c index d46dcc1..cf96805 100644 --- a/pc/instrs/stack.c +++ b/pc/instrs/stack.c @@ -1,4 +1,4 @@ -// The OS/K Team licences this file to you under the MIT license. +// The OS/K Team licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. #include "instrs.h" @@ -67,7 +67,7 @@ IMPL_END; IMPL_START_0(popf) { CHK_STACK(<=); - + // XXX CHK_SUPERV(); @@ -76,4 +76,3 @@ IMPL_START_0(popf) } IMPL_END; - diff --git a/pc/instrs/super.c b/pc/instrs/super.c index a3812f2..b710291 100644 --- a/pc/instrs/super.c +++ b/pc/instrs/super.c @@ -1,4 +1,4 @@ -// The OS/K Team licences this file to you under the MIT license. +// The OS/K Team licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. #include "instrs.h" diff --git a/pc/log.c b/pc/log.c index 1ab4a6e..7ef5532 100644 --- a/pc/log.c +++ b/pc/log.c @@ -6,7 +6,7 @@ void log(const char *fmt, ...) { va_list ap; - + va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); diff --git a/pc/main.c b/pc/main.c index 1b37a33..fe6402b 100644 --- a/pc/main.c +++ b/pc/main.c @@ -1,4 +1,4 @@ -// The OS/K Team licences this file to you under the MIT license. +// The OS/K Team licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. #include "arch.h" @@ -20,9 +20,9 @@ ushort bget(ctx_t *ctx) } ushort c = ctx->mp[addr2real(ctx->r[RIP].val)]; - + ctx->r[RIP].val += 2; - + return c; } @@ -44,7 +44,7 @@ int main(int argc, char **argv) { ctx_t main_ctx; FILE *fwfile; - + main_ctx.r = arch_r; main_ctx.i = arch_i; @@ -52,22 +52,22 @@ int main(int argc, char **argv) log("Not enough arguments\n"); exit(-3); } - + fwprog = malloc(FWPROGSIZE); fwfile = fopen(argv[1], "rb"); - + if (!fwprog) { log("Couldn't allocate firmware buffer\n"); exit(-1); } - + if (!fwfile) { log("Couldn't open program file\n"); exit(-2); } - + fwsize = fread(fwprog, 1, FWPROGSIZE, fwfile); - + //log("Loaded %lu bytes if (fwsize < 2) { @@ -78,19 +78,19 @@ int main(int argc, char **argv) if (!(argc > 2 && !strcmp(argv[2], "-d"))) { main_ctx.mp = malloc(MEMSIZE + 16); main_ctx.mz = MEMSIZE; - + main_ctx.get = bget; main_ctx.disf = NULL; - + main_ctx.r[RIP].val = MEMOFF; - + if (main_ctx.mp == 0) { log("Couldn't allocate RAM\n"); exit(-1); } - - memcpy(&main_ctx.mp[addr2real(main_ctx.r[RIP].val)], fwprog, fwsize); - + + memcpy(&main_ctx.mp[addr2real(main_ctx.r[RIP].val)], fwprog, fwsize); + while (1) { decode(&main_ctx); } @@ -99,7 +99,7 @@ int main(int argc, char **argv) // Disassembly else { main_ctx.disf = fopen("fwprog.dis", "w"); - + if (main_ctx.disf == NULL) { log("Couldn't open fwprog.dis\n"); exit(-2); @@ -109,7 +109,7 @@ int main(int argc, char **argv) main_ctx.mz = 0; main_ctx.get = dget; - + while (1) { disasm(&main_ctx); } diff --git a/pc/mem.c b/pc/mem.c index d79b675..d79849a 100644 --- a/pc/mem.c +++ b/pc/mem.c @@ -47,7 +47,7 @@ ulong readmem8(ctx_t *ctx, ulong addr) { GETREAL(); CHK_RANGE(); - + if (addr % 2 == 0) return (ulong)ctx->mp[real] & 0xFF; else return ((ulong)ctx->mp[real] & 0xFF00) >> 8; } @@ -69,7 +69,7 @@ ulong readmem32(ctx_t *ctx, ulong addr) ulong val = ctx->mp[real++]; val = val | ((ulong)ctx->mp[real] << 16); - + return val; } @@ -83,7 +83,7 @@ ulong readmem64(ctx_t *ctx, ulong addr) val = val | ((ulong)ctx->mp[real++] << 16); val = val | ((ulong)ctx->mp[real++] << 32); val = val | ((ulong)ctx->mp[real] << 48); - + return val; } @@ -93,11 +93,11 @@ void writemem8(ctx_t *ctx, ulong val, ulong addr) CHK_RANGE(); ushort v = ctx->mp[real]; - + if (addr % 2) { ctx->mp[real] = (v & 0xFF00) | (val & 0xFF); } - + else { ctx->mp[real] = (v & 0xFF) | (((val & 0xFF) << 8)); } diff --git a/pc/regs.c b/pc/regs.c index 49d95fe..1f04de2 100644 --- a/pc/regs.c +++ b/pc/regs.c @@ -46,10 +46,10 @@ reg_t arch_r[NREGS] = // cr0: various flags { "cr0", "Control register 0", "Control", 0, CTL }, - + // cr1: code offset { "cr1", "Control register 1", "Control", 0, CTL }, - + // cr2: data offset { "cr2", "Control register 2", "Control", 0, CTL }, @@ -75,15 +75,15 @@ void dumpregs(ctx_t *ctx) { int i; reg_t *r; - + assert(ctx->r[INV].val == 0); - + log("\nRegisters:"); - + for (i = RAX; i < CR4; i++) { if ((i-1) % 4 == 0) log("\n"); - + r = &ctx->r[i]; log("%s%s=0x%-16lX ", r->name, (strlen(r->name) == 2 ? "=" : ""), r->val); diff --git a/pc/regs.h b/pc/regs.h index 7afa634..f96d93f 100644 --- a/pc/regs.h +++ b/pc/regs.h @@ -28,7 +28,7 @@ enum NX5, NX6, NX7, - + AX0, AX1, AX2, @@ -37,7 +37,7 @@ enum AX5, AX6, AX7, - + CR0, CR1, CR2, @@ -46,7 +46,7 @@ enum CR5, CR6, CR7, - + SA0, SA1, SA2, @@ -59,7 +59,7 @@ enum NREGS }; -enum +enum { GPR = 1 << 0, // General CTL = 1 << 1, // Control @@ -78,7 +78,7 @@ enum OV = 1 << 4, // Overflow flag DF = 1 << 5, // Direction flag SF = 1 << 6, // Sign flag - + UF = 1 << 16, // User-mode flag IF = 1 << 17, // Interrupts enable flag }; @@ -92,4 +92,3 @@ struct reg_t ulong flags; }; -