rusty_man_computer 0.5.0

Little-Man Computer emulator and assembler tools
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
use clap::Parser;
use std::{error::Error, fs, io::Write, path::PathBuf};
use value::Value;

pub mod value {
    use std::{
        fmt,
        ops::{AddAssign, RangeInclusive, SubAssign},
    };

    /// Represents a value held by one letterbox (memory cell) in the LMC
    #[derive(Copy, Clone, Debug, PartialEq)]
    pub struct Value(i16);

    impl Value {
        pub const MIN: i16 = -999;
        pub const MAX: i16 = 999;
        pub const RANGE: RangeInclusive<i16> = Self::MIN..=Self::MAX;

        pub fn new(value: i16) -> Result<Value, ()> {
            if value < -999 || value > 999 {
                Err(())
            } else {
                Ok(Value(value))
            }
        }

        /// Creates a new `Value` from an `i16`, wrapping around if the value is out of bounds.
        /// This is useful for handling overflow when adding or subtracting values.
        pub fn wrap_overflow(value: i16) -> Value {
            let positive_overflow = value - Self::MAX;
            if positive_overflow > 0 {
                return Value::new((Self::MIN - 1) + positive_overflow)
                    .expect("Out of bounds after overflow handling");
            };
            let negative_overflow = value + Self::MAX;
            if negative_overflow < 0 {
                return Value::new((Self::MAX + 1) + negative_overflow)
                    .expect("Out of bounds after overflow handling");
            };
            Value::new(value).expect("Out of bounds after overflow handling")
        }

        pub fn zero() -> Value {
            Value::new(0).expect("Failed to create zero value")
        }

        pub fn from_digits(first_digit: i16, last_two_digits: i16) -> Result<Value, &'static str> {
            if !(0..=9).contains(&first_digit) {
                return Err("First digit out of range");
            }
            if !(0..=99).contains(&last_two_digits) {
                return Err("Last two digits out of range");
            }
            Value::new(first_digit * 100 + last_two_digits).or(Err("Value out of range"))
        }

        pub fn first_digit(&self) -> i16 {
            self.0 / 100
        }

        pub fn last_two_digits(&self) -> i16 {
            self.0 % 100
        }

        pub fn is_zero(&self) -> bool {
            self.0 == 0
        }

        pub fn is_negative(&self) -> bool {
            self.0 < 0
        }

        pub fn is_positive(&self) -> bool {
            self.0 > 0
        }

        pub fn is_non_negative(&self) -> bool {
            self.0 >= 0
        }

        pub fn to_string(&self) -> String {
            self.0.to_string()
        }

        /// Converts the value into its big-endian byte representation (2 bytes)
        pub fn to_be_bytes(&self) -> [u8; 2] {
            self.0.to_be_bytes()
        }
    }

    impl PartialEq<i16> for Value {
        fn eq(&self, other: &i16) -> bool {
            self.0 == *other
        }
    }

    impl From<Value> for i16 {
        fn from(value: Value) -> i16 {
            value.0
        }
    }

    impl From<Value> for char {
        fn from(value: Value) -> char {
            value.0 as u8 as char
        }
    }

    impl From<i8> for Value {
        fn from(value: i8) -> Value {
            // This is fine because any i8 value will be within -999 to 999
            Value(value as i16)
        }
    }

    impl TryFrom<i16> for Value {
        type Error = &'static str;

        fn try_from(value: i16) -> Result<Self, Self::Error> {
            Value::new(value).or(Err("Value out of range"))
        }
    }

    impl AddAssign for Value {
        fn add_assign(&mut self, other: Value) {
            *self = Value::wrap_overflow(self.0 + other.0);
        }
    }

    impl SubAssign for Value {
        fn sub_assign(&mut self, other: Value) {
            *self = Value::wrap_overflow(self.0 - other.0);
        }
    }

    // Thank you to https://stackoverflow.com/a/77841395/11519302 for showing me how to do this
    impl fmt::Display for Value {
        fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
            self.0.fmt(formatter)
        }
    }
}

type RAM = [Value; 100];

struct OutputConfig {
    immediately_print_output: bool,
}

pub struct Output {
    buffer: String,
    config: OutputConfig,
}

