diff --git a/.gitignore b/.gitignore index 6e9092e..3646a45 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target -*.pdf \ No newline at end of file +*.pdf +*.bin \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index e4a9bad..789af46 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,6 +5,8 @@ "version": "0.2.0", "configurations": [ + + { "type": "lldb", "request": "launch", @@ -57,7 +59,7 @@ "kind": "bin" } }, - "args": [], + "args": ["simple_test_machine\\programs\\mult10.bin"], "cwd": "${workspaceFolder}" }, ] diff --git a/simple_test_machine/programs/hello.asm b/simple_test_machine/programs/hello.asm new file mode 100644 index 0000000..90d920f --- /dev/null +++ b/simple_test_machine/programs/hello.asm @@ -0,0 +1,37 @@ + # Load string into memory at the output address + 0xA2, b'H', # LDX H + 0x86, output_addr, # STX + 0xA2, b'e', # LDX e + 0x86, output_addr + 1, # STX + 0xA2, b'l', # LDX l + 0x86, output_addr + 2, # STX + 0xA2, b'l', # LDX l + 0x86, output_addr + 3, # STX + 0xA2, b'o', # LDX o + 0x86, output_addr + 4, # STX + + 0xA2, b' ', # LDX ' ' + 0x86, output_addr + 5, # STX + + 0xA2, b'w', # LDX w + 0x86, output_addr + 6, # STX + 0xA2, b'o', # LDX o + 0x86, output_addr + 7, # STX + 0xA2, b'r', # LDX r + 0x86, output_addr + 8, # STX + 0xA2, b'l', # LDX l + 0x86, output_addr + 9, # STX + 0xA2, b'd', # LDX d + 0x86, output_addr + 10, # STX + 0xA2, b'!', # LDX ! + 0x86, output_addr + 11, # STX + + 0xA2, 0x00, # LDX 0 + 0x86, output_addr + 12, # STX + + # Set flag to do the print + 0xA2, 0x01, # LDX 1 + 0x86, print_flag_addr, # STX + + # End the program + 0x60 # RTS \ No newline at end of file diff --git a/simple_test_machine/programs/mult10.asm b/simple_test_machine/programs/mult10.asm new file mode 100644 index 0000000..004ad15 --- /dev/null +++ b/simple_test_machine/programs/mult10.asm @@ -0,0 +1,26 @@ + +; Multiply 7 with 10 and print the result + + ; FAST MULTIPLY program from: + ; http://6502.org/source/integers/fastx10.htm +main LDA #7 ; load 7 into the accumulator + ASL ;multiply by 2 + STA TEMP ;temp store in TEMP + ASL ;again multiply by 2 (*4) + ASL ;again multiply by 2 (*8) + CLC + ADC TEMP ;as result, A = x*8 + x*2 + + ; PRINT RESULT + STA $A0 ; 0x85, 0xA0, ; store A into the output addr + LDX #0 ; 0xA2, 0x00, ; null terminator + STX $A1 ; 0x86, 0xA1, ; store null terminator to output addr + 1 + + ; Set flag to do the print + LDX #1 ; 0xA2, 0x01, + STX $9F ; 0x86, 0x9F, + + ; End the program + RTS ; 0x60 + + TEMP .byte 0 diff --git a/simple_test_machine/src/machine.rs b/simple_test_machine/src/machine.rs index 9cc8cc1..d17ff6a 100644 --- a/simple_test_machine/src/machine.rs +++ b/simple_test_machine/src/machine.rs @@ -52,11 +52,10 @@ impl Bus for TBus // CONSOLE //||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| -pub const OUTPUT_ADDR: u16 = 0x00A0; -pub const PRINT_BYTE_FLAG: u16 = 0x009E; -pub const PRINT_STR_FLAG: u16 = 0x009F; +pub const OUTPUT_ADDR: u16 = 0x00A0; // Put values to be printed at this location! +pub const PRINT_BYTE_FLAG: u16 = 0x009F; // Then set one of these flags to trigger the print +pub const PRINT_STR_FLAG: u16 = 0x009E; // and indicate what type is being printed. -// TODO: Handle input struct OutputConsole { diff --git a/simple_test_machine/src/main.rs b/simple_test_machine/src/main.rs index b1ed7ee..923b3f1 100644 --- a/simple_test_machine/src/main.rs +++ b/simple_test_machine/src/main.rs @@ -1,13 +1,35 @@ -use machine::{OUTPUT_ADDR, PRINT_STR_FLAG, PRINT_BYTE_FLAG, TestMachine}; +use std::{ fs, env }; + mod machine; +use machine::{OUTPUT_ADDR, PRINT_STR_FLAG, PRINT_BYTE_FLAG, TestMachine}; + + fn main() { - hello_world_test(); - println!(); - fast_mult_by_10(); + let args: Vec = env::args().collect(); + + if args.len() < 2 + { + println!("No program provided, running internal test programs..."); + hello_world_test(); + println!(); + fast_mult_by_10(7); + + return; + } + + let program = fs::read(&args[1]).expect(&format!("Failed read program file: {}", &args[1])); + + let mut vm = TestMachine::new(); + + vm.load_program(&program); + vm.reset(); + vm.run_program(); + + println!("Program stopped"); } fn hello_world_test() @@ -66,7 +88,7 @@ fn hello_world_test() } -fn fast_mult_by_10() +fn fast_mult_by_10(val: u8) { // Program from: // http://6502.org/source/integers/fastx10.htm @@ -88,7 +110,7 @@ fn fast_mult_by_10() let program = [ - 0xA9, 0x07, // LDA 7 - The value we want to multiply + 0xA9, val, // LDA val - The value we want to multiply // START OF MULT10 FUNCTION 0x0A, // ASL ;multiply by 2 diff --git a/todo/todo.todo b/todo/todo.todo index 4434ccd..5d6a13c 100644 --- a/todo/todo.todo +++ b/todo/todo.todo @@ -7,6 +7,7 @@ General: Test Machine: ✔ Hello world program @done(24-01-19 17:09) + ✔ Load and run a given program binary @done(24-01-22 17:08) CPU: Signals: