Skip to content

Commit

Permalink
Move VM methods to Thread
Browse files Browse the repository at this point in the history
  • Loading branch information
maximecb committed Aug 29, 2024
1 parent a212441 commit 4a3ed23
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 86 deletions.
14 changes: 9 additions & 5 deletions vm/src/sys/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use sdl2::pixels::PixelFormatEnum;
use std::time::Duration;

use crate::sys::{get_sdl_context};
use crate::vm::{VM, Value};
use crate::vm::{VM, Thread, Value};

/// SDL video subsystem
/// This is a global variable because it doesn't implement
Expand Down Expand Up @@ -63,7 +63,7 @@ fn get_window(window_id: u32) -> &'static mut Window<'static>
}
}

pub fn window_create(vm: &mut VM, width: Value, height: Value, title: Value, flags: Value) -> Value
pub fn window_create(thread: &mut Thread, width: Value, height: Value, title: Value, flags: Value) -> Value
{
unsafe {
if WINDOW.is_some() {
Expand All @@ -73,7 +73,7 @@ pub fn window_create(vm: &mut VM, width: Value, height: Value, title: Value, fla

let width: u32 = width.as_usize().try_into().unwrap();
let height: u32 = height.as_usize().try_into().unwrap();
let title_str = vm.get_heap_str(title.as_usize()).to_owned();
let title_str = thread.get_heap_str(title.as_usize()).to_owned();

let video_subsystem = get_video_subsystem();

Expand Down Expand Up @@ -108,13 +108,17 @@ pub fn window_create(vm: &mut VM, width: Value, height: Value, title: Value, fla
Value::from(0)
}

pub fn window_draw_frame(vm: &mut VM, window_id: Value, src_addr: Value)
pub fn window_draw_frame(thread: &mut Thread, window_id: Value, src_addr: Value)
{

// TODO: test thread id


let window = get_window(window_id.as_u32());

// Get the address to copy pixel data from
let data_len = (4 * window.width * window.height) as usize;
let data_ptr = vm.get_heap_ptr(src_addr.as_usize(), data_len);
let data_ptr = thread.get_heap_ptr(src_addr.as_usize(), data_len);

// If no frame has been drawn yet
if window.texture.is_none() {
Expand Down
159 changes: 78 additions & 81 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,79 +532,6 @@ impl Thread



}







pub struct VM
{
// Heap memory space
heap: MemBlock,

// Code memory space
code: MemBlock,

// Value stack
stack: Vec<Value>,

// List of stack frames (activation records)
frames: Vec<StackFrame>,




// Next thread id to assign
next_tid: u64,

// Map from actor ids to thread join handles
threads: HashMap<u64, thread::JoinHandle<Value>>,

// Reference to self
// Needed to instantiate actors
vm: Option<Arc<Mutex<VM>>>,
}

// Needed to send Arc<Mutex<VM>> to thread
unsafe impl Send for VM {}

impl VM
{
pub fn new(mut code: MemBlock, mut heap: MemBlock, syscalls: HashSet<u16>) -> Arc<Mutex<VM>>
{
// Resize the code and heap space to a page size multiple
code.resize(code.len());
heap.resize(heap.len());

let vm = Self {
code,
heap,
stack: Vec::default(),
frames: Vec::default(),


next_tid: 0,
threads: HashMap::default(),
vm: None,
};

let vm = Arc::new(Mutex::new(vm));

// Store a reference to the mutex on the VM
// This is so we can pass this reference to threads
vm.lock().unwrap().vm = Some(vm.clone());

vm
}

#[cfg(feature = "count_insns")]
pub fn get_insn_count(&self) -> u64
{
self.insn_count
}

pub fn stack_size(&self) -> usize
{
Expand All @@ -627,20 +554,17 @@ impl VM
/// Get the current size of the heap in bytes
pub fn heap_size(&self) -> usize
{
self.heap.len()
}
todo!();

/// Resize the heap to a new size in bytes
pub fn resize_heap(&mut self, num_bytes: usize) -> usize
{
self.heap.resize(num_bytes)
//self.heap.len()
}

// FIXME: this function should be marked unsafe
//
/// Get a pointer to an address/offset in the heap
pub fn get_heap_ptr<T>(&mut self, addr: usize, num_elems: usize) -> *mut T
{
todo!();

/*
if addr + std::mem::size_of::<T>() * num_elems > self.heap.len() {
panic!("attempting to access memory slice past end of heap");
}
Expand All @@ -656,11 +580,15 @@ impl VM
let heap_ptr: *mut u8 = self.heap.data.as_mut_ptr().add(addr);
transmute::<*mut u8 , *mut T>(heap_ptr)
}
*/
}

/// Get a mutable slice to access a memory region in the heap
pub fn get_heap_slice<T>(&mut self, addr: usize, num_elems: usize) -> &mut [T]
{
todo!();

/*
if addr + std::mem::size_of::<T>() * num_elems > self.heap.len() {
panic!("attempting to access memory slice past end of heap");
}
Expand All @@ -677,11 +605,15 @@ impl VM
let start_ptr = transmute::<*mut u8 , *mut T>(heap_ptr);
std::slice::from_raw_parts_mut(start_ptr, num_elems)
}
*/
}

/// Copy an UTF-8 string at a given address in the heap
pub fn get_heap_str(&mut self, str_ptr: usize) -> &str
{
todo!();

/*
// Verify that there is a null-terminator for this string
// within the bounds of the heap
let mut str_len = 0;
Expand All @@ -704,6 +636,7 @@ impl VM
let c_str = unsafe { CStr::from_ptr(char_ptr as *const i8) };
let rust_str = c_str.to_str().unwrap();
rust_str
*/
}

/// Call a function at a given address
Expand All @@ -728,6 +661,7 @@ impl VM
let mut bp = self.stack.len();
let mut pc = callee_pc as usize;

/*
// For each instruction to execute
loop
{
Expand Down Expand Up @@ -1615,13 +1549,76 @@ impl VM
_ => panic!("unknown opcode {:?}", op),
}
}
*/

todo!();
}



}







pub struct VM
{
// Heap memory space
heap: MemBlock,

// Code memory space
code: MemBlock,

// Next thread id to assign
next_tid: u64,

// Map from actor ids to thread join handles
threads: HashMap<u64, thread::JoinHandle<Value>>,

// Reference to self
// Needed to instantiate actors
vm: Option<Arc<Mutex<VM>>>,
}

// Needed to send Arc<Mutex<VM>> to thread
unsafe impl Send for VM {}

impl VM
{
pub fn new(mut code: MemBlock, mut heap: MemBlock, syscalls: HashSet<u16>) -> Arc<Mutex<VM>>
{
// Resize the code and heap space to a page size multiple
code.resize(code.len());
heap.resize(heap.len());

let vm = Self {
code,
heap,
next_tid: 0,
threads: HashMap::default(),
vm: None,
};

let vm = Arc::new(Mutex::new(vm));

// Store a reference to the mutex on the VM
// This is so we can pass this reference to threads
vm.lock().unwrap().vm = Some(vm.clone());

vm
}

/*
/// Resize the heap to a new size in bytes
pub fn resize_heap(&mut self, num_bytes: usize) -> usize
{
self.heap.resize(num_bytes)
}
*/

// Create a new thread
pub fn new_thread(vm: &Arc<Mutex<VM>>, fun: Value, args: Vec<Value>) -> u64
{
Expand Down

0 comments on commit 4a3ed23

Please sign in to comment.