Skip to content

Commit

Permalink
fifth
Browse files Browse the repository at this point in the history
  • Loading branch information
jsvii committed Jan 29, 2014
1 parent 74dbdb2 commit ebaa1b8
Show file tree
Hide file tree
Showing 51 changed files with 28,000 additions and 0 deletions.
48 changes: 48 additions & 0 deletions 05_day/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
harib02
=======


## GDT & IDT

## 什么是分段

在16位时没有分段的概念,但在32位的处理器时,分段就有必要了。它将内存分为一段一段,每段都当作4G来处理,这样,任何一段程序都可以加上一个ORG 0,这个就极大地方便了编程。

看下面一个程序,它的DS是段地址,而不是说像8086那样,DS要剩以16。


MOV AL, [DS:EBX]

## 表示一个段有哪些信息

- 段的大小
- 段的起始地址
- 段的管理属性

CPU需要用`8`个字节的数据来表示这个信息,但段寄存器在i386时,并没有增长到32位,它依然是16位,是没有EDS, ECS 这种寄存器的!
那怎么办?


- 先将一个段号(segment selector)放入段寄存器里,然后预先设定段号与段的对应关系

- 由于CPU设计上的原因,段寄存器的低3位是不能使用的,所以能够用的段号才有13位,那么,能表示的段就只有0 - 8191,那么要存储的信息有 8192 * 8 = 65 536字节,即64KB

- 这64KB的信息就是GDT,将这些数据整齐地排列在内存中固定的某一个地方,即可了,再用一个GDTR这个特殊的寄存器来存储内在的起始地址和有效的设定个数

## IDT

Interrupt descriptor table中断记录表

