Compare commits

...

3 Commits

Author SHA1 Message Date
05793ad4ae all far call and ret 2025-03-31 23:37:50 +02:00
827c593712 all jmpf 2025-03-31 23:08:50 +02:00
804d3c5398 more instruction combinations 2025-03-31 22:56:51 +02:00
4 changed files with 258 additions and 1 deletions

141
asm/asm.c
View File

@ -1,4 +1,6 @@
#include "asm.h"
#include "common/arch.h"
#include "common/fmt_binary.h"
#include "common/op_str.h"
#include <stdbool.h>
#include <stddef.h>
@ -107,6 +109,58 @@ uint16_t assemble_to_binary(uint16_t* out, const Line* lines, size_t lines_size)
ADD_LABEL(op1);
break;
}
case LineTy_JmpFar_Reg_Reg: {
uint16_t op1 = line->op1.reg;
uint16_t op2 = line->op2.reg;
uint32_t ins = Op_Jmp;
ins |= 1 << 7;
add_op1_reg(&ins, op1);
add_op2_reg(&ins, op2);
out[ip++] = (uint16_t)ins;
break;
}
case LineTy_JmpFar_Reg_Imm: {
uint16_t op1 = line->op1.reg;
uint16_t op2 = line->op2.imm;
uint32_t ins = Op_Jmp;
ins |= 1 << 7;
ins |= 1 << 8;
add_op1_reg(&ins, op1);
out[ip++] = (uint16_t)ins;
out[ip++] = op2;
break;
}
case LineTy_JmpFar_Imm_Reg: {
uint16_t op1 = line->op1.imm;
uint16_t op2 = line->op2.reg;
uint32_t ins = Op_Jmp;
ins |= 1 << 7;
set_is_imm(&ins);
add_op2_reg(&ins, op2);
out[ip++] = (uint16_t)ins;
out[ip++] = op1;
break;
}
case LineTy_JmpFar_Imm_Imm: {
uint16_t op1 = line->op1.imm;
uint16_t op2 = line->op2.imm;
uint32_t ins = Op_Jmp;
ins |= 1 << 7;
ins |= 1 << 8;
set_is_imm(&ins);
out[ip++] = (uint16_t)ins;
out[ip++] = op2;
out[ip++] = op1;
break;
}
case LineTy_Jnz_Reg: {
uint16_t op1 = line->op1.reg;
uint16_t op2 = line->op2.reg;
@ -430,6 +484,17 @@ uint16_t assemble_to_binary(uint16_t* out, const Line* lines, size_t lines_size)
ADD_LABEL(dst);
break;
}
case LineTy_In_Reg: {
uint16_t dst = line->dst.reg;
uint16_t op1 = line->op1.reg;
uint32_t ins = Op_In;
add_op1_reg(&ins, op1);
add_dst_reg(&ins, dst);
out[ip++] = (uint16_t)ins;
break;
}
case LineTy_In_Imm: {
uint16_t dst = line->dst.reg;
uint16_t op1 = line->op1.imm;
@ -442,6 +507,15 @@ uint16_t assemble_to_binary(uint16_t* out, const Line* lines, size_t lines_size)
out[ip++] = op1;
break;
}
case LineTy_Call_Reg: {
uint16_t op1 = line->op1.reg;
uint32_t ins = Op_Call;
add_op1_reg(&ins, op1);
out[ip++] = (uint16_t)ins;
break;
}
case LineTy_Call_Imm: {
uint16_t op1 = line->op1.imm;
@ -462,11 +536,78 @@ uint16_t assemble_to_binary(uint16_t* out, const Line* lines, size_t lines_size)
ADD_LABEL(op1);
break;
}
case LineTy_CallFar_Reg_Reg: {
uint16_t op1 = line->op1.reg;
uint16_t op2 = line->op2.reg;
uint32_t ins = Op_Jmp;
ins |= 1 << 13;
add_op1_reg(&ins, op1);
ins |= (op2 << 7) & 0x7;
out[ip++] = (uint16_t)ins;
break;
}
case LineTy_CallFar_Reg_Imm: {
uint16_t op1 = line->op1.reg;
uint16_t op2 = line->op2.imm;
uint32_t ins = Op_Jmp;
ins |= 1 << 13;
ins |= 1 << 14;
add_op1_reg(&ins, op1);
out[ip++] = (uint16_t)ins;
out[ip++] = op2;
break;
}
case LineTy_CallFar_Imm_Reg: {
uint16_t op1 = line->op1.imm;
uint16_t op2 = line->op2.reg;
uint32_t ins = Op_Jmp;
ins |= 1 << 13;
set_is_imm(&ins);
ins |= (op2 << 7) & 0x7;
out[ip++] = (uint16_t)ins;
out[ip++] = op1;
break;
}
case LineTy_CallFar_Imm_Imm: {
uint16_t op1 = line->op1.imm;
uint16_t op2 = line->op2.imm;
uint32_t ins = Op_Jmp;
ins |= 1 << 13;
ins |= 1 << 14;
set_is_imm(&ins);
out[ip++] = (uint16_t)ins;
out[ip++] = op2;
out[ip++] = op1;
break;
}
case LineTy_Ret: {
uint32_t ins = Op_Ret;
out[ip++] = (uint16_t)ins;
break;
}
case LineTy_RetFar: {
uint32_t ins = Op_Ret;
ins |= 1 << 6;
out[ip++] = (uint16_t)ins;
break;
}
case LineTy_Lit_Reg: {
uint16_t op1 = line->op1.reg;
uint32_t ins = Op_Lit;
add_op1_reg(&ins, op1);
out[ip++] = (uint16_t)ins;
break;
}
case LineTy_Lit_Imm: {
uint16_t op1 = line->op1.imm;

View File

@ -13,6 +13,10 @@ typedef enum {
LineTy_Jmp_Reg,
LineTy_Jmp_Imm,
LineTy_Jmp_Label,
LineTy_JmpFar_Reg_Reg,
LineTy_JmpFar_Reg_Imm,
LineTy_JmpFar_Imm_Reg,
LineTy_JmpFar_Imm_Imm,
LineTy_Jnz_Reg,
LineTy_Jnz_Imm,
LineTy_Jnz_Label,
@ -38,10 +42,18 @@ typedef enum {
LineTy_Mov16_MemImm_Reg,
LineTy_Mov16_MemImm_Imm,
LineTy_Mov16_MemLabel_Reg,
LineTy_In_Reg,
LineTy_In_Imm,
LineTy_Call_Reg,
LineTy_Call_Imm,
LineTy_Call_Label,
LineTy_CallFar_Reg_Reg,
LineTy_CallFar_Reg_Imm,
LineTy_CallFar_Imm_Reg,
LineTy_CallFar_Imm_Imm,
LineTy_Ret,
LineTy_RetFar,
LineTy_Lit_Reg,
LineTy_Lit_Imm,
LineTy_Lit_Label,
LineTy_Int,
@ -113,6 +125,10 @@ Line s_hlt(void);
Line s_jmp_r(Reg op1_reg);
Line s_jmp_i(uint16_t op1_imm);
Line s_jmp_l(int op1_label);
Line s_jmpf_r_r(Reg op1_reg, Reg op2_reg);
Line s_jmpf_r_i(Reg op1_reg, uint16_t op2_imm);
Line s_jmpf_i_r(uint16_t op1_imm, Reg op2_reg);
Line s_jmpf_i_i(uint16_t op1_imm, uint16_t op2_imm);
Line s_jnz_r(Reg op1_reg, Reg op2_reg);
Line s_jnz_i(Reg op1_reg, uint16_t op2_imm);
Line s_jnz_l(Reg op1_reg, int op2_label);
@ -138,10 +154,18 @@ Line s_mov16_mr_i(Reg dst_reg, uint16_t dst_offset, uint16_t op2_imm);
Line s_mov16_mi_r(uint16_t dst_imm, Reg op2_reg);
Line s_mov16_mi_i(uint16_t dst_imm, uint16_t op2_imm);
Line s_mov16_ml_r(int dst_label, Reg op2_reg);
Line s_in_r(Reg dst_reg, Reg op1_reg);
Line s_in_i(Reg dst_reg, uint16_t op1_imm);
Line s_call_r(Reg op1_reg);
Line s_call_i(uint16_t op1_imm);
Line s_call_l(int op1_label);
Line s_callf_r_r(Reg op1_reg, Reg op2_reg);
Line s_callf_r_i(Reg op1_reg, uint16_t op2_imm);
Line s_callf_i_r(uint16_t op1_imm, Reg op2_reg);
Line s_callf_i_i(uint16_t op1_imm, uint16_t op2_imm);
Line s_ret(void);
Line s_retf(void);
Line s_lit_r(Reg op1_reg);
Line s_lit_i(uint16_t op1_imm);
Line s_lit_l(int op1_label);
Line s_int(uint8_t int_id);

View File

@ -53,10 +53,43 @@ Line s_jmp_l(int op1_label)
.op1 = (Ex) { .label = op1_label },
};
}
Line s_jmpf_r_r(Reg op1_reg, Reg op2_reg)
{
return (Line) {
.ty = LineTy_JmpFar_Reg_Reg,
.op1 = (Ex) { .reg = (uint16_t)op1_reg },
.op2 = (Ex) { .reg = (uint16_t)op2_reg },
};
}
Line s_jmpf_r_i(Reg op1_reg, uint16_t op2_imm)
{
return (Line) {
.ty = LineTy_JmpFar_Reg_Imm,
.op1 = (Ex) { .reg = (uint16_t)op1_reg },
.op2 = (Ex) { .imm = op2_imm },
};
}
Line s_jmpf_i_r(uint16_t op1_imm, Reg op2_reg)
{
return (Line) {
.ty = LineTy_JmpFar_Imm_Reg,
.op1 = (Ex) { .imm = op1_imm },
.op2 = (Ex) { .reg = (uint16_t)op2_reg },
};
}
Line s_jmpf_i_i(uint16_t op1_imm, uint16_t op2_imm)
{
return (Line) {
.ty = LineTy_JmpFar_Imm_Imm,
.op1 = (Ex) { .imm = op1_imm },
.op2 = (Ex) { .imm = op2_imm },
};
}
Line s_jnz_r(Reg op1_reg, Reg op2_reg)
{
return (Line) {
.ty = LineTy_Jnz_Reg,
.op1 = (Ex) { .reg = (uint16_t)op1_reg },
.op2 = (Ex) { .reg = (uint16_t)op2_reg },
};
}
@ -64,6 +97,7 @@ Line s_jnz_i(Reg op1_reg, uint16_t op2_imm)
{
return (Line) {
.ty = LineTy_Jnz_Imm,
.op1 = (Ex) { .reg = (uint16_t)op1_reg },
.op2 = (Ex) { .imm = op2_imm },
};
}
@ -254,6 +288,14 @@ Line s_mov16_ml_r(int dst_label, Reg op2_reg)
.op2 = (Ex) { .reg = (uint16_t)op2_reg },
};
}
Line s_in_r(Reg dst_reg, Reg op1_reg)
{
return (Line) {
.ty = LineTy_In_Reg,
.dst = (Ex) { .reg = (uint16_t)dst_reg },
.op1 = (Ex) { .reg = (uint16_t)op1_reg },
};
}
Line s_in_i(Reg dst_reg, uint16_t op1_imm)
{
return (Line) {
@ -262,6 +304,13 @@ Line s_in_i(Reg dst_reg, uint16_t op1_imm)
.op1 = (Ex) { .imm = op1_imm },
};
}
Line s_call_r(Reg op1_reg)
{
return (Line) {
.ty = LineTy_Call_Reg,
.op1 = (Ex) { .reg = (uint16_t)op1_reg },
};
}
Line s_call_i(uint16_t op1_imm)
{
return (Line) {
@ -276,10 +325,53 @@ Line s_call_l(int op1_label)
.op1 = (Ex) { .label = op1_label },
};
}
Line s_callf_r_r(Reg op1_reg, Reg op2_reg)
{
return (Line) {
.ty = LineTy_CallFar_Reg_Reg,
.op1 = (Ex) { .reg = (uint16_t)op1_reg },
.op2 = (Ex) { .reg = (uint16_t)op2_reg },
};
}
Line s_callf_r_i(Reg op1_reg, uint16_t op2_imm)
{
return (Line) {
.ty = LineTy_CallFar_Reg_Imm,
.op1 = (Ex) { .reg = (uint16_t)op1_reg },
.op2 = (Ex) { .imm = op2_imm },
};
}
Line s_callf_i_r(uint16_t op1_imm, Reg op2_reg)
{
return (Line) {
.ty = LineTy_CallFar_Imm_Reg,
.op1 = (Ex) { .imm = op1_imm },
.op2 = (Ex) { .reg = (uint16_t)op2_reg },
};
}
Line s_callf_i_i(uint16_t op1_imm, uint16_t op2_imm)
{
return (Line) {
.ty = LineTy_CallFar_Imm_Imm,
.op1 = (Ex) { .imm = op1_imm },
.op2 = (Ex) { .imm = op2_imm },
};
}
Line s_ret(void)
{
return (Line) { .ty = LineTy_Ret };
}
Line s_retf(void)
{
return (Line) { .ty = LineTy_RetFar };
}
Line s_lit_r(Reg op1_reg)
{
return (Line) {
.ty = LineTy_Lit_Imm,
.op1 = (Ex) { .reg = (uint16_t)op1_reg },
};
}
Line s_lit_i(uint16_t op1_imm)
{
return (Line) {

View File

@ -1,4 +1,4 @@
#include "asm/asm.h"
#include "common/fmt_binary.h"
#include "common/op_str.h"
#include "common/video_character_display.h"
#include "vm.h"