#Virtual machine code review

40 messages · Page 1 of 1 (latest)

gloomy heron
#

i am following the little endian format

vernal otter
#
#[repr(u8)]
pub enum OPCode{
    Nop=0x0001,
    LoadImm=0x0002,
}

that is a bit silly

#

1 byte is 2 hexadecimal digits

#

not 4

gloomy heron
#

i see

slender sierra
#
_=>Err(format!("uknown op code 0x{0:X}",ins))
``` this arm is unreachable (without going through undefined behavior)
gloomy heron
#

fixed

#

i need a little help regarding the immedeate value

vernal otter
gloomy heron
#

like my instruction is load Ro , 15 so 8 bits are taken by op code and rest 4,4 by imm value and reg but i need to store more than 4 bit imm values

vernal otter
#

you should use the little endian utilities from std

gloomy heron
vernal otter
#

instead of doing your own stuff with bit masks

gloomy heron
#

i see

vernal otter
gloomy heron
#

any healp for this

#

like my instruction is load Ro , 15 so 8 bits are taken by op code and rest 4,4 by imm value and reg but i need to store more than 4 bit imm values

vernal otter
#

also you really should not use as except maybe for enum casts, and even then probably not

#

you code is a great example of why as is bad

#

you freely mix truncating and non truncating casting with no sign of which is which

vernal otter
#

if you never add any more values

#

otherwise you'll just need to use more bytes

gloomy heron
#

also what should i do insted

vernal otter
#
 let upper_bits = (byte & 0xff00) as u8;
#

you got this

#

that's litterally always 0

gloomy heron
#

yeah i fixed that

vernal otter
#

you should use from where it is available

gloomy heron
#
   let upper_bits = ((byte >>8) &0xff) as u8;
vernal otter
#

and other methods where possible

vernal otter
#

also calling byte a u16 is weird, but i shouldn't judge naming

gloomy heron
#

actually i am very confused about writing vm like how to impl these ive already implemeted chip -8 it was easy cause it had its docs and specification specified

#

so i was able to think clearly

wide crown
#

is this a VM for a specific existing system, or are you designing your own ISA and system?