要使用鼠标,键盘,网卡等设备,就必须要有中断,我们就必须设定IDT,IDT记录了0-255号中断号码与对应的函数关系,其设定方法与GDT非常相似(

## harib操作系统中GDT与IDT的分布

gdt 0x270000 - 0x27ffff
idt 0x26f800 - 0x26ffff






79 changes: 79 additions & 0 deletions 05_day/harib02a/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
TOOLPATH=../../z_tools/
INCPATH=../../z_tools/

MAKE = make -r
NASK = $(TOOLPATH)nask.exe
CC1 = $(TOOLPATH)cc1.exe -I $(INCPATH) -Os -Wall -quiet
GAS2NASK = $(TOOLPATH)gas2nask.exe -a
OBJ2BIM = $(TOOLPATH)obj2bim.exe
BIN2OBJ = $(TOOLPATH)bin2obj.exe
BIM2HRB = $(TOOLPATH)bim2hrb.exe
RULEFILE = $(TOOLPATH)haribote/haribote.rul
EDIMG = $(TOOLPATH)edimg.exe
IMGTOL = $(TOOLPATH)imgtol.com
MAKEFONT = $(TOOLPATH)makefont.exe
GOLIB = $(TOOLPATH)golib00.exe
COPY = cp
DEL = rm -f
QEMU = /usr/bin/qemu-system-i386


default :
$(MAKE) img

ipl10.bin : ipl10.nas Makefile
$(NASK) ipl10.nas ipl10.bin ipl10.lst

asmhead.bin : asmhead.nas Makefile
$(NASK) asmhead.nas asmhead.bin asmhead.lst

bootpack.gas : bootpack.c Makefile
$(CC1) -o bootpack.gas bootpack.c

bootpack.nas : bootpack.gas Makefile
$(GAS2NASK) bootpack.gas bootpack.nas

bootpack.obj : bootpack.nas Makefile
$(NASK) bootpack.nas bootpack.obj bootpack.lst

naskfunc.obj : naskfunc.nas Makefile
$(NASK) naskfunc.nas naskfunc.obj naskfunc.lst

bootpack.bim : bootpack.obj naskfunc.obj Makefile
$(OBJ2BIM) @$(RULEFILE) out:bootpack.bim stack:3136k map:bootpack.map \
bootpack.obj naskfunc.obj


bootpack.hrb : bootpack.bim Makefile
$(BIM2HRB) bootpack.bim bootpack.hrb 0

haribote.sys : asmhead.bin bootpack.hrb Makefile
cat asmhead.bin bootpack.hrb > haribote.sys

haribote.img : ipl10.bin haribote.sys Makefile
$(EDIMG) imgin:../../z_tools/fdimg0at.tek \
wbinimg src:ipl10.bin len:512 from:0 to:0 \
copy from:haribote.sys to:@: \
imgout:haribote.img

img :
$(MAKE) haribote.img

run :
$(MAKE) img
$(QEMU) -fda haribote.img

clean :
-$(DEL) *.bin
-$(DEL) *.lst
-$(DEL) *.gas
-$(DEL) *.obj
-$(DEL) bootpack.nas
-$(DEL) bootpack.map
-$(DEL) bootpack.bim
-$(DEL) bootpack.hrb
-$(DEL) haribote.sys

src_only :
$(MAKE) clean
-$(DEL) haribote.img
122 changes: 122 additions & 0 deletions 05_day/harib02a/asmhead.nas
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
; haribote-os boot asm
; TAB=4

BOTPAK EQU 0x00280000
DSKCAC EQU 0x00100000
DSKCAC0 EQU 0x00008000

CYLS EQU 0x0ff0
LEDS EQU 0x0ff1
VMODE EQU 0x0ff2
SCRNX EQU 0x0ff4
SCRNY EQU 0x0ff6
VRAM EQU 0x0ff8

ORG 0xc200

MOV AL,0x13
MOV AH,0x00
INT 0x10
MOV BYTE [VMODE],8
MOV WORD [SCRNX],320
MOV WORD [SCRNY],200
MOV DWORD [VRAM],0x000a0000

MOV AH,0x02
INT 0x16
MOV [LEDS],AL

MOV AL,0xff
OUT 0x21,AL
NOP
OUT 0xa1,AL

CLI

CALL waitkbdout
MOV AL,0xd1
OUT 0x64,AL
CALL waitkbdout
MOV AL,0xdf
OUT 0x60,AL
CALL waitkbdout


[INSTRSET "i486p"]

LGDT [GDTR0]
MOV EAX,CR0
AND EAX,0x7fffffff
OR EAX,0x00000001
MOV CR0,EAX
JMP pipelineflush
pipelineflush:
MOV AX,1*8
MOV DS,AX
MOV ES,AX
MOV FS,AX
MOV GS,AX
MOV SS,AX


MOV ESI,bootpack
MOV EDI,BOTPAK
MOV ECX,512*1024/4
CALL memcpy

MOV ESI,0x7c00
MOV EDI,DSKCAC
MOV ECX,512/4
CALL memcpy


MOV ESI,DSKCAC0+512
MOV EDI,DSKCAC+512
MOV ECX,0
MOV CL,BYTE [CYLS]
IMUL ECX,512*18*2/4
SUB ECX,512/4
CALL memcpy

MOV EBX,BOTPAK
MOV ECX,[EBX+16]
ADD ECX,3
SHR ECX,2
JZ skip
MOV ESI,[EBX+20]
ADD ESI,EBX
MOV EDI,[EBX+12]
CALL memcpy
skip:
MOV ESP,[EBX+12]
JMP DWORD 2*8:0x0000001b

waitkbdout:
IN AL,0x64
AND AL,0x02
JNZ waitkbdout
RET

memcpy:
MOV EAX,[ESI]
ADD ESI,4
MOV [EDI],EAX
ADD EDI,4
SUB ECX,1
JNZ memcpy
RET


ALIGNB 16
GDT0:
RESB 8
DW 0xffff,0x0000,0x9200,0x00cf
DW 0xffff,0x0000,0x9a28,0x0047

DW 0
GDTR0:
DW 8*3-1
DD GDT0

ALIGNB 16
bootpack:
121 changes: 121 additions & 0 deletions 05_day/harib02a/bootpack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
void io_hlt(void);
void io_cli(void);
void io_out8(int port, int data);
int io_load_eflags(void);
void io_store_eflags(int eflags);

void init_palette(void);
void set_palette(int start, int end, unsigned char *rgb);
void boxfill8(unsigned char *vram, int xsize, unsigned char c, int x0, int y0, int x1, int y1);
void init_screen(char *vram, int x, int y);

#define COL8_000000 0
#define COL8_FF0000 1
#define COL8_00FF00 2
#define COL8_FFFF00 3
#define COL8_0000FF 4
#define COL8_FF00FF 5
#define COL8_00FFFF 6
#define COL8_FFFFFF 7
#define COL8_C6C6C6 8
#define COL8_840000 9
#define COL8_008400 10
#define COL8_848400 11
#define COL8_000084 12
#define COL8_840084 13
#define COL8_008484 14
#define COL8_848484 15

void HariMain(void)
{
char *vram;
int xsize, ysize;
short *binfo_scrnx, *binfo_scrny;
int *binfo_vram;

init_palette();
binfo_scrnx = (short *) 0x0ff4;
binfo_scrny = (short *) 0x0ff6;
binfo_vram = (int *) 0x0ff8;
xsize = *binfo_scrnx;
ysize = *binfo_scrny;
vram = (char *) *binfo_vram;

init_screen(vram, xsize, ysize);

for (;;) {
io_hlt();
}
}

void init_palette(void)
{
static unsigned char table_rgb[16 * 3] = {
0x00, 0x00, 0x00,
0xff, 0x00, 0x00,
0x00, 0xff, 0x00,
0xff, 0xff, 0x00,
0x00, 0x00, 0xff,
0xff, 0x00, 0xff,
0x00, 0xff, 0xff,
0xff, 0xff, 0xff,
0xc6, 0xc6, 0xc6,
0x84, 0x00, 0x00,
0x00, 0x84, 0x00,
0x84, 0x84, 0x00,
0x00, 0x00, 0x84,
0x84, 0x00, 0x84,
0x00, 0x84, 0x84,
0x84, 0x84, 0x84
};
set_palette(0, 15, table_rgb);
return;

}

void set_palette(int start, int end, unsigned char *rgb)
{
int i, eflags;
eflags = io_load_eflags();
io_cli();
io_out8(0x03c8, start);
for (i = start; i <= end; i++) {
io_out8(0x03c9, rgb[0] / 4);
io_out8(0x03c9, rgb[1] / 4);
io_out8(0x03c9, rgb[2] / 4);
rgb += 3;
}
io_store_eflags(eflags);
return;
}

void boxfill8(unsigned char *vram, int xsize, unsigned char c, int x0, int y0, int x1, int y1)
{
int x, y;
for (y = y0; y <= y1; y++) {
for (x = x0; x <= x1; x++)
vram[y * xsize + x] = c;
}
return;
}

void init_screen(char *vram, int x, int y)
{
boxfill8(vram, x, COL8_008484, 0, 0, x - 1, y - 29);
boxfill8(vram, x, COL8_C6C6C6, 0, y - 28, x - 1, y - 28);
boxfill8(vram, x, COL8_FFFFFF, 0, y - 27, x - 1, y - 27);
boxfill8(vram, x, COL8_C6C6C6, 0, y - 26, x - 1, y - 1);

boxfill8(vram, x, COL8_FFFFFF, 3, y - 24, 59, y - 24);
boxfill8(vram, x, COL8_FFFFFF, 2, y - 24, 2, y - 4);
boxfill8(vram, x, COL8_848484, 3, y - 4, 59, y - 4);
boxfill8(vram, x, COL8_848484, 59, y - 23, 59, y - 5);
boxfill8(vram, x, COL8_000000, 2, y - 3, 59, y - 3);
boxfill8(vram, x, COL8_000000, 60, y - 24, 60, y - 3);

boxfill8(vram, x, COL8_848484, x - 47, y - 24, x - 4, y - 24);
boxfill8(vram, x, COL8_848484, x - 47, y - 23, x - 47, y - 4);
boxfill8(vram, x, COL8_FFFFFF, x - 47, y - 3, x - 4, y - 3);
boxfill8(vram, x, COL8_FFFFFF, x - 3, y - 24, x - 3, y - 3);
return;
}
Loading

0 comments on commit ebaa1b8

Please sign in to comment.