This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Admicos's profile picture

The early stages of my Computercraft emulator

Started by Admicos, 30 August 2016 - 12:22 AM
Admicos #1
Posted 30 August 2016 - 02:22 AM
http://youtu.be/PJ7WxTBamws

Currently it cannot read keys through the screen, not all APIs are implemented, and it doesn't use the CC font due to a bug (and if you think you know how to fix it, the associated code (and the place i prefer your solution) is here)
It's basically just a lua output window.

Should i put it on GitHub or is it too early yet?
Admicos #2
Posted 30 August 2016 - 03:40 AM



This error is produced from the computercraft's own bios.lua.
Making progress!
Admicos #3
Posted 30 August 2016 - 09:48 AM
The emulator now has a GitHub page: https://github.com/Admicos/CCPY
If you want to help, go right there!
Admicos #4
Posted 30 August 2016 - 11:15 AM

This error happens at rom/apis/colours Line 3 and is the only error currently blocking progress. It thinks colors is a "userdata" when it's a table. Any help appriciated

EDIT: Also a error at rom/apis/term line 45, but that's due to how native is (NOT) implemented. A fix for that would be appriciated too..
Edited on 30 August 2016 - 09:20 AM
CrazedProgrammer #5
Posted 31 August 2016 - 09:34 AM
Are you sure you're passing a Lua table and not a custom wrapper object?
Admicos #6
Posted 31 August 2016 - 09:36 AM
Are you sure you're passing a Lua table and not a custom wrapper object?

I AM actually passing a custom class, why didn't i think of remaking it in Lua. (sighs)

I guess programming without sleep REALLY doesn't work.

EDIT: That actually worked!
Edited on 31 August 2016 - 07:47 AM
CrazedProgrammer #7
Posted 01 September 2016 - 12:41 AM
No problem, glad I could help!
ebernerd #8
Posted 01 September 2016 - 06:49 AM
I love CC emulators, I'm excited to see how yours does!
Admicos #9
Posted 01 September 2016 - 08:58 AM
I think i fixed the term.native error, but i am now stuck with "attempt to yield across C-Call boundary" which i have absolutely no idea how to fix.

EDIT: It might be due to a function that isn't implemented as bios.lua 178 (where the error probably happens) is os.pullEventRaw, which isn't implemented.
EDIT 2: Confirmed, as implementing it will change the error.
EDIT 3: Getting the same error again. os.pullEventRaw is implemented(-ish as the filter currently doesn't work)
EDIT 4: There are a error i couldn't see. Let's see what it is
EDIT 6: It seems like it can't iterate over "native" in the term API. I think i know why though
EDIT 7: Does someone know how to send Python classes in Lua as modifiable, because i think that's the issue (probably not, and it's probably modifiable by default.)
Edited on 01 September 2016 - 07:30 AM
Bomb Bloke #10
Posted 02 September 2016 - 06:19 AM
As an educated guess, it sounds like you're trying to run bios.lua directly, as opposed to running it within a Lua coroutine. CC sticks it within one so that the one VM can run one copy for each active system in the world - your emulator will presumably only handle one system at a time, but bios.lua expects to be running within a coroutine regardless.

In psuedocode (you may not even be using Lua for this directly):

local myCoroutine = coroutine.create(loadfile("bios.lua"))
local ok, requestedEvent = coroutine.resume(myCoroutine)

