More work on code sharing

main
John Jekel 1 year ago
parent 009c278229
commit 83df4192b8
  1. 33
      lib/asm.rs
  2. 24
      src/legv8assemble.rs

@ -53,7 +53,7 @@ pub fn assemble_raw(instruction_string: &str) -> Option<DecodedInstruction> {//D
decoded_instruction = DecodedInstruction::R{
opcode: DecodedOpcode::Invalid,
rm: 0xFF,
shamt: 0xFF,
shamt: 0,//Always 0 in LEGv8
rn: 0xFF,
rd: 0xFF
};
@ -70,7 +70,7 @@ pub fn assemble_raw(instruction_string: &str) -> Option<DecodedInstruction> {//D
decoded_instruction = DecodedInstruction::D{
opcode: DecodedOpcode::Invalid,
addr9: 0xFFFF,
op2: 0xFF,
op2: 0,//Always 0 in LEGv8
rn: 0xFF,
rt: 0xFF
};
@ -136,14 +136,6 @@ pub fn assemble_raw(instruction_string: &str) -> Option<DecodedInstruction> {//D
//However, the tokens themselves, as well as the condition for B.cond, will still need checking
//Begin to construct the instruction
/*eprintln!("TESTING: {:?}", decoded_instruction);//TESTING
match &mut decoded_instruction {//This works!
DecodedInstruction::R{opcode, ..} => {
*opcode = DecodedOpcode::ADD;
}
_ => {panic!("testing");}
}
*/
//Determine the opcode
match &mut decoded_instruction {
@ -232,32 +224,33 @@ pub fn assemble_raw(instruction_string: &str) -> Option<DecodedInstruction> {//D
DecodedInstruction::B{..} => {}//It does not have this field
}
/*
//Determine Rn depending on the instruction type
match instruction_type {
DecodedInstruction::R | DecodedInstruction::I | DecodedInstruction::D => {
match &mut decoded_instruction {
DecodedInstruction::R{rn, ..} | DecodedInstruction::I{rn, ..} | DecodedInstruction::D{rn, ..} => {
if tokens[0] != "BR" {//This one does not have the Rn operand
if let Some(rn) = parse_register(tokens[2]) {
instruction.set_bits(rn, 9, 5);
if let Some(parsed_rn) = parse_register(tokens[2]) {
*rn = parsed_rn;
} else {
return None;
}
}
}
DecodedInstruction::CB | DecodedInstruction::IM | DecodedInstruction::B => {}//They do not have this field
DecodedInstruction::CB{..} | DecodedInstruction::IW{..} | DecodedInstruction::B{..} => {}//They do not have this field
}
//Determine Rm if the instruction type is R
if matches!(instruction_type, DecodedInstruction::R) {
if tokens[0] != "BR" {//This one does not have the Rn operand
if let Some(rm) = parse_register(tokens[3]) {
instruction.set_bits(rm, 20, 16);
if let DecodedInstruction::R{rm, ..} = &mut decoded_instruction {
if tokens[0] != "BR" {//This one does not have the Rm operand
if let Some(parsed_rm) = parse_register(tokens[3]) {
*rm = parsed_rm;
} else {
return None;
}
}
}
/*
//Determine immediate fields
//Determine the immediate/address to use from the 4th token depending on the instruction_type

@ -11,7 +11,7 @@
/* Imports */
use legv8::ConvenientlyBitAccessible;
use legv8::{asm, encode, ConvenientlyBitAccessible};
/* Constants */
@ -28,13 +28,13 @@ use legv8::ConvenientlyBitAccessible;
/* Types */
#[derive(Copy, Clone, Debug)]
pub enum InstructionType {//TODO remove this
pub enum InstructionType {
R,
I,
D,
B,
CB,
IM
IM//TODO fix to be IW
}
/* Associated Functions and Methods */
@ -44,7 +44,7 @@ pub enum InstructionType {//TODO remove this
/* Functions */
fn main() {
eprintln!("\x1b[1m\x1b[35mlegv8assemble\x1b[0m, by \x1b[96mJZJ\x1b[0m :)");
eprintln!("\x1b[1m\x1b[35mlegv8assemble\x1b[0m, by \x1b[96mJZJ\x1b[0m :)");//TODO replace this with figlet
eprintln!("Copyright (C) 2023 John Jekel");
eprintln!("See the LICENSE file at the root of the project for licensing info.\n");
@ -57,7 +57,7 @@ fn main() {
let trimmed_line = line_buffer.trim();
let nice_line = trimmed_line.to_string().to_uppercase();
if let Some((instruction_type, instruction)) = assemble(&nice_line) {
if let Some((instruction_type, instruction)) = assemble_to_raw_instruction(&nice_line) {
match instruction_type {
InstructionType::R => {
eprintln!(" The instruction \"\x1b[1m{}\x1b[0m\" is \x1b[94mR\x1b[0m-type", nice_line);
@ -180,7 +180,19 @@ fn main() {
}
}
fn assemble(instruction_string: &str) -> Option<(InstructionType, u32)> {
fn assemble_to_raw_instruction(instruction_string: &str) -> Option<(InstructionType, u32)> {
//TODO replace this function with a wrapper over library functions
/*
let assembly_result = asm::assemble_raw(instruction_string)?;
let instruction_type;
match assembly_result {
//TODO convert to InstructionType
}
let raw_instruction = encode::encode(assembly_result)?;
return Some(instruction_type, raw_instruction);
*/
let mut tokens = Vec::<&str>::with_capacity(5);//Max number of tokens we expect
for token in instruction_string.split(&[' ', '\t', '\n', '\r', ',', '[', ']']) {//TODO ensure the syntax is correct instead of just stripping out these symbols when splitting
if !token.is_empty() {