#pragma once //license: GPLv3 //started: 2011-09-05 #include #include namespace Famicom { namespace Info { static const uint SerializerVersion = 3; } } #include namespace Famicom { struct Thread { ~Thread() { if(thread) co_delete(thread); } auto create(auto (*entrypoint)() -> void, uint frequency) -> void { if(thread) co_delete(thread); thread = co_create(65'536 * sizeof(void*), entrypoint); this->frequency = frequency; clock = 0; } auto serialize(serializer& s) -> void { s.integer(frequency); s.integer(clock); } cothread_t thread = nullptr; uint frequency = 0; int64 clock = 0; }; struct Cothread : Thread { auto step(uint clocks) -> void; auto synchronizeCPU() -> void; }; #include #include #include #include #include #include #include #include #include inline auto Cothread::step(uint clocks) -> void { clock += clocks * (uint64)cpu.frequency; synchronizeCPU(); } inline auto Cothread::synchronizeCPU() -> void { if(clock >= 0 && !scheduler.synchronizing()) co_switch(cpu.thread); } } #include