# Code for the first few disk sectors that all programs in this directory need: # - load sectors past the first (using BIOS primitives) since only the first is available by default # - if this fails, print 'D' at top-left of screen and halt # - initialize a minimal graphics mode # - switch to 32-bit mode (giving up access to BIOS primitives) # - set up a handler for keyboard events # - jump to start of program # Code in this file needs to be more deliberate about the SubX facilities it # uses: # - sigils only support 32-bit general-purpose registers, so don't work with segment registers or 16-bit or 8-bit registers # - metadata like rm32 and r32 can sometimes misleadingly refer to only the bottom 16 bits of the register; pay attention to the register name # # While most of Mu is thoroughly tested, this file is not. I don't yet # understand hardware interfaces well enough to explain to others. # Memory map of a Mu computer: # code: [0x00007c00, 0x0007de00) # system font: [0x00100000, 0x00f00000) # stack: (0x02000000, 0x01000000] # heap: [0x02000000, 0x80000000) # see 120allocate.subx; Qemu initializes with 128MB RAM by default; simulating 2GB RAM is known to work # Consult https://wiki.osdev.org/Memory_Map_(x86) before modifying any of # this. And don't forget to keep *stack-debug.subx in sync. == code ## 16-bit entry point: 0x7c00 # Upon reset, the IBM PC: # - loads the first sector (512 bytes) # from some bootable image (look for the boot-sector-marker further down this file) # to the address range [0x7c00, 0x7e00) # - starts executing code at address 0x7c00 fa/disable-interrupts # initialize segment registers b8/copy-to-ax 0/imm16 8e/->seg 3/mod/direct 0/rm32/ax 3/r32/ds 8e/->seg 3/mod/direct 0/rm32/ax 0/r32/es 8e/->seg 3/mod/direct 0/rm32/ax 4/r32/fs 8e/->seg 3/mod/direct 0/rm32/ax 5/r32/gs # Temporarily initialize stack to 0x00070000 in real mode. # We don't read or write the stack before we get to 32-bit mode, but BIOS # calls do. We need to move the stack in case BIOS initializes it to some # low address that we want to write code into. b8/copy-to-ax 0x7000/imm16 8e/->seg 3/mod/direct 0/rm32/ax 2/r32/ss bc/copy-to-esp 0/imm16 # undo the A20 hack: https://en.wikipedia.org/wiki/A20_line # this is from https://github.com/mit-pdos/xv6-public/blob/master/bootasm.S { e4/read-port-into-al 0x64/imm8 a8/test-bits-in-al 0x02/imm8 # set zf if bit 1 (second-least significant) is not set 75/jump-if-!zero loop/disp8 b0/copy-to-al 0xd1/imm8 e6/write-al-into-port 0x64/imm8 } { e4/read-port-into-al 0x64/imm8 a8/test-bits-in-al 0x02/imm8 # set zf if bit 1 (second-least significant) is not set 75/jump-if-!zero loop/disp8 b0/copy-to-al 0xdf/imm8 e6/write-al-into-port 0x64/imm8 } # load remaining sectors from first two tracks of disk into addresses [0x7e00, 0x17800) b4/copy-to-ah 2/imm8/read-drive # dl comes conveniently initialized at boot time with the index of the device being booted b5/copy-to-ch 0/imm8/cylinder b6/copy-to-dh 0/imm8/head # <==== b1/copy-to-cl 2/imm8/sector # 1-based b0/copy-to-al 0x7d/imm8/num-sectors # 2*63 - 1 = 125 # address to write sectors to = es:bx = 0x7e00, contiguous with boot segment bb/copy-to-bx 0/imm16 8e/->seg 3/mod/direct 3/rm32/bx 0/r32/es bb/copy-to-bx 0x7e00/imm16 # <==== cd/syscall 0x13/imm8/bios-disk-services 0f 82/jump-if-carry disk_error/disp16 # load two more tracks of disk into addresses [0x17800, 0x27400) b4/copy-to-ah 2/imm8/read-drive # dl comes conveniently initialized at boot time with the index of the device being booted b5/copy-to-ch 0/imm8/cylinder b6/copy-to-dh 2/imm8/head # <==== b1/copy-to-cl 1/imm8/sector # 1-based b0/copy-to-al 0x7e/imm8/num-sectors # 2*63 = 126 # address to write sectors to = es:bx = 0x17800, contiguous with boot segment bb/copy-to-bx 0x1780/imm16 # <==== 8e/->seg 3/mod/direct 3/rm32/bx 0/r32/es bb/copy-to-bx 0/imm16 cd/syscall 0x13/imm8/bios-disk-services 0f 82/jump-if-carry disk_error/disp16 # load two more tracks of disk into addresses [0x27400, 0x37000) b4/copy-to-ah 2/imm8/read-drive # dl comes conveniently initialized at boot time with the index of the device being booted b5/copy-to-ch 0/imm8/cylinder b6/copy-to-dh 4/imm8/head # <==== b1/copy-to-cl 1/imm8/sector # 1-based b0/copy-to-al 0x7e/imm8/num-sectors # 2*63 = 126 # address to write sectors to = es:bx = 0x27400, contiguous with boot segment bb/copy-to-bx 0x2740/imm16 # <==== 8e/->seg 3/mod/direct 3/rm32/bx 0/r32/es bb/copy-to-bx 0/imm16 cd/syscall 0x13/imm8/bios-disk-services 0f 82/jump-if-carry disk_error/disp16 # load two more tracks of disk into addresses [0x37000, 0x46c00) b4/copy-to-ah 2/imm8/read-drive # dl comes conveniently initialized at boot time with the index of the device being booted b5/copy-to-ch 0/imm8/cylinder b6/copy-to-dh 6/imm8/head # <==== b1/copy-to-cl 1/imm8/sector # 1-based b0/copy-to-al 0x7e/imm8/num-sectors # 2*63 = 126 # address to write sectors to = es:bx = 0x37000, contiguous with boot segment bb/copy-to-bx 0x3700/imm16 # <==== 8e/->seg 3/mod/direct 3/rm32/bx 0/r32/es bb/copy-to-bx 0/imm16 cd/syscall 0x13/imm8/bios-disk-services 0f 82/jump-if-carry disk_error/disp16 # load two more tracks of disk into addresses [0x46c00, 0x56800) b4/copy-to-ah 2/imm8/read-drive # dl comes conveniently initialized at boot time with the index of the device being booted b5/copy-to-ch 0/imm8/cylinder b6/copy-to-dh 8/imm8/head # <==== b1/copy-to-cl 1/imm8/sector # 1-based b0/copy-to-al 0x7e/imm8/num-sectors # 2*63 = 126 # address to write sectors to = es:bx = 0x46c00, contiguous with boot segment bb/copy-to-bx 0x46c0/imm16 # <==== 8e/->seg 3/mod/direct 3/rm32/bx 0/r32/es bb/copy-to-bx 0/imm16 cd/syscall 0x13/imm8/bios-disk-services 0f 82/jump-if-carry disk_error/disp16 # load two more tracks of disk into addresses [0x56800, 0x66400) b4/copy-to-ah 2/imm8/read-drive # dl comes conveniently initialized at boot time with the index of the device being booted b5/copy-to-ch 0/imm8/cylinder b6/copy-to-dh 0xa/imm8/head # <==== b1/copy-to-cl 1/imm8/sector # 1-based b0/copy-to-al 0x7e/imm8/num-sectors # 2*63 = 126 # address to write sectors to = es:bx = 0x56800, contiguous with boot segment bb/copy-to-bx 0x5680/imm16 # <==== 8e/->seg 3/mod/direct 3/rm32/bx 0/r32/es bb/copy-to-bx 0/imm16 cd/syscall 0x13/imm8/bios-disk-services 0f 82/jump-if-carry disk_error/disp16 # load two more tracks of disk into addresses [0x66400, 0x76000) b4/copy-to-ah 2/imm8/read-drive # dl comes conveniently initialized at boot time with the index of the device being booted b5/copy-to-ch 0/imm8/cylinder b6/copy-to-dh 0xc/imm8/head # <==== b1/copy-to-cl 1/imm8/sector # 1-based b0/copy-to-al 0x7e/imm8/num-sectors # 2*63 = 126 # address to write sectors to = es:bx = 0x56800, contiguous with boot segment bb/copy-to-bx 0x6640/imm16 # <==== 8e/->seg 3/mod/direct 3/rm32/bx 0/r32/es bb/copy-to-bx 0/imm16 cd/syscall 0x13/imm8/bios-disk-services 0f 82/jump-if-carry disk_error/disp16 # load one final track of disk into addresses [0x76000, 0x7de00) b4/copy-to-ah 2/imm8/read-drive # dl comes conveniently initialized at boot time with the index of the device being booted b5/copy-to-ch 0/imm8/cylinder b6/copy-to-dh 0xe/imm8/head # <==== b1/copy-to-cl 1/imm8/sector # 1-based b0/copy-to-al 0x3f/imm8/num-sectors=63 # address to write sectors to = es:bx = 0x56800, contiguous with boot segment bb/copy-to-bx 0x7600/imm16 # <==== 8e/->seg 3/mod/direct 3/rm32/bx 0/r32/es bb/copy-to-bx 0/imm16 cd/syscall 0x13/imm8/bios-disk-services 0f 82/jump-if-carry disk_error/disp16 ### Loading more code tracks would clobber BIOS; we need a new compilation strategy. # reset es bb/copy-to-bx 0/imm16 8e/->seg 3/mod/direct 3/rm32/bx 0/r32/es # adjust video mode b4/copy-to-ah 0x4f/imm8 # VBE commands b0/copy-to-al 2/imm8 # set video mode bb/copy-to-bx 0x4105/imm16 # 0x0105 | 0x4000
/*
* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
* See LICENSE file for license details.
*/
#include <stdio.h>
#include <string.h>
#include "draw.h"
#include "util.h"
static void
drawborder(Display *dpy, Brush *b)
{
XPoint points[5];
XSetLineAttributes(dpy, b->gc, 1, LineSolid, CapButt, JoinMiter);
XSetForeground(dpy, b->gc, b->border);
points[0].x = b->rect.x;
points[0].y = b->rect.y;
points[1].x = b->rect.width - 1;
points[1].y = 0;
points[2].x = 0;
points[2].y = b->rect.height - 1;
points[3].x = -(b->rect.width - 1);
points[3].y = 0;
points[4].x = 0;
points[4].y = -(b->rect.height - 1);
XDrawLines(dpy, b->drawable, b->gc, points, 5, CoordModePrevious);
}
void
draw(Display *dpy, Brush *b, Bool border, const char *text)
{
unsigned int x, y, w, h, len;
static char buf[256];
XGCValues gcv;
XSetForeground(dpy, b->gc, b->bg);
XFillRectangles(dpy, b->drawable, b->gc, &b->rect, 1);
if(border)
drawborder(dpy, b);
if(!text)
return;
len = strlen(text);
if(len >= sizeof(buf))
len = sizeof(buf) - 1;
memcpy(buf, text, len);
buf[len] = 0;
h = b->font.ascent + b->font.descent;
y = b->rect.y + (b->rect.height / 2) - (h / 2) + b->font.ascent;
x = b->rect.x + (h / 2);
/* shorten text if necessary */
while(len && (w = textwidth_l(&b->font, buf, len)) > b->rect.width - h)
buf[--len] = 0;
if(w > b->rect.width)
return; /* too long */
gcv.foreground = b->fg;
gcv.background = b->bg;
if(b->font.set) {
XChangeGC(dpy, b->gc, GCForeground | GCBackground, &gcv);
XmbDrawImageString(dpy, b->drawable, b->font.set, b->gc,
x, y, buf, len);
}
else {
gcv.font = b->font.xfont->fid;
XChangeGC(dpy, b->gc, GCForeground | GCBackground | GCFont, &gcv);
XDrawImageString(dpy, b->drawable, b->gc, x, y, buf, len);
}
}
static unsigned long
xloadcolors(Display *dpy, Colormap cmap, const char *colstr)
{
XColor color;
XAllocNamedColor(dpy, cmap, colstr, &color, &color);
return color.pixel;
}
void
loadcolors(Display *dpy, int screen, Brush *b,
const char *bg, const char *fg, const char *border)
{
Colormap cmap = DefaultColormap(dpy, screen);
b->bg = xloadcolors(dpy, cmap, bg);
b->fg = xloadcolors(dpy, cmap, fg);
b->border = xloadcolors(dpy, cmap, border);
}
unsigned int
textwidth_l(Fnt *font, char *text, unsigned int len)
{
if(font->set) {
XRectangle r;
XmbTextExtents(font->set, text, len, 0, &r);
return r.width;
}
return XTextWidth(font->xfont, text, len);
}
unsigned int
textwidth(Fnt *font, char *text)
{
return textwidth_l(font, text, strlen(text));
}
void
loadfont(Display *dpy, Fnt *font, const char *fontstr)
{
char **missing, *def;
int n;
missing = NULL;
def = "?";
setlocale(LC_ALL, "");
if(font->set)
XFreeFontSet(dpy, font->set);
font->set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
if(missing) {
while(n--