4 ms·
I am not sure you can not use `unsafe` in an embedded type of project. `unsafe` isn't necessarily bad, but when used at least makes it very clear where extra a
by bionicbits 8y ago
I am not sure you can not use `unsafe` in an embedded type of project. `unsafe` isn't necessarily bad, but when used at least makes it very clear where extra attention should be paid.
- nukeop 8y agoI also wrote a NES emulator in Rust, and managed to do it without using a single unsafe block. I used SDL bindings for graphics, if that counts, but it can also run in headless mode (for testing) without SDL perfectly well.
- neurotrace 8y agoDo you have a link? I'd like to see how yours compares with OPs
- hopler 8y agoEmulating a system of a few 2Mhz with a 4x3Ghz chipset ought to be doable using managed memory.
- flohofwoe 8y agoNitpicking a bit, but there's not a lot of useful things that could be multithreaded in an 8-bit emulator, so it would be more like 1x3Ghz ;) In those old 8- and 16-bit machines all components usually ran completely synchronous for each clock tick, for instance if the video emulation runs out of sync with the CPU emulation for a tick or two, a write from the CPU to a video hardware register might not make it in time and you get garbage video output. Modern asynchronous systems may need less (relative) host system performance for emulation, because the timing requirements between the different hardware components are much more relaxed.
- jrockway 8y agoThe original hardware was multithreaded... in the sense that there were several separate processing units.
- flohofwoe 8y agoYes, but as far as I can glance from the NES spec, those still run from the same clock (with different dividers, but they're still locked to each other). I haven't written an emulator for the NES yet, but on machines like the Amstrad CPC or C64, the CPU can reprogram video hardware registers (like color palette entries) at any time, for instance in the middle of a scanline. When this is off by a tick, you get the color palette change a couple of pixels late or early. If CPU and video chip emulation would run on different threads, they would need to sync with each other a few million times per second, that doesn't sound like a good idea. If synchronization is only needed once per scanline, or even per frame that's an option of course. Often this is good enough to run some games which didn't go too close to the metal (again this is only from my experience with home computer emulators, haven't done NES stuff yet). Parallelizing through SIMD might be an option though (one can pack a lot of 4- or 8-bit counters into a 512-bit register).
- boomlinde 8y agoYes, but their emulation needs to be performed in lockstep, normally per clock tick or at least externally visible state change, to accurately model their interaction with respect to timing. Threading in that case would likely mean several orders of magnitude of performance degradation.
- simias 8y agoWriting an emulator is not really embedded programming (the target often qualifies as an embedded environment but the host can be any random desktop computer). It's definitely possible to write an emulator without unsafe code. Here's a toy gameboy emulator I wrote a while ago in Rust without any unsafe code: https://github.com/simias/gb-rs https://github.com/simias/gb-rs Here's a very incomplete PSX emulator with only a single unsafe (which might not even be necessary anymore, I haven't updated the code in a while): https://github.com/simias/rustation https://github.com/simias/rustation
- mzs 8y agoAre you sure about that? https://github.com/simias/gb-rs/blob/master/Cargo.toml#L29 https://github.com/simias/gb-rs/blob/master/Cargo.toml#L29
- simias 8y agoThe SDL2 dependency? I don't understand what you mean by that. Or are you saying that a dependency that I use happens to use unsafe code? In which case it's true but then by that definition it's effectively almost impossible to write 100% safe rust since the stdlib itself contains a non-negligible amount of unsafe code.
- steveklabnik 8y agoIt’s always impossible, as you have to call into the OS at some point to do anything of use.