Author Topic: How To Write an OS  (Read 30026 times)

0 Members and 1 Guest are viewing this topic.

SirCmpwn

  • Guest
How To Write an OS
« 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, and I recommend using  TI Developer 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 will do this for you automatically, but if you don't want to use ZDS or TI Developer, you can use os2tools.

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!
« Last Edit: September 10, 2010, 12:00:59 am by SirCmpwn »

Offline qazz42

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1130
  • Rating: +30/-12
  • hiiiiiiiii
    • View Profile
Re: How To Write an OS
« Reply #1 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 )

SirCmpwn

  • Guest
Re: How To Write an OS
« Reply #2 on: September 09, 2010, 07:29:03 pm »
You don't have to use ZDS.  I just recommend it.

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: How To Write an OS
« Reply #3 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?
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline qazz42

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1130
  • Rating: +30/-12
  • hiiiiiiiii
    • View Profile
Re: How To Write an OS
« Reply #4 on: September 09, 2010, 07:38:08 pm »
ah, ok

now, how would the .bin be turned into a .8xu?

SirCmpwn

  • Guest
Re: How To Write an OS
« Reply #5 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.

Offline qazz42

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1130
  • Rating: +30/-12
  • hiiiiiiiii
    • View Profile
Re: How To Write an OS
« Reply #6 on: September 09, 2010, 07:46:42 pm »
hmm, I will keep this in mind when I learn asm :)

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: How To Write an OS
« Reply #7 on: September 09, 2010, 07:53:34 pm »
Ah ok, so you wrote all of them?
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline DJ Omnimaga

  • Former TI programmer
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55876
  • Rating: +3151/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • DJ Omnimaga Music
Re: How To Write an OS
« Reply #8 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.

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: How To Write an OS
« Reply #9 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 and RabbitSign (Windows version here). You'll also need the OS signing keys.
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
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

SirCmpwn

  • Guest
Re: How To Write an OS
« Reply #10 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.

Offline DJ Omnimaga

  • Former TI programmer
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55876
  • Rating: +3151/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • DJ Omnimaga Music
Re: How To Write an OS
« Reply #11 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

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: How To Write an OS
« Reply #12 on: September 09, 2010, 10:04:11 pm »
How do you have the interrupt code be at 38h?

Offline TC01

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 344
  • Rating: +9/-0
    • View Profile
Re: How To Write an OS
« Reply #13 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)...



The userbars in my sig are links embedded links.

And in addition to calculator (and Python!) stuff, I mod Civilization 4 (frequently with Python).

Offline DJ Omnimaga

  • Former TI programmer
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55876
  • Rating: +3151/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • DJ Omnimaga Music
Re: How To Write an OS
« Reply #14 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