impl Output {
    fn new(config: OutputConfig) -> Output {
        Output {
            buffer: String::new(),
            config,
        }
    }

    fn push_char(&mut self, character: char) {
        self.buffer.push(character);
        if self.config.immediately_print_output {
            print!("{}", character);
        }
    }

    fn push_int(&mut self, integer: Value) {
        // If two numbers are printed in a row, separate them with an newline
        // This seems to be what the online LMC simulator does
        let last_digit_was_number = self.chars().last().unwrap_or(' ').is_numeric();
        if last_digit_was_number {
            self.push_char('\n');
        }
        self.buffer.push_str(&integer.to_string());
        if self.config.immediately_print_output {
            print!("{}", integer.to_string());
        }
    }

    fn chars(&self) -> std::str::Chars {
        self.buffer.chars()
    }

    /// Splits the output into lines of 4 characters maximum.
    /// Does a line break when 4 characters is reached, or a \n is reached
    fn split_into_lines(&self, max_line_length: isize) -> Vec<String> {
        let chars = self.buffer.chars();
        let mut lines = Vec::<String>::new();
        lines.push(String::new());
        let mut current_row = lines.last_mut().unwrap();
        let mut row_length = 0;
        for char in chars {
            if char == '\n' {
                lines.push(String::new());
                current_row = lines.last_mut().unwrap();
                row_length = 0;
                continue;
            }
            if row_length >= max_line_length {
                lines.push(String::new());
                current_row = lines.last_mut().unwrap();
                row_length = 0;
            }
            // Add our character to the current row
            current_row.push(char);
            row_length += 1;
        }
        lines
    }

    /// Prints the output on one line by separating the output lines with a pipe
    fn print_on_one_line(&self) {
        const LINE_WIDTH: isize = 4;
        let rows = self.split_into_lines(LINE_WIDTH);
        println!("{}", rows.join(&color_gray("|")));
    }

    pub fn read_all(&self) -> String {
        self.buffer.clone()
    }
}

struct Registers {
    program_counter: usize,
    instruction_register: i8,
    address_register: usize,
    accumulator: Value,
}

pub struct Computer {
    // Array of 100 i16 ints. Valid values are -999 to 999
    ram: RAM,
    registers: Registers,
    pub output: Output,
    config: Config,
}

impl Computer {
    pub fn new(config: Config) -> Computer {
        Computer {
            ram: [Value::zero(); 100],
            registers: Registers {
                program_counter: 0,
                instruction_register: 0,
                address_register: 0,
                accumulator: Value::zero(),
            },
            output: Output::new(OutputConfig {
                immediately_print_output: config.print_raw_output,
            }),
            config: config,
        }
    }

    /// Initialises RAM with the data from the file provided in the config.
    /// If no file is provided, RAM stays empty (untouched).
    pub fn initialize_ram_from_file(&mut self) -> Result<(), Box<dyn Error>> {
        // If a memory dump (.bin file) has been provided, load it into RAM
        match self.config.load_ram_file_path {
            Some(ref file_path) => {
                let data = fs::read(file_path)?;
                let touched_addresses = self.load_data_to_ram(data);
                println!("Loaded {} data cells into RAM", touched_addresses);
                Ok(())
            }
            None => {
                println!("Initial RAM (.bin) file not provided. RAM will be empty.");
                Ok(())
            }
        }
    }

    /// Returns the number of addresses modified
    pub fn load_data_to_ram(&mut self, data_bytes: Vec<u8>) -> i32 {
        let mut touched_addresses = 0;
        for (i, &byte) in data_bytes.iter().enumerate() {
            if i >= self.ram.len() * 2 {
                break;
            }
            let target_address = i / 2;
            if i % 2 == 0 {
                self.ram[target_address] = Value::new((byte as i16) << 8).unwrap();
                touched_addresses += 1;
            } else {
                self.ram[target_address] += Value::new(byte as i16).unwrap();
            }
        }
        touched_addresses
    }

