[cpu] Instruction parsing
This commit is contained in:
@@ -1,6 +1,52 @@
|
|||||||
|
use byteorder::{BigEndian, ByteOrder};
|
||||||
|
use crate::device::cpu::Instruction::{ClearScreen, JumpTo};
|
||||||
|
|
||||||
|
#[derive(Eq, PartialEq, Debug)]
|
||||||
enum Instruction{
|
enum Instruction{
|
||||||
ClearScreen,
|
ClearScreen,
|
||||||
|
/// Jump to location
|
||||||
JumpTo(u16),
|
JumpTo(u16),
|
||||||
|
SetRegister(usize,u8),
|
||||||
|
AddValueToRegister(usize,u8),
|
||||||
|
SetIndex(u16),
|
||||||
|
///
|
||||||
|
DRAW(usize,usize,u8)
|
||||||
|
}
|
||||||
|
impl Instruction{
|
||||||
|
pub fn parse_fetched_instruction(location: &[u8])->Instruction{
|
||||||
|
assert_eq!(location.len(),2);
|
||||||
|
let instruction = BigEndian::read_u16(location);
|
||||||
|
|
||||||
|
if instruction == 0xe0{
|
||||||
|
return ClearScreen;
|
||||||
|
} else if (instruction & 0x1000)==0x1000 {
|
||||||
|
return JumpTo(instruction & 0xfff);
|
||||||
|
}
|
||||||
|
todo!();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests{
|
||||||
|
use crate::device::cpu::Instruction;
|
||||||
|
use crate::device::cpu::Instruction::{ClearScreen, JumpTo};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_clear_screen(){
|
||||||
|
let instruction_bytes = 0x00e0_u16.to_be_bytes();
|
||||||
|
let ins = Instruction::parse_fetched_instruction(&instruction_bytes);
|
||||||
|
assert_eq!(ins,ClearScreen);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn test_jump_to_instruction_1(){
|
||||||
|
let instruction_bytes = 0x1123_u16.to_be_bytes();
|
||||||
|
let ins = Instruction::parse_fetched_instruction(&instruction_bytes);
|
||||||
|
assert_eq!(ins,JumpTo(0x123));
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn test_jump_to_instruction_2(){
|
||||||
|
let instruction_bytes = 0x1faf_u16.to_be_bytes();
|
||||||
|
let ins = Instruction::parse_fetched_instruction(&instruction_bytes);
|
||||||
|
assert_eq!(ins,JumpTo(0xfaf));
|
||||||
|
}
|
||||||
}
|
}
|
@@ -47,13 +47,20 @@ impl Device{
|
|||||||
];
|
];
|
||||||
const FONT_DEFAULT_MEM_LOCATION_START:usize = 0x50;
|
const FONT_DEFAULT_MEM_LOCATION_START:usize = 0x50;
|
||||||
const FONT_DEFAULT_MEM_LOCATION_END:usize = 0x9F;
|
const FONT_DEFAULT_MEM_LOCATION_END:usize = 0x9F;
|
||||||
|
const ROM_START:usize = 0x200;
|
||||||
pub fn cycle(&mut self){
|
pub fn cycle(&mut self){
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_default_font(&mut self){
|
pub fn set_default_font(&mut self){
|
||||||
|
log::info!("Loaded default font from memory");
|
||||||
self.memory[Self::FONT_DEFAULT_MEM_LOCATION_START..=Self::FONT_DEFAULT_MEM_LOCATION_END].copy_from_slice(&Self::DEFAULT_FONT);
|
self.memory[Self::FONT_DEFAULT_MEM_LOCATION_START..=Self::FONT_DEFAULT_MEM_LOCATION_END].copy_from_slice(&Self::DEFAULT_FONT);
|
||||||
}
|
}
|
||||||
|
/// load a rom from bytes
|
||||||
|
pub fn load_rom(&mut self,rom: &[u8]){
|
||||||
|
log::info!("Loaded ROM from memory");
|
||||||
|
self.memory[Self::ROM_START..].copy_from_slice(rom);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
impl Drop for Device{
|
impl Drop for Device{
|
||||||
@@ -64,17 +71,18 @@ impl Drop for Device{
|
|||||||
|
|
||||||
pub struct RegisterFile {
|
pub struct RegisterFile {
|
||||||
pub v: [u8; 0x10],
|
pub v: [u8; 0x10],
|
||||||
// program counter - only u12 technically.
|
/// program counter - only u12 technically.
|
||||||
pub pc: u16,
|
pub pc: u16,
|
||||||
/// stack pointer
|
/// stack pointer
|
||||||
pub i: u16,
|
pub i: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RegisterFile {
|
impl RegisterFile {
|
||||||
|
pub const DEFAULT_PC_VALUE:u16 = Device::ROM_START as u16;
|
||||||
pub fn new() -> RegisterFile {
|
pub fn new() -> RegisterFile {
|
||||||
RegisterFile {
|
RegisterFile {
|
||||||
v: [0; 0x10],
|
v: [0; 0x10],
|
||||||
pc: 0x200,
|
pc: Self::DEFAULT_PC_VALUE,
|
||||||
i: 0,
|
i: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -17,8 +17,6 @@ fn main() {
|
|||||||
device.set_default_font();
|
device.set_default_font();
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user