Spin2 Interpreter
cgracey
Posts: 14,153
Here is the Spin2 interpreter. It includes the brief code that boots up the initial Spin2 instance in cog0, and even a little bytecode program to toggle P0.
The compiler just needs to initialize four longs with the startup data and then append the object blob after the interpreter. Then, you have a Spin2 application ready to load and execute.
I'm at the point now where I just need to modify the compiler to handle the latest changes I've made to the interpreter and then Spin2 should come to life in PNut.exe. Jeff is ready to fit the new Spin2 compiler into the PropellerTool.exe and make the modifications needed to the syntax highlighter.
This interpreter really packs in the functionality using XBYTE. The entire interpreter, including initialization code is 3,564 bytes. It will probably grow to 4KB once all the smart pin auxiliary bytecodes and routines are added in.
Cog registers $000.$140 are free for user PASM programs and data, while $1E0..$1EF serve as the RESULT/parameters/locals conduit for inline assembly and cog/hub calls. Auxiliary bytecodes can be added without any impact on cog registers.
In this example, a 250MHz clock is selected and the following program loops at 3.906MHz:
This file should assemble and run on the current P2 Eval board. (I say "should" because I'm not sure if I've used any operators that might not be in the last release of PNut.exe.)
The compiler just needs to initialize four longs with the startup data and then append the object blob after the interpreter. Then, you have a Spin2 application ready to load and execute.
I'm at the point now where I just need to modify the compiler to handle the latest changes I've made to the interpreter and then Spin2 should come to life in PNut.exe. Jeff is ready to fit the new Spin2 compiler into the PropellerTool.exe and make the modifications needed to the syntax highlighter.
This interpreter really packs in the functionality using XBYTE. The entire interpreter, including initialization code is 3,564 bytes. It will probably grow to 4KB once all the smart pin auxiliary bytecodes and routines are added in.
Cog registers $000.$140 are free for user PASM programs and data, while $1E0..$1EF serve as the RESULT/parameters/locals conduit for inline assembly and cog/hub calls. Auxiliary bytecodes can be added without any impact on cog registers.
In this example, a 250MHz clock is selected and the following program loops at 3.906MHz:
REPEAT PINNOT(0)
This file should assemble and run on the current P2 Eval board. (I say "should" because I'm not sure if I've used any operators that might not be in the last release of PNut.exe.)
Comments
Best I got out of TAQOZ was about 2.36MHz using 48 PIN 0 FOR T NEXT which in this case NEXT is counting down to zero from zero and toggling the selected pin with T.
It would be really nice if the Spin2 compiler could produce a listing though.
CORRECTION: the pin outputs 2.23MHz but the toggle rate is 4.46MHz
Yes, a listing will happen at some point.
I just posted a correction but the scope doesn't always show the correct frequency however I measured 56 cycles per loop So that brings it to a toggle rate of 4.464MHz @250MHz
Actually, I can get that SAME rate if the bytecodes are on copacetic boundaries for the the RDFAST.
I just found that putting in four extra bytecodes before the loop get me to 4.464MHz toggle rate.
Luv it
These are the sections of code that get executed in TAQOZ btw. edit: added doCALL line and aligned
Except that now I will have to update Catalina and re-do all the C/Spin interoperability ... and just when I thought I was nearly finished!
Is there a definition of the updated Spin2 language? Or is this the original Spin?
Here are the XBYTE code snippets that execute to make the loop (6-clock overhead per bytecode):
It's the pusha and popa that drag things down, mostly.
I don't have a definition, yet, but I'll get one together as soon as things are working well.
Does that mean speed will bounce around, depending on code alignment ?
Is there an ALIGN mechanism to prevent that from occurring ?
I don't even know what the rule for optimal alignment would be. It would take some experimenting and reasoning. If we figure it out, though, it would really kill performance to start adding filler bytecodes to get some things to loop fast.
It would be impossible to compile-time align run-time things like stack pointers.
I'm finishing off my debugger now, and this will be a nice test of the XBYTE tracing.
Great. Man, remember how tricky it was to get everything working right on the FPGA? Hopefully, there are no silicon bugs lurking.
A completely off topic question: Is there any conditional compile in Pnut v32i? eg: "if const < 10" sort of thing?
In the P2 ZPU interpreter I had two XBYTE tables, essentially one for "top of stack in register" and one for "top two stack items in registers". Push opcodes would transition from the one register state to the two register one (and if already in two would have to hit memory). Math opcodes would typically consume the top two registers and transition back to one item in register. It did add book-keeping complexity, but the ZPU instruction set is pretty simple so it all fit.
My JIT compiler toolkit has a stack optimizer that lets you keep up to N stack items in registers. That relies on knowledge of where jumps go so we don't ever jump into the middle of a sequence (and every time there's a branch the stack state has to be reset).
Are you really going to get PropTool updated with Spin2???
I know it just works, but it it lacks even the basic of features like conditional compile, etc. Surely we can give it a miss and move on to something better??? There is Eric's fastspin. We can use Visual Studio Code to edit which is open source and cross platform. Then there is OpenSpin which Roy is likely to update.
Not yet. I recognize the need to add that functionality, though.
Thanks for those explanations, Eric. That gives me something to think about.
It is something that Parallax wrote and has complete knowledge of. So, that is our primary platform.
Parallax simply must make a new cross platform editor or endorse/use one of the existing ones from the community.
One step at a time. PropellerTool is a nice step up from PNut, is it not?
That could be done, but we are days away from having PropellerTool working. New chips will be here in a month. I want Spin2 to be in a proven state by then. We could Implement Spin2 in many different ways, going forward. I just need something that works ASAP - something that will let me prove the interpreter and get syntax issues sorted out. It's going to be a very iterative process.
That's understandable.
Good. Very important to have a listing. I sort of found that for P1 (until I started to use BST) without a listing file it was often very difficult to understand how exactly the 32kB of memory was layed out without a fair bit of reverse engineering and head scratching. For P2, the memory might not be as scarce to need to try to reuse every spare block like I often needed to for P1 but the listing is still important when integrating with other languages etc, and it can be useful for code debug as well.
PropTool is written in Delphi and uses some closed source libs or something. Porting it out to a reasonable language would be kind of silly. The better path to take would be to make something new from scratch using Qt. You can get a reasonable IDE up and running in Qt in a couple days, I've done the basics a couple times but then gave up because it was easier to just use VS Code or notepad++ for myself.
There needs to be an open source cross platform IDE/compiler that is official, and it needs to be available at launch.