好了,所以这里是我要怎样做。
// bus controls reads and writes to/from emulated cpu
// a trait allows me to define the interface the cpu expects
pub trait Bus {
fn read(&self, size: Size, address: u32) -> u32;
fn write(&self, size: Size, address: u32, data: u32);
}
// must have an object to implement the bus on
pub struct Mem {} // this can be filled out later
// implement Bus trait for our Mem
impl Bus for Mem {
fn read(&self, size: Size, address: u32) -> u32 { unimplemented!() }
fn write(&self, size: Size, address: u32, data: u32) { unimplemented!() }
}
// here is our cpu struct with a generic Bus trait object in it
pub struct M68K<A: Bus> {
pub d: [u32; 8],
pub a: [u32; 8],
pub x: bool, // extend
pub n: bool, // negative
pub z: bool, // zero
pub v: bool, // overflow
pub c: bool, // carry
pub bus: A // here is the Bus trait object
}
// when we implement our cpu we can use the Bus trait methods, this allows
// multiple cpu's to be created with the same or different Bus objects.
// possibly connect 2 cpu's to the same Bus object?
impl M68K<Mem> {
pub fn step(&self, cycles: u32) {
let x = self.bus.read(Size::Byte, 0xffff);
}
}
此代码尚未完成。我的主要目标是为我自己的项目创建一个易于使用的68k cpu仿真器。在这里,我将所有内容显示在一个文件中,但实际上整个cpu模拟器现在可以在没有任何有关总线实现的知识的情况下编写。我希望这是有道理的,它对我有用,我再次享受着鲁斯特!
这是**高度建议**,你阅读**优秀** [*锈编程语言*](https://doc.rust-lang.org/stable/book/second-edition/)其中涵盖了很多这些介绍性问题。这在[特性章节](https://doc.rust-lang.org/stable/book/second-edition/ch10-02-traits.html)中有介绍。 – Shepmaster
我读过这本书的第一版,没有意识到有第二版,我必须阅读它,因为Rust自从我上次使用它之后发生了一些变化。我想我的主要问题是从基于继承的语言切换到Rust的基于合成的设计。 –