Omnimaga

Calculator Community => TI Calculators => General Calculator Help => Topic started by: SirCmpwn on September 09, 2010, 07:19:08 pm

Title: How To Write an OS
Post by: SirCmpwn on September 09, 2010, 07:19:08 pm
Hello!
Several people may want to write their own OSes, but I know from experience that it is very hard finding out where to start.  I have put together a small tutorial and a basic kernel so that anyone who wants to can get started building their own OSes.

If anyone wants to skip the struggle of simply getting something to boot, I have attached KnightKernelBasic, which is what I gave to Eeems to build Rouge on.  It requires ZDS (http://www.brandonw.net/crap/zds368.exe), and I recommend using  TI Developer (http://www.sourceforge.net/projects/tidev) with it as well.

Every OS needs these, just to boot:
*Page 00
*A valid OS header

But if you want it to actually do anything useful, it needs to:
*Initialize the LCD
*Set up memory
*Initialize the stack

You should have some other stuff, but that's all you really *need.*  In order to boot and display something pretty, you really just need to have page 00, the stack, LCD, and a header.  A basic OS header looks like this:

Code: (OS) [Select]
jr Boot
   db 0,0,0,0,0,0
db 0, 0 ; rst 08h
db 0,0,0,0,0
db 0, 0   ; rst 10h
db 0,0,0,0,0
db 0,0   ; rst 18h
  db 0,0,0,0,0
db 0, 0 ; rst 20h
db 0,0,0,0,0
db 0,0   ; rst 28h
db 0,0,0,0,0
db 0,0   ; rst 30h
db 0,0,0,0,0,0,0
db 0, 0 ; rst 38h / System Interrupt
  db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
db 0,0,0,0,0,0,0,0,0
jp Boot
dw 0A55Ah
Boot:
        ; This executes at boot

You could copy this into a document and start coding at Boot: and have an OS.  This is also valid and signable, and may be sent to a calculator.  It is worth noting that you need to have an interrupt or risk crashing the calculator.  Add it at rst 38h:

Code: [Select]
; System Interrupt Routines
SysInterrupt:
exx
ex af, af'
in a, (04h)
bit 0, a
jr nz, HandleON
bit 1, a
jr nz, HandleTimer1
bit 2, a
jr nz, HandleTimer2
bit 4, a
jr nz, HandleLink
jr InterruptDone
HandleON:
in a, (03h)
res 0, a
out (03h), a
set 0, a
out (03h), a
; ON interrupt
jr InterruptDone
HandleTimer1:
in a, (03h)
res 1, a
out (03h), a
set 1, a
out (03h), a
; Timer one interrupt (might be best to merge with timer 2)
jr InterruptDone
HandleTimer2:
in a, (03h)
res 2, a
out (03h), a
set 2, a
out (03h), a
; Timer two interrupt
jr InterruptDone
HandleLink:
in a, (03h)
res 4, a
out (03h), a
set 4, a
out (03h), a
; Link interrupt
InterruptDone:
ex af, af'
exx
ei
ret

This is a basic interrupt with blank spots to put your code.  In order to actually compile and test your OS, you need to sign it.  It may also be useful to convert it to a ROM for testing.  TI Developer (http://www.sourceforge.net/projects/tidev) will do this for you automatically, but if you don't want to use ZDS (http://www.brandonw.net/crap/zds368.exe) or TI Developer, you can use os2tools (http://www.ticalc.org/pub/win/os2tools.zip).

Simple code to set up the LCD so you can draw on it:
Code: [Select]
ld a, 40h
out (10h), a
ld a, 05h
call LCDDelay
out (10h), a
ld a, 01h
call LCDDelay
out (10h), a
ld a, 3
call LCDDelay
out (10h), a
ld a, 0F6h
call LCDDelay
out (10h), a

And to set up memory (ROM 00\ROM 01\RAM 01\RAM 00):
Code: [Select]
   ld a, 1    ; Set flash page 1 in bank 1.
    out (6), a
    in a, (2)    ;get calc version
    rlca ;Roll bit 7 into carry.
    ld a, 41h ; Set ram page 1 in bank 2.
    jr nc, LowerModel
HigherModel:
    out (5),a
    ld a,81h
    out (7),a
    jr Done
LowerModel:
    out (6), a
    out (7),a
Done:

Now go build some OSes!
Title: Re: How To Write an OS
Post by: qazz42 on September 09, 2010, 07:28:11 pm
O_o woah

do we have to use ZDS? I find ZDS very confusing

(of course, I dont think I will be writing OSes very soon XD )
Title: Re: How To Write an OS
Post by: SirCmpwn on September 09, 2010, 07:29:03 pm
You don't have to use ZDS.  I just recommend it.
Title: Re: How To Write an OS
Post by: meishe91 on September 09, 2010, 07:36:52 pm
I added this to the tutorial thread. It looks great.

What are TI-Developer, ZDS, and os2tools though?
Title: Re: How To Write an OS
Post by: qazz42 on September 09, 2010, 07:38:08 pm
ah, ok

now, how would the .bin be turned into a .8xu?
Title: Re: How To Write an OS
Post by: SirCmpwn on September 09, 2010, 07:40:49 pm
TI Developer does it for you, qazz42, or you can use the programs included in os2tools.
meishe91, they are respectively, a development tool to simplify the process (written by yours truly), an IDE, and tools for signing OSes and such.
Title: Re: How To Write an OS
Post by: qazz42 on September 09, 2010, 07:46:42 pm
hmm, I will keep this in mind when I learn asm :)
Title: Re: How To Write an OS
Post by: meishe91 on September 09, 2010, 07:53:34 pm
Ah ok, so you wrote all of them?
Title: Re: How To Write an OS
Post by: DJ Omnimaga on September 09, 2010, 08:07:14 pm
He wrote TI-Developer.

Also nice tutorial Sir, I'll sticky this topic in case. I recommend people to be careful with OS projects, though. Those are ambitious projects and I do not recommend them for novice programmers.
Title: Re: How To Write an OS
Post by: calcdude84se on September 09, 2010, 08:15:48 pm
I've already posted this on Cemetech, but I'll also do it here.
[cross-post starts here]
Tutorials are always good :)
However, I recommend you use .org, unless ZDS doesn't support that.
In the end you will need to compile it. Here's my current OS build script (not that is has anything to build right now :P)
Code: [Select]
tools\brass "src\Page $00.z80"
tools\bin2hex "Page $00.bin" 20 0000 > "Page $00.hex"
tools\brass "src\Page $1D.z80"
tools\bin2hex "Page $1D.bin" 20 4000 > "Page $1D.hex"
tools\ostools-0.1\multihex 00 "Page $00.hex" 1D "Page $1D.hex" > os.hex
tools\ostools-0.1\packxxu os.hex -o os84.8xu -t 83p -q 0A -v 0.01 -h 255
tools\ostools-0.1\packxxu os.hex -o os83.8xu -t 83p -q 04 -v 0.01 -h 255
tools\rabbitsign\rabbitsign -t 8xu -k tools\keys\0A.key -K 0A -g -p -r os84.8xu
tools\rabbitsign\rabbitsign -t 8xu -k tools\keys\04.key -K 04 -g -p -r os83.8xu
del os83.8xu os84.8xu
copy src\PartesOS.h + *.inc PartesOS.h
del *.bin*
del *.hex
del PartesOS*.8xu
ren os83-signed.8xu "PartesOS 83+.8xu"
ren os84-signed.8xu "PartesOS 84+.8xu"
Note that you need OS Tools (http://www.ticalc.org/archives/files/fileinfo/350/35057.html) and RabbitSign (http://www.ticalc.org/archives/files/fileinfo/383/38392.html) (Windows version here (http://www.ticalc.org/archives/files/fileinfo/420/42035.html)). You'll also need the OS signing keys. (http://brandonw.net/calcstuff/keys.zip)
You can customize this to fit your needs (right now it's set to how my folders are set up, along with the OS name). Note that you need to set your assembler of choice to produce hex output. (Brass for some reason doesn't handle it properly, so I wrote my own bin2hex program. TASM doesn't have this problem)
The -v argument of packxxu can be changed to reflect OS version, though the other arguments should be left as is. Note that this script builds OS's for both the 83+(SE) and 84+(SE).

And yes, go build some OS's! :D
Title: Re: How To Write an OS
Post by: SirCmpwn on September 09, 2010, 09:24:22 pm
He wrote TI-Developer.

Also nice tutorial Sir, I'll sticky this topic in case. I recommend people to be careful with OS projects, though. Those are ambitious projects and I do not recommend them for novice programmers.

Thank you, and OSes are indeed not for the beginning programmer.  This is an extremely advanced tutorial.
Title: Re: How To Write an OS
Post by: DJ Omnimaga on September 09, 2010, 10:00:12 pm
ANother thing to consider is that when KOS comes out, let's not flood the OS scene with like 40 third party OSes with no compatibility between each others. With computers we already have troubles keeping up with 3 different kind of popular OSes x.x
Title: Re: How To Write an OS
Post by: jnesselr on September 09, 2010, 10:04:11 pm
How do you have the interrupt code be at 38h?
Title: Re: How To Write an OS
Post by: TC01 on September 09, 2010, 10:06:03 pm
This is potentially very useful for people who want to create an OS! I remember reading somewhere in something about the keys that there should be documentation on how to create an OS... now there is.

It's a shame that there's no documentation for building a 68k OS (that I know of, anyway), as I wanted to eventually have a go at a WFRNG OS for 68k calcs (after I actually learn 68k assembly)...
Title: Re: How To Write an OS
Post by: DJ Omnimaga on September 09, 2010, 10:08:07 pm
I wanted to eventually have a go at a WFRNG OS for 68k calcs
-.- XD

I wonder if there are still some stuff somewhere about 68K calcs that could explain it... maybe the tutorials are in French, though. Maybe you would have to ask the author of Pedrom
Title: Re: How To Write an OS
Post by: TC01 on September 09, 2010, 10:19:10 pm
Or Punix (http://sourceforge.net/projects/punix/), the other 68k OS that I just found about yesterday when reading some old ticalc.org news article (and with the assistance of a search engine).

But yeah. I didn't feel like looking through yAronet with Google Translate, especially since I don't know enough 68k assembly right now to even begin to work on an OS.
Title: Re: How To Write an OS
Post by: DJ Omnimaga on September 09, 2010, 10:22:47 pm
Wow I didn't knew about that one. Nice find.
Title: Re: How To Write an OS
Post by: TC01 on September 09, 2010, 10:29:46 pm
Wow I didn't knew about that one. Nice find.
It was in this article (http://www.ticalc.org/archives/news/articles/14/144/144591.html), and mentioned by either Kevin Kofler or Lionel Debroux when they were arguing, in particular about GCC4TI adding back Flash OS support.
Title: Re: How To Write an OS
Post by: DJ Omnimaga on September 09, 2010, 10:31:08 pm
Oh ok I see, I thought it was in an older news, specifically about the OS itself.

And typical KK stuff in the comment there x.x (although this is nothing compared to what I saw on french forums in 2004-06)
Title: Re: How To Write an OS
Post by: SirCmpwn on September 09, 2010, 11:53:12 pm
How do you have the interrupt code be at 38h?
The hardware automatically RST 38H when an interrupt fires in IM 1.

If you are already using my code from the first post, make sure you update the first block after my edit.  I had an extra zero in there by mistake.
Title: Re: How To Write an OS
Post by: Deep Toaster on September 19, 2010, 11:20:06 am
Anyone done the WFRNG yet? ;D
Title: Re: How To Write an OS
Post by: SirCmpwn on September 19, 2010, 11:43:08 am
*facepalm* :P
The kernel does include random number generation... :)
Title: Re: How To Write an OS
Post by: Deep Toaster on September 19, 2010, 11:45:33 am
*facepalm* :P
The kernel does include random number generation... :)

LOL! And the first thing anybody's gotta do as soon as KOS comes out is port a certain number-guessing game ;)
Title: Re: How To Write an OS
Post by: SirCmpwn on September 19, 2010, 11:57:32 am
Yeah, I can feel the fail now...
By the way, the random number generator is pretty good about random numbers, it updates the seed every time it executes, and the OS updates the seed on every interrupt.
Title: Re: How To Write an OS
Post by: Deep Toaster on September 19, 2010, 12:22:22 pm
Wow, that's great!

So you can't really "predict" the rands like on the TI-OS anymore...
Title: Re: How To Write an OS
Post by: DJ Omnimaga on September 19, 2010, 12:31:31 pm
It's hard to predict many on the TI-OS though, due to all possibilities but yeah it's possible, if you write numbers down or something, to do luck manipulation, like in games, by setting the seed to something specific then running the game before the boss fight.
Title: Re: How To Write an OS
Post by: SirCmpwn on September 19, 2010, 01:05:53 pm
You can predict the numbers, it's just harder; you have to factor in things like ON presses and link activity, as well as the entire time the calculator has been on.
Title: Re: How To Write an OS
Post by: Deep Toaster on September 19, 2010, 04:04:12 pm
It's hard to predict many on the TI-OS though, due to all possibilities but yeah it's possible, if you write numbers down or something, to do luck manipulation, like in games, by setting the seed to something specific then running the game before the boss fight.

Yeah, when we did a certain project in math class a few years ago, we were supposed to use dice to get our individual "stats". I convinced my teacher to let me use the num generator on an 83 instead, then stored 0 to rand and got two sixes (the max) :D
Title: Re: How To Write an OS
Post by: Builderboy on September 19, 2010, 04:29:20 pm
Doesnt the z80 have an R register that can be used for Random number generation that is much harder to calculate?  I believe Axe random numbers use something like this...
Title: Re: How To Write an OS
Post by: matthias1992 on September 19, 2010, 04:39:39 pm
Doesnt the z80 have an R register that can be used for Random number generation that is much harder to calculate?  I believe Axe random numbers use something like this...
Yes it has a R register but this isn't a random number, it's the refresh register. it could easily be used as a seed though...
Title: Re: How To Write an OS
Post by: ztrumpet on September 19, 2010, 05:01:38 pm
Nice tutorial Sir! ;D
Title: Re: How To Write an OS
Post by: DJ Omnimaga on September 19, 2010, 11:32:40 pm
I wonder what kind of randomizing Iambian uses in E:SoR...