Discussion:
modula-2 kernel programming
(too old to reply)
Arnauld
2011-04-18 15:21:30 UTC
Permalink
Hi,

I'm a C / asm programer and I'm very interesting in switching to
modula-2 language as I'm a bit tired with bound checking, *overflows,
etc.. As far as I understood, we don't write in modula-2 like we write
in C. The inner philosophy is very different in m2. So, I looked for a
kernel wrote in modula-2 to have an example of how to address hardware
and very low level code in m2. But I found nothing....

So, I'd like to know if :
1/ modula-2 really suit well for writing a tiny kernel
2/ there's a simple example for i386 or amd64 plateform.


Here is a more concrete example.
I'd like to write on my video memory that begin in 0xB8A00. There are
25*80 characters on the terminal and each character is defined with 2
bytes : 1 byte for the character per se, and another byte for the
character attributes (color, bleep, etc.). So, for example, if I want
to write the letter 'A' in red in the upper left corner of my screen :
byte 0xB8A00 := 'A';
byte 0xB8A01 := 4; (* 4 encodes red color *)
How can I do this in m2 ?

Best regards,
Bartholomew
2011-04-18 16:44:25 UTC
Permalink
On 04/18/2011 11:21 AM, Arnauld wrote (in part):
[...]
Post by Arnauld
1/ modula-2 really suit well for writing a tiny kernel
2/ there's a simple example for i386 or amd64 plateform.
Here is a more concrete example.
I'd like to write on my video memory that begin in 0xB8A00.
[...]
Post by Arnauld
How can I do this in m2 ?
Much will depend on the compiler/platform/OS you use.

Peter Moylan has a collection at
http://www.pmoylan.org/pages/m2/PMOS/PMOS.html for x86.
Arnauld
2011-04-18 17:38:14 UTC
Permalink
Post by Bartholomew
Much will depend on the compiler/platform/OS you use.
Gnu modula-2 (gm2) on Debian.
Post by Bartholomew
Peter Moylan has a collection athttp://www.pmoylan.org/pages/m2/PMOS/PMOS.htmlfor x86.
Ok, thank you very much, it seems there's lot of interesting stuff in
it :-)

However, everyting is made on dos platform... any idea how to link
with gm2 compiler (on Linux) ?
Pascal J. Bourguignon
2011-04-18 18:17:44 UTC
Permalink
Post by Arnauld
Hi,
I'm a C / asm programer and I'm very interesting in switching to
modula-2 language as I'm a bit tired with bound checking, *overflows,
etc.. As far as I understood, we don't write in modula-2 like we write
in C. The inner philosophy is very different in m2. So, I looked for a
kernel wrote in modula-2 to have an example of how to address hardware
and very low level code in m2. But I found nothing....
1/ modula-2 really suit well for writing a tiny kernel
It does. Even to write a big kernel.
Post by Arnauld
2/ there's a simple example for i386 or amd64 plateform.
The most famous example will Lilith:
http://www.cfbsoftware.com/modula2/
http://www.bitsavers.org/pdf/eth/lilith/
http://e-collection.ethbib.ethz.ch/eserv/eth:21975/eth-21975-01.pdf


But then again, all this was developed before the Internet explosion,
andd it might be indeed somewhat difficult to find the sources.

But it really is not so complex to write a simple kernel.
Post by Arnauld
Here is a more concrete example.
I'd like to write on my video memory that begin in 0xB8A00. There are
25*80 characters on the terminal and each character is defined with 2
bytes : 1 byte for the character per se, and another byte for the
character attributes (color, bleep, etc.). So, for example, if I want
byte 0xB8A00 := 'A';
byte 0xB8A01 := 4; (* 4 encodes red color *)
How can I do this in m2 ?
I would do something like:

PROCEDURE example;
TYPE byte:[0..255];
VAR buffer:POINTER TO ARRAY[0..24],[0..79] OF byte;
BEGIN
buffer:=SYSTEM.MAKEADR(0B8A00H);
buffer^[0,0]:=SYSTEM.CAST(byte,'A');
buffer^[0,1]:=4;
END example;

