Giter Club home page Giter Club logo

m68000's People

Contributors

stovent avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

geekstakulus

m68000's Issues

Adding displacement panics because of integer overflow

The code that adds a 16-bit displacement to the PC fails under some circumstances, because Rust (at least in debug mode) panics if integers overflow.

I got this when I tried to execute a dbf instruction with a negative displacement.

thread 'main' panicked at 'attempt to add with overflow', /home/vilcans/proj/m68000/src/interpreter.rs:1186:32

This was caused by the following code:

self.regs.pc = pc + disp as u32;

which causes a panic if disp is negative.

A fix is to explicitly allow wraparound:

self.regs.pc = pc.wrapping_add(disp as u32);

I have tried searching for the string + disp in interpreter.rs which finds some other places that look similar and may have the same problem.

Reproduction case

Try running the following code with cargo +nightly run. Note that it panics. It does not panic when you run it with cargo +nightly run --release as overflow checks are switched off in release mode.

use m68000::{MemoryAccess, M68000};

const START_ADDRESS: u32 = 0x10000;

const PROGRAM: [u16; 4] = [
    0x51c8, // dbf d0,nnnn
    0xfffe, // negative offset
    0x4e72, // stop
    0x2700,
];

struct Memory;

#[allow(unused_variables)]
impl MemoryAccess for Memory {
    fn get_byte(&mut self, addr: u32) -> Option<u8> {
        unimplemented!();
    }
    fn get_word(&mut self, addr: u32) -> Option<u16> {
        PROGRAM
            .get(addr.checked_sub(START_ADDRESS)? as usize / 2)
            .copied()
    }
    fn set_byte(&mut self, addr: u32, value: u8) -> Option<()> {
        unimplemented!()
    }
    fn set_word(&mut self, addr: u32, value: u16) -> Option<()> {
        unimplemented!()
    }
    fn reset_instruction(&mut self) {
        unimplemented!()
    }
}

fn main() {
    let mut memory = Memory;
    let mut cpu = M68000::new_no_reset();
    cpu.regs.pc = START_ADDRESS;
    cpu.regs.sr = 0x2700.into();
    cpu.regs.d[0] = 1;  // ensure dbf tries to branch
    let cycles = cpu.interpreter(&mut memory);  // panics
    println!("Done in {cycles} cycles");
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.