Skip to content

Commit

Permalink
Start implementing vm_op_and, vm_op_live, and vm_op_zjmp
Browse files Browse the repository at this point in the history
  • Loading branch information
rizky committed Apr 25, 2018
1 parent 41f2ef4 commit 2e7f1da
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
10 changes: 5 additions & 5 deletions include/ft_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: rnugroho <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/19 21:39:11 by rnugroho #+# #+# */
/* Updated: 2018/04/25 01:38:11 by rnugroho ### ########.fr */
/* Updated: 2018/04/25 03:14:13 by rnugroho ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -111,7 +111,7 @@ void vm_op_print(t_process p);
void vm_sti_print(t_process p);
void vm_and_print(t_process p);
void vm_live_print(t_process p);
void vm_zjump_print(t_process p);
void vm_zjmp_print(t_process p);

int vm_valid_arg(char *arg);
int vm_valid_verbosity_lvl(int lvl);
Expand All @@ -137,7 +137,7 @@ static t_op_dict g_op_dict[17] = {
{ .name = "\0", .opcode = 0x00, .d_size = 0, .param_c = 0, .is_oc = 0,
{0, 0, 0}, &vm_op_inc, &vm_op_print, .is_car = 0, .cycles = 0},
{ .name = "live", .opcode = 0x01, .d_size = 4, .param_c = 1, .is_oc = 0,
{T_DIR, 0, 0}, &vm_op_inc, &vm_live_print, .is_car = 0, .cycles = 10},
{T_DIR, 0, 0}, &vm_op_live, &vm_live_print, .is_car = 0, .cycles = 10},
{ .name = "ld", .opcode = 0x02, .d_size = 4, .param_c = 2, .is_oc = 1,
{T_DIR | T_IND, T_REG, 0},
&vm_op_inc, &vm_op_print, .is_car = 1, .cycles = 5},
Expand All @@ -152,7 +152,7 @@ static t_op_dict g_op_dict[17] = {
&vm_op_inc, &vm_op_print, .is_car = 1, .cycles = 10},
{ .name = "and", .opcode = 0x06, .d_size = 4, .param_c = 3, .is_oc = 1,
{T_REG | T_IND | T_DIR, T_REG | T_IND | T_DIR, T_REG},
&vm_op_inc, &vm_and_print, .is_car = 1, .cycles = 6},
&vm_op_and, &vm_and_print, .is_car = 1, .cycles = 6},
{ .name = "or", .opcode = 0x07, .d_size = 4, .param_c = 3, .is_oc = 1,
{T_REG | T_IND | T_DIR, T_REG | T_IND | T_DIR, T_REG},
&vm_op_inc, &vm_op_print, .is_car = 1, .cycles = 6},
Expand All @@ -161,7 +161,7 @@ static t_op_dict g_op_dict[17] = {
&vm_op_inc, &vm_op_print, .is_car = 1, .cycles = 6},
{ .name = "zjmp", .opcode = 0x09, .d_size = 2, .param_c = 1, .is_oc = 0,
{T_DIR, 0, 0},
&vm_op_inc, &vm_zjump_print, .is_car = 0, .cycles = 20},
&vm_op_zjmp, &vm_zjmp_print, .is_car = 0, .cycles = 20},
{ .name = "ldi", .opcode = 0x0a, .d_size = 2, .param_c = 3, .is_oc = 1,
{T_REG | T_DIR | T_IND, T_DIR | T_REG, T_REG},
&vm_op_inc, &vm_op_print, .is_car = 0, .cycles = 25},
Expand Down
35 changes: 34 additions & 1 deletion src/ft_vm/vm_op.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: rnugroho <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/23 19:23:14 by rnugroho #+# #+# */
/* Updated: 2018/04/25 01:37:22 by rnugroho ### ########.fr */
/* Updated: 2018/04/25 03:14:13 by rnugroho ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -62,3 +62,36 @@ void
free(temp);
vm_op_inc(vm, p);
}

void
vm_op_and(t_vm *vm, t_process *p)
{
int param0;
int param1;

(void)vm;
param0 = (p->op.params[0].type == REG_CODE) ?
g_reg[p->op.params[0].value] : p->op.params[1].value;
param1 = (p->op.params[1].type == REG_CODE) ?
g_reg[p->op.params[1].value] : p->op.params[2].value;
g_reg[p->op.params[2].value] = param0 & param1;
if (g_reg[p->op.params[2].value] == 0)
g_carrier = 1;
vm_op_inc(vm, p);
}

void
vm_op_live(t_vm *vm, t_process *p)
{
(void)vm;
p->live_nbr++;
vm_op_inc(vm, p);
}

void
vm_op_zjmp(t_vm *vm, t_process *p)
{
(void)vm;
if (g_carrier)
p->pc = p->op.params[0].value;
}
4 changes: 2 additions & 2 deletions src/ft_vm/vm_op_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: rnugroho <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/24 13:51:02 by rnugroho #+# #+# */
/* Updated: 2018/04/24 19:36:00 by rnugroho ### ########.fr */
/* Updated: 2018/04/25 03:14:13 by rnugroho ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -56,7 +56,7 @@ void
}

void
vm_zjump_print(t_process p)
vm_zjmp_print(t_process p)
{
int value;

Expand Down
5 changes: 4 additions & 1 deletion tests/asm/valid/ex.s
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
.name "zork"
.comment "just a basic living prog"

l2: sti r1,r1,%0
l2: sti r1,%:live,%0
and r1,%0,r1
live: live %1
zjmp %:live

0 comments on commit 2e7f1da

Please sign in to comment.