(no guarantee, the last Modula-2 program I wrote was 20 years ago).


Nowadays, you could also consider Modula-3 to write an OS.
I would do it in Common Lisp.
--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.
Rugxulo
2011-04-19 01:10:05 UTC
Permalink
Hi,
Post by Pascal J. Bourguignon
Post by Arnauld
I'm a C / asm programer and I'm very interesting in switching to
modula-2 language as I'm a bit tired with bound checking, *overflows,
There are C compilers that have bounds checking, e.g. TinyC (IIRC) or
maybe even old GCC (3rd-party patches), possibly even EMX's last
version. Honestly, I think it was somehow claimed as patented (!)
somewhere.

Long story short, don't want to derail your plans, Modula-2 is nice
(though I'm a noob), preferable (IMO) to C, but I'm just giving you
your options.
Post by Pascal J. Bourguignon
Post by Arnauld
etc.. As far as I understood, we don't write in modula-2 like we write
in C. The inner philosophy is very different in m2. So, I looked for a
kernel wrote in modula-2 to have an example of how to address hardware
and very low level code in m2. But I found nothing....
1/ modula-2 really suit well for writing a tiny kernel
It does. Even to write a big kernel.
Haven't ever looked at it, but check here (LUMOS-2):

http://www.uranus.ru/download.html
Post by Pascal J. Bourguignon
Post by Arnauld
2/ there's a simple example for i386 or amd64 plateform.
You may honestly have better luck investigating the Oberon System(s).
Post by Pascal J. Bourguignon
Post by Arnauld
Here is a more concrete example.
I'd like to write on my video memory that begin in 0xB8A00. There are
25*80 characters on the terminal and each character is defined with 2
bytes : 1 byte for the character per se, and another byte for the
character attributes (color, bleep, etc.). So, for example, if I want
  byte 0xB8A00 := 'A';
  byte 0xB8A01 := 4; (* 4 encodes red color *)
How can I do this in m2 ?
On Linux?? No idea, but I'm sure somebody knows. Ask on the gm2
mailing list. Gaius is definitely pro-Linux.
Post by Pascal J. Bourguignon
Nowadays, you could also consider Modula-3 to write an OS.  
I would do it in Common Lisp.
No offense, seriously, but Modula-3 is almost deader than Modula-2. :-
( It's a nice language, but it's much less popular (though CM3 does
still work on Linux). But yeah, SpinOS was written in it. You can find
sources online, but good luck recompiling it! :-/
Jos Dreesen
2011-04-19 17:49:51 UTC
Permalink
Post by Pascal J. Bourguignon
http://www.cfbsoftware.com/modula2/
http://www.bitsavers.org/pdf/eth/lilith/
http://e-collection.ethbib.ethz.ch/eserv/eth:21975/eth-21975-01.pdf
But then again, all this was developed before the Internet explosion,
andd it might be indeed somewhat difficult to find the sources.
Not at all, find the Medos (Lilith OS) sources here :

ftp://jdreesen.dyndns.org/ftp/Files/

Emulith, the Lilith Emulator can be found here

ftp://jdreesen.dyndns.org/ftp/Emulith

Enjoy, Jos
Pascal J. Bourguignon
2011-04-19 18:01:35 UTC
Permalink
Post by Jos Dreesen
Post by Pascal J. Bourguignon
http://www.cfbsoftware.com/modula2/
http://www.bitsavers.org/pdf/eth/lilith/
http://e-collection.ethbib.ethz.ch/eserv/eth:21975/eth-21975-01.pdf
But then again, all this was developed before the Internet explosion,
andd it might be indeed somewhat difficult to find the sources.
My google-foo failed me this time...
Post by Jos Dreesen
ftp://jdreesen.dyndns.org/ftp/Files/
Emulith, the Lilith Emulator can be found here
ftp://jdreesen.dyndns.org/ftp/Emulith
Thanks!
--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.
Christoph Schlegel
2011-04-18 19:58:06 UTC
Permalink
Post by Arnauld
Hi,
I'm a C / asm programer and I'm very interesting in switching to
modula-2 language as I'm a bit tired with bound checking, *overflows,
etc.. As far as I understood, we don't write in modula-2 like we write
in C. The inner philosophy is very different in m2. So, I looked for a
kernel wrote in modula-2 to have an example of how to address hardware
and very low level code in m2. But I found nothing....
1/ modula-2 really suit well for writing a tiny kernel
2/ there's a simple example for i386 or amd64 plateform.
Here is a more concrete example.
I'd like to write on my video memory that begin in 0xB8A00. There are
25*80 characters on the terminal and each character is defined with 2
bytes : 1 byte for the character per se, and another byte for the
character attributes (color, bleep, etc.). So, for example, if I want
byte 0xB8A00 := 'A';
byte 0xB8A01 := 4; (* 4 encodes red color *)
How can I do this in m2 ?
Best regards,
Some other starting points:

http://freepages.modula2.org/m2faq.html#faq3
Marco van de Voort
2011-04-19 08:50:51 UTC
Permalink
Post by Arnauld
Here is a more concrete example.
I'd like to write on my video memory that begin in 0xB8A00. There are
25*80 characters on the terminal and each character is defined with 2
bytes : 1 byte for the character per se, and another byte for the
character attributes (color, bleep, etc.). So, for example, if I want
byte 0xB8A00 := 'A';
byte 0xB8A01 := 4; (* 4 encodes red color *)
How can I do this in m2 ?
That smells like Dos, and not like kernel programming, but making programs
on top of dos. And that means you need compilers with specific dos quirks

Most 32-bit compilers won't let you do this that easily. Using (very) old
16-bits compilers will probably be easier.
Arnauld
2011-04-19 09:22:31 UTC
Permalink
Post by Marco van de Voort
That smells like Dos, and not like kernel programming, but making programs
on top of dos.  And that means you need compilers with specific dos quirks
It was just for an example.
Anyway, it's not dos related but x86/amd64 arch related. On these
harwares, video memory (in text mode) is mapped on ram.

I already made a baby kernel for x86. It runs on protected mode, with
multi-tasking, paging memory, preemptible kernel, etc. (tutorial in on
http://a.michelizza.free.fr/pmwiki.php?n=TutoOS.IndexBig, but sorry,
it's currently only in french).

For various reasons (but main concern is code security), I'd like to
do the same in modula-2.

Thanks for all your posts, there are some good tracks to follow :)
Marco van de Voort
2011-04-19 09:31:02 UTC
Permalink
Post by Arnauld
Post by Marco van de Voort
That smells like Dos, and not like kernel programming, but making programs
on top of dos. ?And that means you need compilers with specific dos quirks
It was just for an example.
Anyway, it's not dos related but x86/amd64 arch related. On these
harwares, video memory (in text mode) is mapped on ram.
True but since no OS allows access to it but Dos, it is unlikely for non DOS
compilers to allow this.

Specially for 32/64-bit compilers, since that 16-bit realm can be outside
their data-space and need special access. (segment prefixes)

This goes for e.g. DJGPP or FPC. (which use the same go32v2 extender. Some
other extenders might map the dos 1MB directly into the 32-bit data space)

But the non-dos versions of these same compilers (gcc and FPC) don't support
these options. Under Windows console access is handled on *console functions
basis, and under *nix using ncurses/terminfo.
Arnauld
2011-04-19 09:50:01 UTC
Permalink
Post by Marco van de Voort
True but since no OS allows access to it but Dos, it is unlikely for non DOS
compilers to allow this.
Specially for 32/64-bit compilers, since that 16-bit realm can be outside
their data-space and need special access. (segment prefixes) [...]
You're right, but don't forget we are talking about a kernel. So,
memory addressing is not related to some Dos operating system but to
the MMU and the processor mode (real or protected). When your kernel
switch in protected mode, depending on how you deal with
segmentation / pagination, you can directly address video ram.
Martin Brown
2011-04-19 10:47:12 UTC
Permalink
Post by Marco van de Voort
Post by Arnauld
Here is a more concrete example.
I'd like to write on my video memory that begin in 0xB8A00. There are
25*80 characters on the terminal and each character is defined with 2
bytes : 1 byte for the character per se, and another byte for the
character attributes (color, bleep, etc.). So, for example, if I want
byte 0xB8A00 := 'A';
byte 0xB8A01 := 4; (* 4 encodes red color *)
How can I do this in m2 ?
That smells like Dos, and not like kernel programming, but making programs
on top of dos. And that means you need compilers with specific dos quirks
Typically in the JPIDOS dialect you could declare an array to map onto
memory mapped IO by using the following construct.

CONST SCREENSIZE=2*25*80;
VAR Screen [0A000H:0] : ARRAY[0..SCREENSIZE] OF BYTE;

I don't know if this feature made it into ISO M2 so I expect you might
have to do it longhand by ADDADR().

It would make much more sense to declare it as array of a 16 bit packed
TYPE CHAR with BITSET8 using named bits in the short BITSET for each of
the attributes. Doing this sort of direct memory mapped trick makes more
sense when you are doing things in bitmapped graphics like playing
Conways life using the screen itself as the working array.
Post by Marco van de Voort
Most 32-bit compilers won't let you do this that easily. Using (very) old
16-bits compilers will probably be easier.
And you can have trouble if there is an OS in the way that takes a dim
view of peeky pokey access to protected IO space memory. There is
usually an OS call to do this legitimately see module SYSTEM or InOut.

Regards,
Martin Brown
Marco van de Voort
2011-04-19 14:58:16 UTC
Permalink
Post by Martin Brown
Typically in the JPIDOS dialect you could declare an array to map onto
memory mapped IO by using the following construct.
CONST SCREENSIZE=2*25*80;
VAR Screen [0A000H:0] : ARRAY[0..SCREENSIZE] OF BYTE;
I don't know if this feature made it into ISO M2 so I expect you might
have to do it longhand by ADDADR().
Maybe. But using it to access the screen only works for sure with 16-bit
dos.

Even 32-bit apps sometimes can't do this, or only limited.

(Turbo Pascal has a similar extension, and FPC works around the fact by
prefixing such 16-bit addresses with a segment (iirc %fs). But not all
operations are possible that way iirc)
Post by Martin Brown
Post by Marco van de Voort
Most 32-bit compilers won't let you do this that easily. Using (very) old
16-bits compilers will probably be easier.
And you can have trouble if there is an OS in the way that takes a dim
view of peeky pokey access to protected IO space memory. There is
usually an OS call to do this legitimately see module SYSTEM or InOut.
Protected and outside the address space is different. Since then you need a
LONG address type (something identifying a segment and a 32/64-bits linear
address). Such things are rarely standarized.
tbreeden
2011-04-20 13:00:06 UTC
Permalink
Post by Martin Brown
VAR Screen [0A000H:0] : ARRAY[0..SCREENSIZE] OF BYTE;
I don't know if this feature made it into ISO M2 so I expect you might
have to do it longhand by ADDADR().
from ISO/IEC IS 10514, the international standard for Modula-2:

variable identifier list =
identifier, [ machine address], {comma, identifier,
[machine address] } ;
machine address =
left bracket, value of address type, right bracket ;

Constant values of the address type may only be formed by application
of the predefined function procedures MAKEADR or CAST from the system
module SYSTEM. The use of a machine address to fix the address of a
variable thus requires a direct or indirect import from SYSTEM

aaaaaaaaaaaaaaaaaaaaaaaaaa
2011-04-19 23:54:01 UTC
Permalink
Post by Arnauld
I'd like to write on my video memory that begin in 0xB8A00. There are
25*80 characters on the terminal and each character is defined with 2
bytes : 1 byte for the character per se, and another byte for the
character attributes (color, bleep, etc.). So, for example, if I want
byte 0xB8A00 := 'A';
byte 0xB8A01 := 4; (* 4 encodes red color *)
How can I do this in m2 ?
Best regards,
Good choice.

Read this: http://fruttenboel.verhoeven272.nl/modula-2/vgalib3.html

It is source code for DOS, but it does what you want.
Loading...