Add other instructions

This commit is contained in:
shylie 2025-03-07 09:04:26 -05:00
parent d58425bf75
commit e15a2e2aaf
4 changed files with 52 additions and 26 deletions

View File

@ -1,10 +1,10 @@
;#include "../src/foot.asm" ;#include "../src/foot.asm"
label:
CNST. r0, 0xFBFF CNST. r0, 0xFBFF
CNST. r29, 0x400 CNST. r29, 0x400
label: ADDI.r [r0++], [r0], r0
CNST.r [r0++], 0b1100110011001100
CNST. r31, label CNST. r31, label

View File

@ -2,7 +2,7 @@
#subruledef operand #subruledef operand
{ {
#{immediate: s5} => 0b000 @ immediate #{immediate: u5} => 0b000 @ immediate
r{register: u5} => 0b001 @ register r{register: u5} => 0b001 @ register
[r{register: u5}++] => 0b010 @ register [r{register: u5}++] => 0b010 @ register
[r{register: u5}--] => 0b011 @ register [r{register: u5}--] => 0b011 @ register
@ -31,6 +31,22 @@
#ruledef #ruledef
{ {
CNST{flags: crflags} {dst: operand}, {imm: i16} => imm @ flags @ 0b0000 @ dst CNST{flags: crflags} {dst: operand}, {imm: i16} => imm @ flags @ 0b0000 @ dst
CMPR{flags: crflags} {dst: operand}, {a: operand} => 0x00 @ a @ flags @ 0b0001 @ dst
BWNG{flags: crflags} {dst: operand}, {a: operand} => 0x01 @ a @ flags @ 0b0001 @ dst
ARNG{flags: crflags} {dst: operand}, {a: operand} => 0x02 @ a @ flags @ 0b0001 @ dst
LONG{flags: crflags} {dst: operand}, {a: operand} => 0x03 @ a @ flags @ 0b0001 @ dst
BWOR{flags: crflags} {dst: operand}, {a: operand}, {b: operand} => b @ a @ flags @ 0b0010 @ dst
BAND{flags: crflags} {dst: operand}, {a: operand}, {b: operand} => b @ a @ flags @ 0b0011 @ dst
BXOR{flags: crflags} {dst: operand}, {a: operand}, {b: operand} => b @ a @ flags @ 0b0100 @ dst
URSH{flags: crflags} {dst: operand}, {a: operand}, {b: operand} => b @ a @ flags @ 0b0101 @ dst
SRSH{flags: crflags} {dst: operand}, {a: operand}, {b: operand} => b @ a @ flags @ 0b0110 @ dst
ZLSH{flags: crflags} {dst: operand}, {a: operand}, {b: operand} => b @ a @ flags @ 0b0111 @ dst
CLSH{flags: crflags} {dst: operand}, {a: operand}, {b: operand} => b @ a @ flags @ 0b1000 @ dst
ADDI{flags: crflags} {dst: operand}, {a: operand}, {b: operand} => b @ a @ flags @ 0b1001 @ dst
SUBT{flags: crflags} {dst: operand}, {a: operand}, {b: operand} => b @ a @ flags @ 0b1010 @ dst
MULT{flags: crflags} {dst: operand}, {a: operand}, {b: operand} => b @ a @ flags @ 0b1011 @ dst
DIVI{flags: crflags} {dst: operand}, {a: operand}, {b: operand} => b @ a @ flags @ 0b1100 @ dst
MODU{flags: crflags} {dst: operand}, {a: operand}, {b: operand} => b @ a @ flags @ 0b1101 @ dst
} }
#bankdef mem #bankdef mem

View File

@ -1,5 +1,3 @@
#![windows_subsystem = "windows"]
mod vm; mod vm;
use std::sync::Arc; use std::sync::Arc;
@ -9,9 +7,9 @@ use pixels::{Pixels, SurfaceTexture};
use winit::application::ApplicationHandler; use winit::application::ApplicationHandler;
use winit::dpi::LogicalSize; use winit::dpi::LogicalSize;
use winit::event::WindowEvent; use winit::event::{StartCause, WindowEvent};
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop}; use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
use winit::window::{Window, WindowAttributes, WindowId}; use winit::window::{Window, WindowAttributes, WindowButtons, WindowId};
pub struct App pub struct App
{ {
@ -32,6 +30,11 @@ impl App
} }
} }
fn update(&mut self)
{
self.vm.cycle();
}
fn draw(&mut self) fn draw(&mut self)
{ {
let pixels = &mut self.pixels.as_mut().unwrap(); let pixels = &mut self.pixels.as_mut().unwrap();
@ -64,6 +67,19 @@ impl App
impl ApplicationHandler for App impl ApplicationHandler for App
{ {
fn new_events(&mut self, _: &ActiveEventLoop, cause: StartCause)
{
match cause
{
StartCause::Poll =>
{
self.update();
self.draw()
},
_ => {}
}
}
fn resumed(&mut self, event_loop: &ActiveEventLoop) fn resumed(&mut self, event_loop: &ActiveEventLoop)
{ {
const WIDTH: u32 = 128; const WIDTH: u32 = 128;
@ -72,6 +88,7 @@ impl ApplicationHandler for App
let window = event_loop.create_window( let window = event_loop.create_window(
WindowAttributes::default() WindowAttributes::default()
.with_resizable(false) .with_resizable(false)
.with_enabled_buttons(WindowButtons::CLOSE | WindowButtons::MINIMIZE)
.with_inner_size(LogicalSize::new(WIDTH * 4, HEIGHT * 4)) .with_inner_size(LogicalSize::new(WIDTH * 4, HEIGHT * 4))
.with_title("foot vm") .with_title("foot vm")
).unwrap(); ).unwrap();
@ -85,8 +102,6 @@ impl ApplicationHandler for App
fn window_event(&mut self, event_loop: &ActiveEventLoop, _: WindowId, event: WindowEvent) fn window_event(&mut self, event_loop: &ActiveEventLoop, _: WindowId, event: WindowEvent)
{ {
self.vm.cycle();
match event match event
{ {
WindowEvent::RedrawRequested => self.draw(), WindowEvent::RedrawRequested => self.draw(),

View File

@ -144,7 +144,7 @@ impl TryFrom<u32> for Instruction
{ {
let a: Operand = (value & 0xFF).try_into()?; let a: Operand = (value & 0xFF).try_into()?;
let b: Operand = ((value >> 8) & 0xFF).try_into()?; let b: Operand = ((value >> 8) & 0xFF).try_into()?;
match value >> 24 match (value >> 24) & 0xF
{ {
2 => Ok(BWOR(ceflag, rflag, dst, b, a)), 2 => Ok(BWOR(ceflag, rflag, dst, b, a)),
3 => Ok(BAND(ceflag, rflag, dst, b, a)), 3 => Ok(BAND(ceflag, rflag, dst, b, a)),
@ -179,7 +179,7 @@ const MEMORY_SIZE: usize = 1 << 16;
pub struct FootVM pub struct FootVM
{ {
memory: [u16; MEMORY_SIZE], memory: [u16; MEMORY_SIZE],
registers: [u16; 32], pub registers: [u16; 32],
status: Status status: Status
} }
@ -294,7 +294,9 @@ impl FootVM
{ {
let first_word = self[self.program_counter()]; let first_word = self[self.program_counter()];
let second_word = self[self.program_counter() + 1]; let second_word = self[self.program_counter() + 1];
match ((second_word as u32) << 16 | first_word as u32).try_into() let full_instruction_bits = (second_word as u32) << 16 | first_word as u32;
*self.mut_program_counter() = self.program_counter().wrapping_add(2);
match full_instruction_bits.try_into()
{ {
Ok(CNST(cef, rep, dst, imm)) => Ok(CNST(cef, rep, dst, imm)) =>
run_instruction!(self, cef, rep, cnst, dst, imm), run_instruction!(self, cef, rep, cnst, dst, imm),
@ -330,10 +332,8 @@ impl FootVM
run_instruction!(self, cef, rep, divi, dst, a, b), run_instruction!(self, cef, rep, divi, dst, a, b),
Ok(MODU(cef, rep, dst, b, a)) => Ok(MODU(cef, rep, dst, b, a)) =>
run_instruction!(self, cef, rep, modu, dst, a, b), run_instruction!(self, cef, rep, modu, dst, a, b),
Err(_) => () Err(_) => {}
} }
*self.mut_program_counter() += 2;
} }
fn cnst(&mut self, dst: Operand, imm: u16) fn cnst(&mut self, dst: Operand, imm: u16)
@ -429,35 +429,35 @@ impl FootVM
{ {
let a = self.load_operand(src_a); let a = self.load_operand(src_a);
let b = self.load_operand(src_b); let b = self.load_operand(src_b);
self.store_operand(dst, ((a as i16) + (b as i16)) as u16); self.store_operand(dst, a.wrapping_add(b));
} }
fn subt(&mut self, dst: Operand, src_a: Operand, src_b: Operand) fn subt(&mut self, dst: Operand, src_a: Operand, src_b: Operand)
{ {
let a = self.load_operand(src_a); let a = self.load_operand(src_a);
let b = self.load_operand(src_b); let b = self.load_operand(src_b);
self.store_operand(dst, ((a as i16) - (b as i16)) as u16); self.store_operand(dst, a.wrapping_sub(b));
} }
fn mult(&mut self, dst: Operand, src_a: Operand, src_b: Operand) fn mult(&mut self, dst: Operand, src_a: Operand, src_b: Operand)
{ {
let a = self.load_operand(src_a); let a = self.load_operand(src_a);
let b = self.load_operand(src_b); let b = self.load_operand(src_b);
self.store_operand(dst, ((a as i16) * (b as i16)) as u16); self.store_operand(dst, a.wrapping_mul(b));
} }
fn divi(&mut self, dst: Operand, src_a: Operand, src_b: Operand) fn divi(&mut self, dst: Operand, src_a: Operand, src_b: Operand)
{ {
let a = self.load_operand(src_a); let a = self.load_operand(src_a);
let b = self.load_operand(src_b); let b = self.load_operand(src_b);
self.store_operand(dst, ((a as i16) / (b as i16)) as u16); self.store_operand(dst, a.wrapping_div(b));
} }
fn modu(&mut self, dst: Operand, src_a: Operand, src_b: Operand) fn modu(&mut self, dst: Operand, src_a: Operand, src_b: Operand)
{ {
let a = self.load_operand(src_a); let a = self.load_operand(src_a);
let b = self.load_operand(src_b); let b = self.load_operand(src_b);
self.store_operand(dst, ((a as i16) % (b as i16)) as u16); self.store_operand(dst, a.wrapping_rem(b));
} }
fn store_operand(&mut self, dst: Operand, value: u16) fn store_operand(&mut self, dst: Operand, value: u16)
@ -520,11 +520,6 @@ impl FootVM
&mut self.registers[31] &mut self.registers[31]
} }
fn stack_pointer(&self) -> u16
{
self.registers[30]
}
fn loop_count(&self) -> u16 fn loop_count(&self) -> u16
{ {
self.registers[29] self.registers[29]