    pub fn clock_cycle(&mut self) -> bool {
        // Stage 1: Fetch
        let ram_index = self.registers.program_counter;
        self.registers.program_counter += 1;

        // Stage 2: Decode
        let instruction = self.ram[ram_index];
        let instruction_code = instruction.first_digit();
        let instruction_address = instruction.last_two_digits();
        self.registers.instruction_register =
            instruction_code.try_into().expect("Opcode out of range");
        self.registers.address_register = instruction_address as usize;

        // Stage 3: Execute
        self.execute_instruction()
    }

    fn get_input(&mut self) -> Value {
        match &mut self.config.input {
            Some(input) => {
                if input.is_empty() {
                    panic!("No more input values available");
                }
                input.remove(0)
            }
            None => {
                let prompt = format!("INP: Number input: {}", BOLD);
                read_input_until_valid(&prompt).unwrap_or_else(|_| Value::zero())
            }
        }
    }

    /// Returns `false` if the computer should halt, and `true` otherwise
    fn execute_instruction(&mut self) -> bool {
        match self.registers.instruction_register {
            0 => {
                // HLT - Stop (Little Man has a rest)
                println!("\n{}", bold("Halted!"));
                return false;
            }
            1 => {
                // ADD - Add the contents of the memory address to the Accumulator
                self.registers.accumulator += self.ram[self.registers.address_register];
            }
            2 => {
                // SUB - Subtract the contents of the memory address from the Accumulator
                self.registers.accumulator -= self.ram[self.registers.address_register];
            }
            3 => {
                // STA or STO - Store the value in the Accumulator in the memory address given
                self.ram[self.registers.address_register] = self.registers.accumulator;
            }
            4 => {
                // This code is unused and gives an error
                panic!("Opcode 4 is not allowed!");
            }
            5 => {
                // LDA - Load the Accumulator with the contents of the memory address given
                self.registers.accumulator = self.ram[self.registers.address_register];
            }
            6 => {
                // BRA - Branch - use the address given as the address of the next instruction
                self.registers.program_counter = self.registers.address_register;
                if self.config.print_computer_state {
                    println!("BRA: Jumping to address {}", self.registers.program_counter)
                }
            }
            7 => {
                // BRZ - Branch to the address given if the Accumulator is zero
                if self.registers.accumulator.is_zero() {
                    self.registers.program_counter = self.registers.address_register;
                    if self.config.print_computer_state {
                        println!("BRZ: Jumping to address {}", self.registers.program_counter)
                    }
                }
            }
            8 => {
                // BRP - Branch to the address given if the Accumulator is zero or positive
                if self.registers.accumulator.is_non_negative() {
                    self.registers.program_counter = self.registers.address_register;
                }
            }
            9 => {
                if self.registers.address_register == 1 {
                    // INP - Take from Input
                    self.registers.accumulator = self.get_input();
                }
                if self.registers.address_register == 2 {
                    // OUT - Copy to Output
                    self.output.push_int(self.registers.accumulator);
                }
                if self.registers.address_register == 22 {
                    // OTC - self. accumulator as a character (Non-standard instruction)
                    let character = self.registers.accumulator.into();
                    self.output.push_char(character);
                }
            }
            _ => {
                panic!("Unhandled opcode: {}", self.registers.instruction_register);
            }
        }
        true
    }

    fn print_registers(&self) {
        println!(
            "PC: {}, Instruction: {}, Addr: {}, Acc: {}",
            bold(&format!("{:02}", self.registers.program_counter)),
            bold(&format!("{:01}", self.registers.instruction_register)),
            bold(&format!("{:02}", self.registers.address_register)),
            bold(&format!("{:03}", self.registers.accumulator))
        );
    }

    fn print_ram(&self) {
        let columns = 10;
        for (i, &cell) in self.ram.iter().enumerate() {
            if cell.is_zero() {
                // Print in gray
                print!("{} ", color_gray("000"));
            } else {
                print!("{:#03} ", cell);
            }

            if (i + 1) % columns == 0 {
                println!();
            }
        }
    }

    pub fn run(&mut self) {
        let mut should_continue = true;
        while should_continue {
            if self.config.print_computer_state {
                println!();
                self.print_registers();
                self.output.print_on_one_line();
                self.print_ram();
            }
            should_continue = self.clock_cycle();
        }
    }
}

