0

I'm working on simple 8086 simulator. But I'm not sure how to implement lock prefix. I know this prefix used in semaphore implementations.

1) For now I have only 8086 core and no other cores like FPU. Manual says lock prefix used in multiprocessor environment. So how treat lock prefix with 8086 alone.

2) How to treat lock prefix if exists other processors like FPU.

Any patterns and suggestion how to implement lock prefix?

7
  • So I need implement semaphores in my C code to protect memory?
    – kesrut
    Sep 27, 2016 at 6:30
  • 2
    You're simulating hardware. You need to lock your simulated bus to prevent any other simulated cores from interrupting you in the middle of an atomic test and set of shared memory. Sep 27, 2016 at 6:34
  • @CandiedOrange I think this applies to single core too ? O I'm wrong?
    – kesrut
    Sep 27, 2016 at 7:06
  • @kerut you're defending against anything that can mutate (change) values in memory. if ANYTHING ELSE in your simulation can do that when your 8086 is not looking then you need your simulated lock to actually do something. Sep 27, 2016 at 7:09

1 Answer 1

3

So how treat lock prefix with 8086 alone.

ignore it.

How to treat lock prefix if exists other processors like FPU.

  1. Introduce a "Memory Lock" flag, visible by all hardware in the simulation environment.

  2. Before any memory access operation, by any piece of hardware in the simulation environment, check the "Memory Lock" flag: if it is set, then skip this clock cycle, try again on the next clock cycle.

  3. If the lock prefix is present on an instruction, (and if the memory lock flag has not already been set,) then set the "Memory Lock" flag, execute the instruction without obeying the "Memory Lock" flag, and when the instruction is complete, clear the "Memory Lock" flag.

If you are running all your hardware in lock-step fashion, (meaning that on each clock cycle you tick each device, and when all devices have ticked, you proceed to the next clock cycle,) then that's all you need to do.

If you are running your hardware in parallel, (meaning that on each clock cycle you simultaneously tick all available hardware on different threads,) then you may need to implement the "Memory Lock" flag as a mutex, so as to prevent devices from accidentally overwriting it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.