while coroutine.status(myCoroutine) ~= "dead" do
        -- Generate an event, put its parameters into a table called eg "myEvent".
        -- If you have nothing to generate an event with (eg the user hasn't typed anything, no timers have expired, yadda yadda,
        --    ... then pause here until you do.

        if not requestedEvent or requestedEvent == myEvent[1] then
        	ok, requestedEvent = coroutine.resume(myCoroutine, unpack(myEvent))
        end
end

-- End emulation, system has stopped.

Assuming you're already familiar with coroutines, it may still be worth reading chapter four onwards of this. If it doesn't make sense, then you may instead be better off reading the whole thing.
Admicos #11
Posted 02 September 2016 - 08:38 AM
As an educated guess, it sounds like you're trying to run bios.lua directly, as opposed to running it within a Lua coroutine. CC sticks it within one so that the one VM can run one copy for each active system in the world - your emulator will presumably only handle one system at a time, but bios.lua expects to be running within a coroutine regardless.

In psuedocode (you may not even be using Lua for this directly):

local myCoroutine = coroutine.create(loadfile("bios.lua"))
local ok, requestedEvent = coroutine.resume(myCoroutine)

while coroutine.status(myCoroutine) ~= "dead" do
		-- Generate an event, put its parameters into a table called eg "myEvent".
		-- If you have nothing to generate an event with (eg the user hasn't typed anything, no timers have expired, yadda yadda,
		--	... then pause here until you do.

		if not requestedEvent or requestedEvent == myEvent[1] then
			ok, requestedEvent = coroutine.resume(myCoroutine, unpack(myEvent))
		end
end

-- End emulation, system has stopped.

Assuming you're already familiar with coroutines, it may still be worth reading chapter four onwards of this. If it doesn't make sense, then you may instead be better off reading the whole thing.

Thanks for the info.

EDIT: If my memory is correct, then this just fixed a error, i think. (there is still one though, i think i have an idea)
Edited on 02 September 2016 - 07:03 AM
Admicos #12
Posted 02 September 2016 - 09:26 AM
I can (posssibly) run the shell now as the error suggests that it tries to open the shell.

Now let's tune the fs api a bit and try now.
Admicos #13
Posted 02 September 2016 - 10:46 AM
guys?



Make a filler RS api and possibly no errors!

EDIT: No errors on the shell, yay! Now let's try events.
EDIT 2: Events are now causing the c-call boundary error, i'm gonna try some more stuff.
EDIT 3: Also, the events don't register either. Let me try a different method.
EDIT 4: Events now register properly, now let't try the cc part.
EDIT 5: I made the events all in Lua, to hopefully be more compatible. But still no CC reaction.
EDIT 6: I just saw that bios.lua implements its own pullEventRaw that is just a coroutine.yield. *facepalming intensifies*
Edited on 02 September 2016 - 02:32 PM
Admicos #14
Posted 04 September 2016 - 04:33 PM
http://www.youtube.com/watch?v=pBB6HeSwbIs

I think i just got a modified bios to get the events, but they are all nil, gotta figure this out.
Edited on 04 September 2016 - 02:42 PM
Bomb Bloke #15
Posted 05 September 2016 - 02:00 AM
My experience with Python is minimal, but if I'm reading things correctly, your code may incorrectly resume the computer's coroutine here - this "update" method gets called every time the loop here notices an event, but not every event pygame.event.wait() collects is added to the CC system's queue.

You should not resume CC's coroutine if you have no relevant events to pass in, and you should furthermore discard any events which mismatch a set event filter (other than "terminate").
ashnwill #16
Posted 09 October 2016 - 05:09 AM
I don't know if you're still working on this, but the computercraft font has all the letters centered in their little rectangle in the terminal's grid rather than leaning to the left
Admicos #17
Posted 09 October 2016 - 04:20 PM
I don't know if you're still working on this, but the computercraft font has all the letters centered in their little rectangle in the terminal's grid rather than leaning to the left
i know and i'm trying to fix it, and i kind of stopped it for a month or so, will try to make it work once github gets unblocked :(/>
LDDestroier #18
Posted 20 October 2016 - 12:47 PM
i know and i'm trying to fix it, and i kind of stopped it for a month or so, will try to make it work once github gets unblocked :(/>

Damn, man. It must be a horrible time to live in Turkey, huh? Can you access the Tor project?
Admicos #19
Posted 20 October 2016 - 05:36 PM
i know and i'm trying to fix it, and i kind of stopped it for a month or so, will try to make it work once github gets unblocked :(/>

Damn, man. It must be a horrible time to live in Turkey, huh? Can you access the Tor project?

It's unblocked already and yes i can access Tor.
Piorjade #20
Posted 21 October 2016 - 08:08 PM
Will we be able to use custom CC versions with this?
Or does it implement CC?
Admicos #21
Posted 21 October 2016 - 09:28 PM
Will we be able to use custom CC versions with this?
Or does it implement CC?
It "implements" cc, so nothing to do with the original jar other than the bios files, but it's still incomplete (just the events missing, but i'll try to fix it later)
Admicos #22
Posted 22 March 2017 - 12:14 PM
[media]http://www.youtube.com/watch?v=CfeX1PBqEPs[/media]

So, i got events working to the point where you can type into the shell, but they are still buggy and most events do not exist.
And due to LWJGL and Pygame having different key codes, most non-letter keys do not work. If they worked, the cc shell would probably run without problems though.

EDIT: So, i made the events work faster by making the event queue return the last item instead of the first item. But i still need to implement proper key mapping and the rest of the events
Edited on 22 March 2017 - 06:32 PM
Admicos #23
Posted 23 March 2017 - 06:57 PM
The current state of the emulator:
[media]http://www.youtube.com/watch?v=Ig_S8Bh99G8[/media]

Considering the emulator couldn't even handle keypresses yesterday, this is some good progress right here!
Edited on 23 March 2017 - 05:58 PM
Admicos #24
Posted 18 May 2017 - 08:33 AM
Pretty late update: I changed the renderer from using TTF fonts to using CC's term_font.png file. So the current rendering is pretty accurate i'd say.



Now I need to do key combinations, but I think that will need seperate files for each keyboard layout unless I can find a way to do it easier.
MineRobber___T #25
Posted 26 May 2017 - 02:13 AM
Pretty late update: I changed the renderer from using TTF fonts to using CC's term_font.png file. So the current rendering is pretty accurate i'd say.



Now I need to do key combinations, but I think that will need seperate files for each keyboard layout unless I can find a way to do it easier.

If I'm not mistaken, you just need to keep track of keydown and keyup events. on a keydown event where a status key (shift,ctrl,alt,etc.) is pressed, set a corresponding bool and when they are released, set the bool to false. If I knew where input was being handled, I could probably submit a PR to do this myself.

EDIT: using a test program, I found the key events for modifier keys:

Control key = 29
Alt key = 56
Shift key = 42

if you queue said events, programs should run without a hitch.
Edited on 26 May 2017 - 12:23 AM
Admicos #26
Posted 08 June 2018 - 06:17 AM
So, after almost a year of not doing anything with this, I decided to rewrite it. I managed to get this up to about the same level of functionality in about 4 days using some copy-paste and generally knowing how to handle stuff properly. Now the code is MUCH cleaner and it also has some tests too. (No idea how to test anything internal, so I just tested the APIs exposed to CC, except FS because I know that's broken. Gotta fix that someday)

Also, HTTP support:


The new code is over at https://gitlab.com/admicos/ccpy if anyone's interested.
Admicos #27
Posted 09 June 2018 - 09:40 PM
I managed to split the emulator's display and event handling to different "Drivers". This makes cleaner code and has the possibility to allow fun stuff like running the emulator totally headless, displaying on a CLI or simply using a different renderer if pygame doesn't work for some reason.
Admicos #28
Posted 13 June 2018 - 04:43 AM
I re-wrote the path sanitizer, so most fs bugs should (I hope) be fixed. I also added tests to the fs API so the emulator's at 90% coverage now. (Doesn't mean much but I like seeing nice numbers)

The emulator should be pretty usable (assuming there aren't any other bugs or other stuff I might've missed) when I fix the key handling.
Admicos #29
Posted 14 June 2018 - 08:03 PM
Finally! Key events are fixed and should work pretty well!

Now that that's out of the way, I might actually consider using this for anything other than testing if it works or not.