const BOLD: &str = "\x1b[1m";
const GRAY: &str = "\x1b[90m";
const RED: &str = "\x1b[31m";
const FORMAT_END: &str = "\x1b[0m";

/// Wrap the provided test in a grey/gray color code
fn color_gray(text: &str) -> String {
    [GRAY, text, FORMAT_END].concat()
}

/// Wrap the provided test in a grey/gray color code. Used for errors
fn color_red(text: &str) -> String {
    [RED, text, FORMAT_END].concat()
}

fn bold(text: &str) -> String {
    [BOLD, text, FORMAT_END].concat()
}

pub fn print_error(error: &str) {
    eprintln!("{}", color_red(error));
}

enum ReadInputError {
    Unrecoverable(std::io::Error),
    Validation,
}

fn read_input() -> Result<Value, ReadInputError> {
    let mut input = String::new();
    match std::io::stdin().read_line(&mut input) {
        Ok(_) => match input.trim().parse() {
            Ok(num) => match Value::new(num) {
                Ok(value) => return Ok(value),
                Err(_) => {
                    print_error("Please input an integer between -999 and 999");
                    return Err(ReadInputError::Validation);
                }
            },
            Err(_) => {
                print_error("Please input a valid integer between -999 and 999");
                return Err(ReadInputError::Validation);
            }
        },
        Err(error) => {
            print_error("Error: Failed to read input");
            return Err(ReadInputError::Unrecoverable(error));
        }
    }
}

fn read_input_until_valid(prompt: &str) -> Result<Value, ()> {
    loop {
        print!("{}", prompt);
        std::io::stdout().flush().unwrap_or(());
        print!("{}", FORMAT_END);
        match read_input() {
            Ok(num) => return Ok(num),
            Err(ReadInputError::Unrecoverable(_)) => return Err(()),
            Err(ReadInputError::Validation) => continue,
        }
    }
}

pub struct Config {
    pub load_ram_file_path: Option<PathBuf>,
    /// If the register values, output buffer, RAM values, and branch messages should be printed after every clock cycle
    pub print_computer_state: bool,
    /// If output should be directly and immediately printed when a OUT/OTC instruction is executed
    pub print_raw_output: bool,
    /// Allows specifying input to be given to the emulator programmatically, instead of interactively in the terminal.
    /// You must specify the entire input ahead-of-time.
    /// It is formatted as a vector of integers. Each time the INP instruction is called, the next integer in the vector is used.
    /// Panics if the INP instruction is called after all values have been used.
    /// This feature is most useful when writing tests.
    pub input: Option<Vec<Value>>,
}

impl Config {
    pub fn from_args(args: Args) -> Config {
        if args.ram_legacy.is_some() && args.ram.is_some() {
            print_error("Warning: Ignoring positional argument and using --ram argument instead.");
            print_error("Specifying a RAM file without --ram is no longer recommended.");
        }

        Config {
            load_ram_file_path: args.ram.or_else(|| {
                eprintln!(
                    "Note: It is recommended to use the --ram argument to specify a RAM file."
                );
                args.ram_legacy
            }),
            print_computer_state: !args.output_only,
            print_raw_output: args.output_only,
            input: None,
        }
    }
}

impl Default for Config {
    fn default() -> Self {
        Config {
            load_ram_file_path: None,
            print_computer_state: true,
            print_raw_output: false,
            input: None,
        }
    }
}

#[derive(Parser)]
#[command(version)]
pub struct Args {
    // Positional arg for memory file (kept for backwards compatibility)
    #[arg(hide = true)]
    ram_legacy: Option<PathBuf>,
    /// Path to a memory dump (.bin) file to load into RAM
    #[arg(long)]
    ram: Option<PathBuf>,
    /// Only print the output of the LMC, excluding the RAM and register values.
    #[arg(long)]
    output_only: bool,
}

pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
    let mut computer = Computer::new(config);
    computer.initialize_ram_from_file()?;
    computer.run();
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn hlt_instruction_works() {
        let mut computer = Computer::new(Config::default());
        computer.ram[0] = 000.into();
        let has_halted = !computer.clock_cycle();
        // It should halt after the first clock cycle
        assert!(has_halted);
    }

    #[test]
    fn add_instruction_works() {
        // Test 40 + 2 = 42
        let mut computer = Computer::new(Config::default());
        computer.registers.accumulator = 40.into();
        computer.ram[99] = 2.into(); // Operand
        computer.ram[0] = Value::new(199).unwrap(); // Add address 99 to ACC
        computer.clock_cycle();
        assert_eq!(computer.registers.accumulator, 42);
    }

    #[test]
    fn sub_instruction_works() {
        // Test 42 - 2 = 40
        let mut computer = Computer::new(Config::default());
        computer.registers.accumulator = 42.into();
        computer.ram[99] = 2.into(); // Operand
        computer.ram[0] = Value::new(299).unwrap(); // Subtract address 99 from ACC
        computer.clock_cycle();
        assert_eq!(computer.registers.accumulator, 40);
    }

    #[test]
    fn store_instruction_works() {
        // Test storing 42 in address 99
        let mut computer = Computer::new(Config::default());
        computer.registers.accumulator = 42.into();
        computer.ram[0] = Value::new(399).unwrap(); // Store ACC to address 99
        computer.clock_cycle();
        assert_eq!(computer.ram[99], 42);
    }

    #[test]
    fn load_instruction_works() {
        // Test loading 42 from address 99
        let mut computer = Computer::new(Config::default());
        computer.ram[99] = 42.into();
        computer.ram[0] = Value::new(599).unwrap(); // Load ACC from address 99
        computer.clock_cycle();
        assert_eq!(computer.registers.accumulator, 42);
    }

    #[test]
    fn branch_instruction_works() {
        // Test branching/jumping to address 42
        let mut computer = Computer::new(Config::default());
        computer.ram[0] = Value::new(642).unwrap(); // Branch to address 42
        computer.clock_cycle();
        assert_eq!(computer.registers.program_counter, 42);
    }

    #[test]
    fn branch_zero_instruction_when_zero() {
        // Test BRZ when the accumulator is zero (so it should branch)
        let mut computer = Computer::new(Config::default());
        computer.registers.accumulator = 0.into();
        computer.ram[0] = Value::new(742).unwrap(); // Branch to address 42 if ACC is zero
        computer.clock_cycle();
        assert_eq!(computer.registers.program_counter, 42);
    }

    #[test]
    fn branch_zero_instruction_when_non_zero() {
        // Test BRZ when the accumulator is non-zero (so it should not branch)
        let mut computer = Computer::new(Config::default());
        computer.registers.accumulator = (-5).into();
        computer.ram[0] = Value::new(742).unwrap(); // Branch to address 42 if ACC is zero
        computer.clock_cycle();
        assert_eq!(computer.registers.program_counter, 1);
    }

    #[test]
    fn branch_positive_instruction_when_positive() {
        // Test BRP when the accumulator is positive (so it should branch)
        let mut computer = Computer::new(Config::default());
        computer.registers.accumulator = 5.into();
        computer.ram[0] = Value::new(842).unwrap(); // Branch to address 42 if ACC is positive
        computer.clock_cycle();
        assert_eq!(computer.registers.program_counter, 42);
    }

    #[test]
    fn branch_positive_instruction_when_zero() {
        // Test BRP when the accumulator is zero (so it should branch)
        // (boundary data)
        let mut computer = Computer::new(Config::default());
        computer.registers.accumulator = 0.into();
        computer.ram[0] = Value::new(842).unwrap(); // Branch to address 42 if ACC is positive
        computer.clock_cycle();
        assert_eq!(computer.registers.program_counter, 42);
    }

    #[test]
    fn branch_positive_instruction_when_negative() {
        // Test BRP when the accumulator is negative (so it should not branch)
        let mut computer = Computer::new(Config::default());
        computer.registers.accumulator = (-5).into();
        computer.ram[0] = Value::new(842).unwrap(); // Branch to address 42 if ACC is positive
        computer.clock_cycle();
        assert_eq!(computer.registers.program_counter, 1);
    }

    #[test]
    fn input_instruction_works() {
        // Test inputting 21
        let mut computer = Computer::new(Config {
            input: Some(vec![21.into()]),
            ..Config::default()
        });
        computer.ram[0] = Value::new(901).unwrap();
        computer.clock_cycle();
        assert_eq!(computer.registers.accumulator, 21);
    }

    #[test]
    fn output_instruction_works() {
        // Test outputting 21
        let mut computer = Computer::new(Config::default());
        computer.registers.accumulator = 21.into();
        computer.ram[0] = Value::new(902).unwrap();
        computer.clock_cycle();
        assert_eq!(computer.output.read_all(), "21");
    }

    #[test]
    fn output_character_instruction_works() {
        // Test outputting ASCII value 104 (h)
        let mut computer = Computer::new(Config::default());
        computer.registers.accumulator = 104.into();
        computer.ram[0] = Value::new(922).unwrap();
        computer.clock_cycle();
        assert_eq!(computer.output.read_all(), "h");
    }

    #[test]
    fn output_basic_line_wrapping() {
        let mut output = Output::new(OutputConfig {
            immediately_print_output: false,
        });
        output.push_char('a');
        output.push_char('b');
        output.push_char('c');
        output.push_char('d');
        output.push_char('e');
        let lines = output.split_into_lines(4);
        assert_eq!(lines, vec!["abcd", "e"]);
    }

    #[test]
    fn output_numbers_on_separate_lines() {
        let mut output = Output::new(OutputConfig {
            immediately_print_output: false,
        });
        output.push_int(Value::from(1));
        output.push_int(Value::from(2));
        output.push_int(Value::from(3));
        let lines = output.split_into_lines(4);
        assert_eq!(lines, vec!["1", "2", "3"]);
    }

    #[test]
    fn output_mixed_numbers_and_characters() {
        let mut output = Output::new(OutputConfig {
            immediately_print_output: false,
        });
        // Part of an ASCII table
        output.push_int(Value::from(33));
        output.push_char(' ');
        output.push_char('!');
        output.push_int(Value::from(34));
        output.push_char(' ');
        output.push_char('"');
        let lines = output.split_into_lines(4);
        assert_eq!(lines, vec!["33 !", "34 \""]);
    }

    #[test]
    fn value_works() {
        // Normal data
        Value::new(0).unwrap();
        Value::new(901).unwrap();
        Value::new(-10).unwrap();
        // Boundary data
        Value::new(999).unwrap();
        Value::new(-999).unwrap();
    }

    #[test]
    fn value_equality_check() {
        // We like our zeroes here
        assert!(Value::zero() == 0);
        assert!(Value::new(0).unwrap() == 0);
        assert!(Value::from(0) == Value::new(0).unwrap());
        assert!(Value::zero().is_zero());
        // Let's add in one normal number too
        assert!(Value::new(21).unwrap() == 21);
    }

    #[test]
    fn value_wraps_overflow() {
        // Boundary data
        let mut value = Value::new(999).unwrap();
        value += Value::from(1);
        assert_eq!(value, -999);
    }

    #[test]
    fn value_wraps_underflow() {
        // Boundary data
        let mut value = Value::new(-999).unwrap();
        value -= Value::from(1);
        assert_eq!(value, 999);
    }

    #[test]
    fn value_wraps() {
        // "Normal" data (if we assume that wrapping is the normal behaviour)
        let mut value = Value::new(990).unwrap();
        value += Value::from(21);
        // Checked against Peter Higginson's LMC simulator (wraps to -988)
        assert_eq!(value, -988);
    }

    #[test]
    fn value_to_string() {
        assert_eq!(Value::from(3).to_string(), "3");
        assert_eq!(Value::new(-123).unwrap().to_string(), "-123");
    }

    #[test]
    fn value_disallows_invalid_values() {
        // Boundary data
        assert!(Value::new(1000).is_err());
        assert!(Value::new(-1000).is_err());
        // Invalid data
        assert!(Value::new(2025).is_err());
    }

    #[test]
    fn value_first_and_last_digits() {
        // Testing the functions used to extract operators and operands from instructions
        let instruction = Value::new(599).unwrap();
        assert_eq!(instruction.first_digit(), 5);
        assert_eq!(instruction.last_two_digits(), 99);
        assert_eq!(Value::zero().first_digit(), 0);
        assert_eq!(Value::zero().last_two_digits(), 0);
    }
}