Compare commits

...

12 Commits

Author SHA1 Message Date
Yourtion
140774a293 bug fix 2016-05-21 00:24:41 +08:00
Yourtion
7c2fc66927 add screen 2016-05-20 15:28:31 +08:00
Yourtion
636c1a8c3d Update README 2016-05-20 15:16:20 +08:00
Yourtion
c177821913 整理 2016-05-20 14:56:29 +08:00
Yourtion
febac10ebf IPL的改良 2016-05-20 14:49:26 +08:00
Yourtion
0e31ff06ca bug fix 2016-05-20 14:27:07 +08:00
Yourtion
08d19d7512 图片阅览器 2016-05-20 14:05:13 +08:00
Yourtion
b2d31a8e54 MML播放器 2016-05-20 13:38:43 +08:00
Yourtion
72532c8cd7 文本阅览器 2016-05-20 12:03:41 +08:00
Yourtion
3871740126 命令行计算器 2016-05-20 11:01:41 +08:00
Yourtion
70b335278d bug fix 2016-05-20 10:54:34 +08:00
Yourtion
2c262f986d Add 30 day code 2016-05-20 10:44:09 +08:00
249 changed files with 14963 additions and 46 deletions

4
.gitignore vendored
View File

@@ -1 +1,5 @@
*.hrb
*.lst
*.map
*.bim
*.hrb

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -71,6 +71,7 @@ install :
full :
$(MAKE) -C haribote
$(MAKE) -C apilib
$(MAKE) -C stdlib
$(MAKE) -C a
$(MAKE) -C hello3
$(MAKE) -C hello4
@@ -121,6 +122,7 @@ src_only :
clean_full :
$(MAKE) -C haribote clean
$(MAKE) -C apilib clean
$(MAKE) -C stdlib clean
$(MAKE) -C a clean
$(MAKE) -C hello3 clean
$(MAKE) -C hello4 clean
@@ -150,6 +152,7 @@ clean_full :
src_only_full :
$(MAKE) -C haribote src_only
$(MAKE) -C apilib src_only
$(MAKE) -C stdlib src_only
$(MAKE) -C a src_only
$(MAKE) -C hello3 src_only
$(MAKE) -C hello4 src_only

Binary file not shown.

Binary file not shown.

1
30_day/!cons_9x.bat Normal file
View File

@@ -0,0 +1 @@
command

1
30_day/!cons_nt.bat Normal file
View File

@@ -0,0 +1 @@
cmd.exe

209
30_day/Makefile Normal file
View File

@@ -0,0 +1,209 @@
TOOLPATH = ../z_tools/
INCPATH = ../z_tools/haribote/
MAKE = $(TOOLPATH)make.exe -r
EDIMG = $(TOOLPATH)edimg.exe
IMGTOL = $(TOOLPATH)imgtol.com
COPY = copy
DEL = del
#默认动作
default :
$(MAKE) haribote.img
#文件生成规则
haribote.img : haribote/ipl10.bin haribote/haribote.sys Makefile \
a/a.hrb hello3/hello3.hrb hello4/hello4.hrb hello5/hello5.hrb \
winhelo/winhelo.hrb winhelo2/winhelo2.hrb winhelo3/winhelo3.hrb \
star1/star1.hrb stars/stars.hrb stars2/stars2.hrb \
lines/lines.hrb walk/walk.hrb noodle/noodle.hrb \
beepdown/beepdown.hrb color/color.hrb color2/color2.hrb \
sosu/sosu.hrb sosu2/sosu2.hrb sosu3/sosu3.hrb \
type/type.hrb iroha/iroha.hrb chklang/chklang.hrb \
notrec/notrec.hrb bball/bball.hrb invader/invader.hrb \
calc/calc.hrb tview/tview.hrb mmlplay/mmlplay.hrb gview/gview.hrb
$(EDIMG) imgin:../z_tools/fdimg0at.tek \
wbinimg src:haribote/ipl10.bin len:512 from:0 to:0 \
copy from:haribote/haribote.sys to:@: \
copy from:haribote/jp.nas to:@: \
copy from:make.bat to:@: \
copy from:a/a.hrb to:@: \
copy from:hello3/hello3.hrb to:@: \
copy from:hello4/hello4.hrb to:@: \
copy from:hello5/hello5.hrb to:@: \
copy from:winhelo/winhelo.hrb to:@: \
copy from:winhelo2/winhelo2.hrb to:@: \
copy from:winhelo3/winhelo3.hrb to:@: \
copy from:star1/star1.hrb to:@: \
copy from:stars/stars.hrb to:@: \
copy from:stars2/stars2.hrb to:@: \
copy from:lines/lines.hrb to:@: \
copy from:walk/walk.hrb to:@: \
copy from:noodle/noodle.hrb to:@: \
copy from:beepdown/beepdown.hrb to:@: \
copy from:color/color.hrb to:@: \
copy from:color2/color2.hrb to:@: \
copy from:sosu/sosu.hrb to:@: \
copy from:sosu2/sosu2.hrb to:@: \
copy from:sosu3/sosu3.hrb to:@: \
copy from:type/type.hrb to:@: \
copy from:iroha/iroha.hrb to:@: \
copy from:chklang/chklang.hrb to:@: \
copy from:euc.txt to:@: \
copy from:notrec/notrec.hrb to:@: \
copy from:bball/bball.hrb to:@: \
copy from:invader/invader.hrb to:@: \
copy from:calc/calc.hrb to:@: \
copy from:tview/tview.hrb to:@: \
copy from:mmlplay/mmlplay.hrb to:@: \
copy from:mmldata/kirakira.mml to:@: \
copy from:mmldata/fujisan.mml to:@: \
copy from:mmldata/daigo.mml to:@: \
copy from:mmldata/daiku.mml to:@: \
copy from:gview/gview.hrb to:@: \
copy from:pictdata/fujisan.jpg to:@: \
copy from:pictdata/night.bmp to:@: \
copy from:nihongo/nihongo.fnt to:@: \
imgout:haribote.img
#命令
run :
$(MAKE) haribote.img
$(COPY) haribote.img ..\z_tools\qemu\fdimage0.bin
$(MAKE) -C ../z_tools/qemu
install :
$(MAKE) haribote.img
$(IMGTOL) w a: haribote.img
full :
$(MAKE) -C haribote
$(MAKE) -C apilib
$(MAKE) -C stdlib
$(MAKE) -C a
$(MAKE) -C hello3
$(MAKE) -C hello4
$(MAKE) -C hello5
$(MAKE) -C winhelo
$(MAKE) -C winhelo2
$(MAKE) -C winhelo3
$(MAKE) -C star1
$(MAKE) -C stars
$(MAKE) -C stars2
$(MAKE) -C lines
$(MAKE) -C walk
$(MAKE) -C noodle
$(MAKE) -C beepdown
$(MAKE) -C color
$(MAKE) -C color2
$(MAKE) -C sosu
$(MAKE) -C sosu2
$(MAKE) -C sosu3
$(MAKE) -C type
$(MAKE) -C iroha
$(MAKE) -C chklang
$(MAKE) -C notrec
$(MAKE) -C bball
$(MAKE) -C invader
$(MAKE) -C calc
$(MAKE) -C tview
$(MAKE) -C mmlplay
$(MAKE) -C gview
$(MAKE) haribote.img
run_full :
$(MAKE) full
$(COPY) haribote.img ..\z_tools\qemu\fdimage0.bin
$(MAKE) -C ../z_tools/qemu
install_full :
$(MAKE) full
$(IMGTOL) w a: haribote.img
run_os :
$(MAKE) -C haribote
$(MAKE) run
clean :
#不执行任何操作
src_only :
$(MAKE) clean
-$(DEL) haribote.img
clean_full :
$(MAKE) -C haribote clean
$(MAKE) -C apilib clean
$(MAKE) -C stdlib clean
$(MAKE) -C a clean
$(MAKE) -C hello3 clean
$(MAKE) -C hello4 clean
$(MAKE) -C hello5 clean
$(MAKE) -C winhelo clean
$(MAKE) -C winhelo2 clean
$(MAKE) -C winhelo3 clean
$(MAKE) -C star1 clean
$(MAKE) -C stars clean
$(MAKE) -C stars2 clean
$(MAKE) -C lines clean
$(MAKE) -C walk clean
$(MAKE) -C noodle clean
$(MAKE) -C beepdown clean
$(MAKE) -C color clean
$(MAKE) -C color2 clean
$(MAKE) -C sosu clean
$(MAKE) -C sosu2 clean
$(MAKE) -C sosu3 clean
$(MAKE) -C type clean
$(MAKE) -C iroha clean
$(MAKE) -C chklang clean
$(MAKE) -C notrec clean
$(MAKE) -C bball clean
$(MAKE) -C invader clean
$(MAKE) -C calc clean
$(MAKE) -C tview clean
$(MAKE) -C mmlplay clean
$(MAKE) -C gview clean
src_only_full :
$(MAKE) -C haribote src_only
$(MAKE) -C apilib src_only
$(MAKE) -C stdlib src_only
$(MAKE) -C a src_only
$(MAKE) -C hello3 src_only
$(MAKE) -C hello4 src_only
$(MAKE) -C hello5 src_only
$(MAKE) -C winhelo src_only
$(MAKE) -C winhelo2 src_only
$(MAKE) -C winhelo3 src_only
$(MAKE) -C star1 src_only
$(MAKE) -C stars src_only
$(MAKE) -C stars2 src_only
$(MAKE) -C lines src_only
$(MAKE) -C walk src_only
$(MAKE) -C noodle src_only
$(MAKE) -C beepdown src_only
$(MAKE) -C color src_only
$(MAKE) -C color2 src_only
$(MAKE) -C sosu src_only
$(MAKE) -C sosu2 src_only
$(MAKE) -C sosu3 src_only
$(MAKE) -C type src_only
$(MAKE) -C iroha src_only
$(MAKE) -C chklang src_only
$(MAKE) -C notrec src_only
$(MAKE) -C bball src_only
$(MAKE) -C invader src_only
$(MAKE) -C calc src_only
$(MAKE) -C tview src_only
$(MAKE) -C mmlplay src_only
$(MAKE) -C gview src_only
-$(DEL) haribote.img
refresh :
$(MAKE) full
$(MAKE) clean_full
-$(DEL) haribote.img

1
30_day/a/!cons_9x.bat Normal file
View File

@@ -0,0 +1 @@
command

1
30_day/a/!cons_nt.bat Normal file
View File

@@ -0,0 +1 @@
cmd.exe

8
30_day/a/Makefile Normal file
View File

@@ -0,0 +1,8 @@
APP = a
STACK = 1k
MALLOC = 0k
include ../app_make.txt
$(APP).hrb : $(APP).org Makefile
$(COPY) $(APP).org $(APP).hrb

7
30_day/a/a.c Normal file
View File

@@ -0,0 +1,7 @@
#include "apilib.h"
void HariMain(void)
{
api_putchar('A');
api_end();
}

1
30_day/a/make.bat Normal file
View File

@@ -0,0 +1 @@
..\..\z_tools\make.exe %1 %2 %3 %4 %5 %6 %7 %8 %9

27
30_day/apilib.h Normal file
View File

@@ -0,0 +1,27 @@
void api_putchar(int c);
void api_putstr0(char *s);
void api_putstr1(char *s, int l);
void api_end(void);
int api_openwin(char *buf, int xsiz, int ysiz, int col_inv, char *title);
void api_putstrwin(int win, int x, int y, int col, int len, char *str);
void api_boxfilwin(int win, int x0, int y0, int x1, int y1, int col);
void api_initmalloc(void);
char *api_malloc(int size);
void api_free(char *addr, int size);
void api_point(int win, int x, int y, int col);
void api_refreshwin(int win, int x0, int y0, int x1, int y1);
void api_linewin(int win, int x0, int y0, int x1, int y1, int col);
void api_closewin(int win);
int api_getkey(int mode);
int api_alloctimer(void);
void api_inittimer(int timer, int data);
void api_settimer(int timer, int time);
void api_freetimer(int timer);
void api_beep(int tone);
int api_fopen(char *fname);
void api_fclose(int fhandle);
void api_fseek(int fhandle, int offset, int mode);
int api_fsize(int fhandle, int mode);
int api_fread(char *buf, int maxsize, int fhandle);
int api_cmdline(char *buf, int maxsize);
int api_getlang(void);

View File

@@ -0,0 +1 @@
command

View File

@@ -0,0 +1 @@
cmd.exe

48
30_day/apilib/Makefile Normal file
View File

@@ -0,0 +1,48 @@
OBJS_API = api001.obj api002.obj api003.obj api004.obj api005.obj api006.obj \
api007.obj api008.obj api009.obj api010.obj api011.obj api012.obj \
api013.obj api014.obj api015.obj api016.obj api017.obj api018.obj \
api019.obj api020.obj api021.obj api022.obj api023.obj api024.obj \
api025.obj api026.obj api027.obj alloca.obj
TOOLPATH = ../../z_tools/
INCPATH = ../../z_tools/haribote/
MAKE = $(TOOLPATH)make.exe -r
NASK = $(TOOLPATH)nask.exe
CC1 = $(TOOLPATH)cc1.exe -I$(INCPATH) -Os -Wall -quiet
GAS2NASK = $(TOOLPATH)gas2nask.exe -a
OBJ2BIM = $(TOOLPATH)obj2bim.exe
MAKEFONT = $(TOOLPATH)makefont.exe
BIN2OBJ = $(TOOLPATH)bin2obj.exe
BIM2HRB = $(TOOLPATH)bim2hrb.exe
RULEFILE = ../haribote.rul
EDIMG = $(TOOLPATH)edimg.exe
IMGTOL = $(TOOLPATH)imgtol.com
GOLIB = $(TOOLPATH)golib00.exe
COPY = copy
DEL = del
#默认动作
default :
$(MAKE) apilib.lib
#库生成规则
apilib.lib : Makefile $(OBJS_API)
$(GOLIB) $(OBJS_API) out:apilib.lib
#文件生成规则
%.obj : %.nas Makefile
$(NASK) $*.nas $*.obj $*.lst
#命令
clean :
-$(DEL) *.lst
-$(DEL) *.obj
src_only :
$(MAKE) clean
-$(DEL) apilib.lib

13
30_day/apilib/alloca.nas Normal file
View File

@@ -0,0 +1,13 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "alloca.nas"]
GLOBAL __alloca
[SECTION .text]
__alloca:
ADD EAX,-4
SUB ESP,EAX
JMP DWORD [ESP+EAX] ; 代替RET

14
30_day/apilib/api001.nas Normal file
View File

@@ -0,0 +1,14 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api001.nas"]
GLOBAL _api_putchar
[SECTION .text]
_api_putchar: ; void api_putchar(int c);
MOV EDX,1
MOV AL,[ESP+4] ; c
INT 0x40
RET

16
30_day/apilib/api002.nas Normal file
View File

@@ -0,0 +1,16 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api002.nas"]
GLOBAL _api_putstr0
[SECTION .text]
_api_putstr0: ; void api_putstr0(char *s);
PUSH EBX
MOV EDX,2
MOV EBX,[ESP+8] ; s
INT 0x40
POP EBX
RET

17
30_day/apilib/api003.nas Normal file
View File

@@ -0,0 +1,17 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api003.nas"]
GLOBAL _api_putstr1
[SECTION .text]
_api_putstr1: ; void api_putstr1(char *s, int l);
PUSH EBX
MOV EDX,3
MOV EBX,[ESP+ 8] ; s
MOV ECX,[ESP+12] ; l
INT 0x40
POP EBX
RET

12
30_day/apilib/api004.nas Normal file
View File

@@ -0,0 +1,12 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api004.nas"]
GLOBAL _api_end
[SECTION .text]
_api_end: ; void api_end(void);
MOV EDX,4
INT 0x40

24
30_day/apilib/api005.nas Normal file
View File

@@ -0,0 +1,24 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api005.nas"]
GLOBAL _api_openwin
[SECTION .text]
_api_openwin: ; int api_openwin(char *buf, int xsiz, int ysiz, int col_inv, char *title);
PUSH EDI
PUSH ESI
PUSH EBX
MOV EDX,5
MOV EBX,[ESP+16] ; buf
MOV ESI,[ESP+20] ; xsiz
MOV EDI,[ESP+24] ; ysiz
MOV EAX,[ESP+28] ; col_inv
MOV ECX,[ESP+32] ; title
INT 0x40
POP EBX
POP ESI
POP EDI
RET

27
30_day/apilib/api006.nas Normal file
View File

@@ -0,0 +1,27 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api006.nas"]
GLOBAL _api_putstrwin
[SECTION .text]
_api_putstrwin: ; void api_putstrwin(int win, int x, int y, int col, int len, char *str);
PUSH EDI
PUSH ESI
PUSH EBP
PUSH EBX
MOV EDX,6
MOV EBX,[ESP+20] ; win
MOV ESI,[ESP+24] ; x
MOV EDI,[ESP+28] ; y
MOV EAX,[ESP+32] ; col
MOV ECX,[ESP+36] ; len
MOV EBP,[ESP+40] ; str
INT 0x40
POP EBX
POP EBP
POP ESI
POP EDI
RET

27
30_day/apilib/api007.nas Normal file
View File

@@ -0,0 +1,27 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api007.nas"]
GLOBAL _api_boxfilwin
[SECTION .text]
_api_boxfilwin: ; void api_boxfilwin(int win, int x0, int y0, int x1, int y1, int col);
PUSH EDI
PUSH ESI
PUSH EBP
PUSH EBX
MOV EDX,7
MOV EBX,[ESP+20] ; win
MOV EAX,[ESP+24] ; x0
MOV ECX,[ESP+28] ; y0
MOV ESI,[ESP+32] ; x1
MOV EDI,[ESP+36] ; y1
MOV EBP,[ESP+40] ; col
INT 0x40
POP EBX
POP EBP
POP ESI
POP EDI
RET

20
30_day/apilib/api008.nas Normal file
View File

@@ -0,0 +1,20 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api008.nas"]
GLOBAL _api_initmalloc
[SECTION .text]
_api_initmalloc: ; void api_initmalloc(void);
PUSH EBX
MOV EDX,8
MOV EBX,[CS:0x0020] ; malloc内存空间的地址
MOV EAX,EBX
ADD EAX,32*1024 ; 加上32KB
MOV ECX,[CS:0x0000] ; 数据段的大小
SUB ECX,EAX
INT 0x40
POP EBX
RET

17
30_day/apilib/api009.nas Normal file
View File

@@ -0,0 +1,17 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api009.nas"]
GLOBAL _api_malloc
[SECTION .text]
_api_malloc: ; char *api_malloc(int size);
PUSH EBX
MOV EDX,9
MOV EBX,[CS:0x0020]
MOV ECX,[ESP+8] ; size
INT 0x40
POP EBX
RET

18
30_day/apilib/api010.nas Normal file
View File

@@ -0,0 +1,18 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api010.nas"]
GLOBAL _api_free
[SECTION .text]
_api_free: ; void api_free(char *addr, int size);
PUSH EBX
MOV EDX,10
MOV EBX,[CS:0x0020]
MOV EAX,[ESP+ 8] ; addr
MOV ECX,[ESP+12] ; size
INT 0x40
POP EBX
RET

23
30_day/apilib/api011.nas Normal file
View File

@@ -0,0 +1,23 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api011.nas"]
GLOBAL _api_point
[SECTION .text]
_api_point: ; void api_point(int win, int x, int y, int col);
PUSH EDI
PUSH ESI
PUSH EBX
MOV EDX,11
MOV EBX,[ESP+16] ; win
MOV ESI,[ESP+20] ; x
MOV EDI,[ESP+24] ; y
MOV EAX,[ESP+28] ; col
INT 0x40
POP EBX
POP ESI
POP EDI
RET

24
30_day/apilib/api012.nas Normal file
View File

@@ -0,0 +1,24 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api012.nas"]
GLOBAL _api_refreshwin
[SECTION .text]
_api_refreshwin: ; void api_refreshwin(int win, int x0, int y0, int x1, int y1);
PUSH EDI
PUSH ESI
PUSH EBX
MOV EDX,12
MOV EBX,[ESP+16] ; win
MOV EAX,[ESP+20] ; x0
MOV ECX,[ESP+24] ; y0
MOV ESI,[ESP+28] ; x1
MOV EDI,[ESP+32] ; y1
INT 0x40
POP EBX
POP ESI
POP EDI
RET

27
30_day/apilib/api013.nas Normal file
View File

@@ -0,0 +1,27 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api013.nas"]
GLOBAL _api_linewin
[SECTION .text]
_api_linewin: ; void api_linewin(int win, int x0, int y0, int x1, int y1, int col);
PUSH EDI
PUSH ESI
PUSH EBP
PUSH EBX
MOV EDX,13
MOV EBX,[ESP+20] ; win
MOV EAX,[ESP+24] ; x0
MOV ECX,[ESP+28] ; y0
MOV ESI,[ESP+32] ; x1
MOV EDI,[ESP+36] ; y1
MOV EBP,[ESP+40] ; col
INT 0x40
POP EBX
POP EBP
POP ESI
POP EDI
RET

16
30_day/apilib/api014.nas Normal file
View File

@@ -0,0 +1,16 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api014.nas"]
GLOBAL _api_closewin
[SECTION .text]
_api_closewin: ; void api_closewin(int win);
PUSH EBX
MOV EDX,14
MOV EBX,[ESP+8] ; win
INT 0x40
POP EBX
RET

14
30_day/apilib/api015.nas Normal file
View File

@@ -0,0 +1,14 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api015.nas"]
GLOBAL _api_getkey
[SECTION .text]
_api_getkey: ; int api_getkey(int mode);
MOV EDX,15
MOV EAX,[ESP+4] ; mode
INT 0x40
RET

13
30_day/apilib/api016.nas Normal file
View File

@@ -0,0 +1,13 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api016.nas"]
GLOBAL _api_alloctimer
[SECTION .text]
_api_alloctimer: ; int api_alloctimer(void);
MOV EDX,16
INT 0x40
RET

17
30_day/apilib/api017.nas Normal file
View File

@@ -0,0 +1,17 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api017.nas"]
GLOBAL _api_inittimer
[SECTION .text]
_api_inittimer: ; void api_inittimer(int timer, int data);
PUSH EBX
MOV EDX,17
MOV EBX,[ESP+ 8] ; timer
MOV EAX,[ESP+12] ; data
INT 0x40
POP EBX
RET

17
30_day/apilib/api018.nas Normal file
View File

@@ -0,0 +1,17 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api018.nas"]
GLOBAL _api_settimer
[SECTION .text]
_api_settimer: ; void api_settimer(int timer, int time);
PUSH EBX
MOV EDX,18
MOV EBX,[ESP+ 8] ; timer
MOV EAX,[ESP+12] ; time
INT 0x40
POP EBX
RET

16
30_day/apilib/api019.nas Normal file
View File

@@ -0,0 +1,16 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api019.nas"]
GLOBAL _api_freetimer
[SECTION .text]
_api_freetimer: ; void api_freetimer(int timer);
PUSH EBX
MOV EDX,19
MOV EBX,[ESP+ 8] ; timer
INT 0x40
POP EBX
RET

14
30_day/apilib/api020.nas Normal file
View File

@@ -0,0 +1,14 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api020.nas"]
GLOBAL _api_beep
[SECTION .text]
_api_beep: ; void api_beep(int tone);
MOV EDX,20
MOV EAX,[ESP+4] ; tone
INT 0x40
RET

16
30_day/apilib/api021.nas Normal file
View File

@@ -0,0 +1,16 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api021.nas"]
GLOBAL _api_fopen
[SECTION .text]
_api_fopen: ; int api_fopen(char *fname);
PUSH EBX
MOV EDX,21
MOV EBX,[ESP+8] ; fname
INT 0x40
POP EBX
RET

14
30_day/apilib/api022.nas Normal file
View File

@@ -0,0 +1,14 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api022.nas"]
GLOBAL _api_fclose
[SECTION .text]
_api_fclose: ; void api_fclose(int fhandle);
MOV EDX,22
MOV EAX,[ESP+4] ; fhandle
INT 0x40
RET

18
30_day/apilib/api023.nas Normal file
View File

@@ -0,0 +1,18 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api023.nas"]
GLOBAL _api_fseek
[SECTION .text]
_api_fseek: ; void api_fseek(int fhandle, int offset, int mode);
PUSH EBX
MOV EDX,23
MOV EAX,[ESP+8] ; fhandle
MOV ECX,[ESP+16] ; mode
MOV EBX,[ESP+12] ; offset
INT 0x40
POP EBX
RET

15
30_day/apilib/api024.nas Normal file
View File

@@ -0,0 +1,15 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api024.nas"]
GLOBAL _api_fsize
[SECTION .text]
_api_fsize: ; int api_fsize(int fhandle, int mode);
MOV EDX,24
MOV EAX,[ESP+4] ; fhandle
MOV ECX,[ESP+8] ; mode
INT 0x40
RET

18
30_day/apilib/api025.nas Normal file
View File

@@ -0,0 +1,18 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api025.nas"]
GLOBAL _api_fread
[SECTION .text]
_api_fread: ; int api_fread(char *buf, int maxsize, int fhandle);
PUSH EBX
MOV EDX,25
MOV EAX,[ESP+16] ; fhandle
MOV ECX,[ESP+12] ; maxsize
MOV EBX,[ESP+8] ; buf
INT 0x40
POP EBX
RET

17
30_day/apilib/api026.nas Normal file
View File

@@ -0,0 +1,17 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api026.nas"]
GLOBAL _api_cmdline
[SECTION .text]
_api_cmdline: ; int api_cmdline(char *buf, int maxsize);
PUSH EBX
MOV EDX,26
MOV ECX,[ESP+12] ; maxsize
MOV EBX,[ESP+8] ; buf
INT 0x40
POP EBX
RET

13
30_day/apilib/api027.nas Normal file
View File

@@ -0,0 +1,13 @@
[FORMAT "WCOFF"]
[INSTRSET "i486p"]
[BITS 32]
[FILE "api027.nas"]
GLOBAL _api_getlang
[SECTION .text]
_api_getlang: ; int api_getlang(void);
MOV EDX,27
INT 0x40
RET

1
30_day/apilib/make.bat Normal file
View File

@@ -0,0 +1 @@
..\..\z_tools\make.exe %1 %2 %3 %4 %5 %6 %7 %8 %9

92
30_day/app_make.txt Normal file
View File

@@ -0,0 +1,92 @@
TOOLPATH = ../../z_tools/
INCPATH = ../../z_tools/haribote/
APILIBPATH = ../apilib/
STDLIBPATH = ../stdlib/
HARIBOTEPATH = ../haribote/
MAKE = $(TOOLPATH)make.exe -r
NASK = $(TOOLPATH)nask.exe
CC1 = $(TOOLPATH)cc1.exe -I$(INCPATH) -I../ -Os -Wall -quiet
GAS2NASK = $(TOOLPATH)gas2nask.exe -a
OBJ2BIM = $(TOOLPATH)obj2bim.exe
MAKEFONT = $(TOOLPATH)makefont.exe
BIN2OBJ = $(TOOLPATH)bin2obj.exe
BIM2HRB = $(TOOLPATH)bim2hrb.exe
BIM2BIN = $(TOOLPATH)bim2bin.exe
RULEFILE = ../haribote.rul
EDIMG = $(TOOLPATH)edimg.exe
IMGTOL = $(TOOLPATH)imgtol.com
GOLIB = $(TOOLPATH)golib00.exe
COPY = copy
DEL = del
#默认动作
default :
$(MAKE) $(APP).hrb
#文件生成规则
$(APP).bim : $(APP).obj $(APILIBPATH)apilib.lib $(STDLIBPATH)stdlib.lib Makefile ../app_make.txt
$(OBJ2BIM) @$(RULEFILE) out:$(APP).bim map:$(APP).map stack:$(STACK) \
$(APP).obj $(APILIBPATH)apilib.lib $(STDLIBPATH)stdlib.lib
$(STDAPP).bim : $(APP).obj $(STDLIBPATH)stdlib.lib Makefile ../app_make.txt
$(OBJ2BIM) @$(RULEFILE) out:$(APP).bim map:$(APP).map stack:$(STACK) \
$(APP).obj $(STDLIBPATH)stdlib.lib
haribote.img : ../haribote/ipl10.bin ../haribote/haribote.sys $(APP).hrb \
Makefile ../app_make.txt
$(EDIMG) imgin:../../z_tools/fdimg0at.tek \
wbinimg src:../haribote/ipl10.bin len:512 from:0 to:0 \
copy from:../haribote/haribote.sys to:@: \
copy from:$(APP).hrb to:@: \
copy from:../nihongo/nihongo.fnt to:@: \
imgout:haribote.img
#一般规则
%.gas : %.c ../apilib.h ../stdlib.h Makefile ../app_make.txt
$(CC1) -o $*.gas $*.c
%.nas : %.gas Makefile ../app_make.txt
$(GAS2NASK) $*.gas $*.nas
%.obj : %.nas Makefile ../app_make.txt
$(NASK) $*.nas $*.obj $*.lst
%.org : %.bim Makefile ../app_make.txt
$(BIM2HRB) $*.bim $*.org $(MALLOC)
%.hrb : %.org Makefile ../app_make.txt
$(BIM2BIN) -osacmp in:$*.org out:$*.hrb
#命令
run :
$(MAKE) haribote.img
$(COPY) haribote.img ..\..\z_tools\qemu\fdimage0.bin
$(MAKE) -C ../../z_tools/qemu
full :
$(MAKE) -C $(APILIBPATH)
$(MAKE) -C $(STDLIBPATH)
$(MAKE) $(APP).hrb
run_full :
$(MAKE) -C $(APILIBPATH)
$(MAKE) -C $(STDLIBPATH)
$(MAKE) -C ../haribote
$(MAKE) run
clean :
-$(DEL) *.lst
-$(DEL) *.obj
-$(DEL) *.map
-$(DEL) *.bim
-$(DEL) *.org
-$(DEL) haribote.img
src_only :
$(MAKE) clean
-$(DEL) $(APP).hrb

View File

@@ -0,0 +1 @@
command

View File

@@ -0,0 +1 @@
cmd.exe

5
30_day/bball/Makefile Normal file
View File

@@ -0,0 +1,5 @@
APP = bball
STACK = 52k
MALLOC = 0k
include ../app_make.txt

37
30_day/bball/bball.c Normal file
View File

@@ -0,0 +1,37 @@
#include "apilib.h"
void HariMain(void)
{
int win, i, j, dis;
char buf[216 * 237];
struct POINT {
int x, y;
};
static struct POINT table[16] = {
{ 204, 129 }, { 195, 90 }, { 172, 58 }, { 137, 38 }, { 98, 34 },
{ 61, 46 }, { 31, 73 }, { 15, 110 }, { 15, 148 }, { 31, 185 },
{ 61, 212 }, { 98, 224 }, { 137, 220 }, { 172, 200 }, { 195, 168 },
{ 204, 129 }
};
win = api_openwin(buf, 216, 237, -1, "bball");
api_boxfilwin(win, 8, 29, 207, 228, 0);
for (i = 0; i <= 14; i++) {
for (j = i + 1; j <= 15; j++) {
dis = j - i; /*两点间的距离*/
if (dis >= 8) {
dis = 15 - dis; /*逆向计数*/
}
if (dis != 0) {
api_linewin(win, table[i].x, table[i].y, table[j].x, table[j].y, 8 - dis);
}
}
}
for (;;) {
if (api_getkey(1) == 0x0a) {
break; /*按下回车键则break; */
}
}
api_end();
}

1
30_day/bball/make.bat Normal file
View File

@@ -0,0 +1 @@
..\..\z_tools\make.exe %1 %2 %3 %4 %5 %6 %7 %8 %9

View File

@@ -0,0 +1 @@
command

View File

@@ -0,0 +1 @@
cmd.exe

5
30_day/beepdown/Makefile Normal file
View File

@@ -0,0 +1,5 @@
APP = beepdown
STACK = 1k
MALLOC = 40k
include ../app_make.txt

View File

@@ -0,0 +1,19 @@
#include "apilib.h"
void HariMain(void)
{
int i, timer;
timer = api_alloctimer();
api_inittimer(timer, 128);
for (i = 20000000; i >= 20000; i -= i / 100) {
/* 20KHz20Hz即人类可以听到的声音范围*/
/* i以1%的速度递减*/
api_beep(i);
api_settimer(timer, 1); /* 0.01秒*/
if (api_getkey(1) != 128) {
break;
}
}
api_beep(0);
api_end();
}

1
30_day/beepdown/make.bat Normal file
View File

@@ -0,0 +1 @@
..\..\z_tools\make.exe %1 %2 %3 %4 %5 %6 %7 %8 %9

1
30_day/calc/!cons_9x.bat Normal file
View File

@@ -0,0 +1 @@
command

1
30_day/calc/!cons_nt.bat Normal file
View File

@@ -0,0 +1 @@
cmd.exe

5
30_day/calc/Makefile Normal file
View File

@@ -0,0 +1,5 @@
APP = calc
STACK = 4k
MALLOC = 0k
include ../app_make.txt

163
30_day/calc/calc.c Normal file
View File

@@ -0,0 +1,163 @@
#include "apilib.h"
#include <stdio.h> /* sprintf */
#define INVALID -0x7fffffff
int strtol(char *s, char **endp, int base); /* 标准函数 <stdlib.h> */
char *skipspace(char *p);
int getnum(char **pp, int priority);
void HariMain(void)
{
int i;
char s[30], *p;
api_cmdline(s, 30);
for (p = s; *p > ' '; p++) { } /* 一直读到空格为止 */
i = getnum(&p, 9);
if (i == INVALID) {
api_putstr0("error!\n");
} else {
sprintf(s, "= %d = 0x%x\n", i, i);
api_putstr0(s);
}
api_end();
}
char *skipspace(char *p)
{
for (; *p == ' '; p++) { } /* 将空格跳过去 */
return p;
}
int getnum(char **pp, int priority)
{
char *p = *pp;
int i = INVALID, j;
p = skipspace(p);
/*单项运算符*/
if (*p == '+') {
p = skipspace(p + 1);
i = getnum(&p, 0);
} else if (*p == '-') {
p = skipspace(p + 1);
i = getnum(&p, 0);
if (i != INVALID) {
i = - i;
}
} else if (*p == '~') {
p = skipspace(p + 1);
i = getnum(&p, 0);
if (i != INVALID) {
i = ~i;
}
} else if (*p == '(') { /*括号*/
p = skipspace(p + 1);
i = getnum(&p, 9);
if (*p == ')') {
p = skipspace(p + 1);
} else {
i = INVALID;
}
} else if ('0' <= *p && *p <= '9') { /*数值*/
i = strtol(p, &p, 0);
} else { /*错误 */
i = INVALID;
}
/*二项运算符*/
for (;;) {
if (i == INVALID) {
break;
}
p = skipspace(p);
if (*p == '+' && priority > 2) {
p = skipspace(p + 1);
j = getnum(&p, 2);
if (j != INVALID) {
i += j;
} else {
i = INVALID;
}
} else if (*p == '-' && priority > 2) {
p = skipspace(p + 1);
j = getnum(&p, 2);
if (j != INVALID) {
i -= j;
} else {
i = INVALID;
}
} else if (*p == '*' && priority > 1) {
p = skipspace(p + 1);
j = getnum(&p, 1);
if (j != INVALID) {
i *= j;
} else {
i = INVALID;
}
} else if (*p == '/' && priority > 1) {
p = skipspace(p + 1);
j = getnum(&p, 1);
if (j != INVALID && j != 0) {
i /= j;
} else {
i = INVALID;
}
} else if (*p == '%' && priority > 1) {
p = skipspace(p + 1);
j = getnum(&p, 1);
if (j != INVALID && j != 0) {
i %= j;
} else {
i = INVALID;
}
} else if (*p == '<' && p[1] == '<' && priority > 3) {
p = skipspace(p + 2);
j = getnum(&p, 3);
if (j != INVALID && j != 0) {
i <<= j;
} else {
i = INVALID;
}
} else if (*p == '>' && p[1] == '>' && priority > 3) {
p = skipspace(p + 2);
j = getnum(&p, 3);
if (j != INVALID && j != 0) {
i >>= j;
} else {
i = INVALID;
}
} else if (*p == '&' && priority > 4) {
p = skipspace(p + 1);
j = getnum(&p, 4);
if (j != INVALID) {
i &= j;
} else {
i = INVALID;
}
} else if (*p == '^' && priority > 5) {
p = skipspace(p + 1);
j = getnum(&p, 5);
if (j != INVALID) {
i ^= j;
} else {
i = INVALID;
}
} else if (*p == '|' && priority > 6) {
p = skipspace(p + 1);
j = getnum(&p, 6);
if (j != INVALID) {
i |= j;
} else {
i = INVALID;
}
} else {
break;
}
}
p = skipspace(p);
*pp = p;
return i;
}

1
30_day/calc/make.bat Normal file
View File

@@ -0,0 +1 @@
..\..\z_tools\make.exe %1 %2 %3 %4 %5 %6 %7 %8 %9

View File

@@ -0,0 +1 @@
command

View File

@@ -0,0 +1 @@
cmd.exe

5
30_day/chklang/Makefile Normal file
View File

@@ -0,0 +1,5 @@
APP = chklang
STACK = 1k
MALLOC = 0k
include ../app_make.txt

24
30_day/chklang/chklang.c Normal file
View File

@@ -0,0 +1,24 @@
#include "apilib.h"
void HariMain(void)
{
int langmode = api_getlang();
static char s1[23] = { /* 日本語シフトJISモード日文Shift-JIS模式*/
0x93, 0xfa, 0x96, 0x7b, 0x8c, 0xea, 0x83, 0x56, 0x83, 0x74, 0x83, 0x67,
0x4a, 0x49, 0x53, 0x83, 0x82, 0x81, 0x5b, 0x83, 0x68, 0x0a, 0x00
};
static char s2[17] = { /*日本語EUCモード日文EUC模式*/
0xc6, 0xfc, 0xcb, 0xdc, 0xb8, 0xec, 0x45, 0x55, 0x43, 0xa5, 0xe2, 0xa1,
0xbc, 0xa5, 0xc9, 0x0a, 0x00
};
if (langmode == 0) {
api_putstr0("English ASCII mode\n");
}
if (langmode == 1) {
api_putstr0(s1);
}
if (langmode == 2) {
api_putstr0(s2);
}
api_end();
}

1
30_day/chklang/make.bat Normal file
View File

@@ -0,0 +1 @@
..\..\z_tools\make.exe %1 %2 %3 %4 %5 %6 %7 %8 %9

View File

@@ -0,0 +1 @@
command

View File

@@ -0,0 +1 @@
cmd.exe

5
30_day/color/Makefile Normal file
View File

@@ -0,0 +1,5 @@
APP = color
STACK = 1k
MALLOC = 56k
include ../app_make.txt

21
30_day/color/color.c Normal file
View File

@@ -0,0 +1,21 @@
#include "apilib.h"
void HariMain(void)
{
char *buf;
int win, x, y, r, g, b;
api_initmalloc();
buf = api_malloc(144 * 164);
win = api_openwin(buf, 144, 164, -1, "color");
for (y = 0; y < 128; y++) {
for (x = 0; x < 128; x++) {
r = x * 2;
g = y * 2;
b = 0;
buf[(x + 8) + (y + 28) * 144] = 16 + (r / 43) + (g / 43) * 6 + (b / 43) * 36;
}
}
api_refreshwin(win, 8, 28, 136, 156);
api_getkey(1); /*等待按下任意键*/
api_end();
}

1
30_day/color/make.bat Normal file
View File

@@ -0,0 +1 @@
..\..\z_tools\make.exe %1 %2 %3 %4 %5 %6 %7 %8 %9

View File

@@ -0,0 +1 @@
command

View File

@@ -0,0 +1 @@
cmd.exe

5
30_day/color2/Makefile Normal file
View File

@@ -0,0 +1,5 @@
APP = color2
STACK = 1k
MALLOC = 56k
include ../app_make.txt

36
30_day/color2/color2.c Normal file
View File

@@ -0,0 +1,36 @@
#include "apilib.h"
unsigned char rgb2pal(int r, int g, int b, int x, int y);
void HariMain(void)
{
char *buf;
int win, x, y;
api_initmalloc();
buf = api_malloc(144 * 164);
win = api_openwin(buf, 144, 164, -1, "color2");
for (y = 0; y < 128; y++) {
for (x = 0; x < 128; x++) {
buf[(x + 8) + (y + 28) * 144] = rgb2pal(x * 2, y * 2, 0, x, y);
}
}
api_refreshwin(win, 8, 28, 136, 156);
api_getkey(1); /* <20>Ă<EFBFBD><C482>Ƃ<EFBFBD><C682>ȃL<C883>[<5B><><EFBFBD>͂<EFBFBD>҂<EFBFBD> */
api_end();
}
unsigned char rgb2pal(int r, int g, int b, int x, int y)
{
static int table[4] = { 3, 1, 0, 2 };
int i;
x &= 1; /*判断是偶数还是奇数*/
y &= 1;
i = table[x + y * 2]; /*用来生成中间色的常量*/
r = (r * 21) / 256; /* r为020*/
g = (g * 21) / 256;
b = (b * 21) / 256;
r = (r + i) / 4; /* r为05*/
g = (g + i) / 4;
b = (b + i) / 4;
return 16 + r + g * 6 + b * 36;
}

1
30_day/color2/make.bat Normal file
View File

@@ -0,0 +1 @@
..\..\z_tools\make.exe %1 %2 %3 %4 %5 %6 %7 %8 %9

1
30_day/euc.txt Normal file
View File

@@ -0,0 +1 @@
日本語EUCで書いてみたよー

View File

@@ -0,0 +1 @@
command

View File

@@ -0,0 +1 @@
cmd.exe

90
30_day/gview/Makefile Normal file
View File

@@ -0,0 +1,90 @@
APP = gview
STACK = 4480k
MALLOC = 0k
TOOLPATH = ../../z_tools/
INCPATH = ../../z_tools/haribote/
APILIBPATH = ../apilib/
HARIBOTEPATH = ../haribote/
MAKE = $(TOOLPATH)make.exe -r
NASK = $(TOOLPATH)nask.exe
CC1 = $(TOOLPATH)cc1.exe -I$(INCPATH) -I../ -Os -Wall -quiet
GAS2NASK = $(TOOLPATH)gas2nask.exe -a
OBJ2BIM = $(TOOLPATH)obj2bim.exe
MAKEFONT = $(TOOLPATH)makefont.exe
BIN2OBJ = $(TOOLPATH)bin2obj.exe
BIM2HRB = $(TOOLPATH)bim2hrb.exe
BIM2BIN = $(TOOLPATH)bim2bin.exe
RULEFILE = ../haribote.rul
EDIMG = $(TOOLPATH)edimg.exe
IMGTOL = $(TOOLPATH)imgtol.com
GOLIB = $(TOOLPATH)golib00.exe
COPY = copy
DEL = del
#默认动作
default :
$(MAKE) $(APP).hrb
#文件生成规则
$(APP).bim : $(APP).obj bmp.obj jpeg.obj $(APILIBPATH)apilib.lib Makefile
$(OBJ2BIM) @$(RULEFILE) out:$(APP).bim map:$(APP).map stack:$(STACK) \
$(APP).obj jpeg.obj bmp.obj $(APILIBPATH)apilib.lib
haribote.img : ../haribote/ipl20.bin ../haribote/haribote.sys $(APP).hrb \
Makefile
$(EDIMG) imgin:../../z_tools/fdimg0at.tek \
wbinimg src:../haribote/ipl20.bin len:512 from:0 to:0 \
copy from:../haribote/haribote.sys to:@: \
copy from:$(APP).hrb to:@: \
copy from:../nihongo/nihongo.fnt to:@: \
imgout:haribote.img
# 其他指令
%.gas : %.c ../apilib.h Makefile
$(CC1) -o $*.gas $*.c
%.nas : %.gas Makefile
$(GAS2NASK) $*.gas $*.nas
%.obj : %.nas Makefile
$(NASK) $*.nas $*.obj $*.lst
%.org : %.bim Makefile
$(BIM2HRB) $*.bim $*.org $(MALLOC)
%.hrb : %.org Makefile
$(BIM2BIN) -osacmp in:$*.org out:$*.hrb
# 运行程序
run :
$(MAKE) haribote.img
$(COPY) haribote.img ..\..\z_tools\qemu\fdimage0.bin
$(MAKE) -C ../../z_tools/qemu
full :
$(MAKE) -C $(APILIBPATH)
$(MAKE) $(APP).hrb
run_full :
$(MAKE) -C $(APILIBPATH)
$(MAKE) -C ../haribote
$(MAKE) run
clean :
-$(DEL) *.lst
-$(DEL) gview.obj
-$(DEL) jpeg.obj
-$(DEL) *.map
-$(DEL) *.bim
-$(DEL) *.org
-$(DEL) haribote.img
src_only :
$(MAKE) clean
-$(DEL) $(APP).hrb

646
30_day/gview/bmp.nasm Normal file
View File

@@ -0,0 +1,646 @@
; BMP decode routine by I.Tak. 2003
section .text align=1
[bits 32]
;BMP File Structure (I can't understand MS.)
struc BMP
;FILE HEADER
.fType: resw 1 ;BM
.fSize: resd 1 ;whole file size
resd 1 ;reserved
.fOffBits: resd 1 ;offset from file top to image
;INFO HEADER
.iSize: resd 1 ;INFO HEADER size
.iWidth: resd 1 ;Image Width in pixels
.iHeight: resd 1 ;Image Height in pixels
.iPlanes: resw 1 ;must be 1
.iBitCount: resw 1 ;BitPerPixel 1, 4, 8, 24 (and 15,16 for new OS/2 ?)
.iCompression: resd 1 ;Compress Type. 0 for none, then SizeImage=0
.iSizeImage: resd 1 ;Image Size(compressed)
.iXPPM: resd 1 ;X Pixel Per Meter
.iYPPM: resd 1
.iClrUsed: resd 1 ;Number of used ColorQuad (0 for whole Quad)
.iClrImportant: resd 1 ;Number of Important ColorQuad.
endstruc
struc BMPOS2
;FILE HEADER
.fType: resw 1 ;BM
.fSize: resd 1 ;whole file size
resd 1 ;reserved
.fOffBits: resd 1 ;offset from file top to image
;CORE HEADER
.iSize: resd 1 ;CORE HEADER size
.iWidth: resw 1 ;Image Width in pixels
.iHeight: resw 1 ;Image Height in pixels
.iPlanes: resw 1 ;must be 1
.iBitCount: resw 1 ;BitPerPixel 1, 4, 8, 24 (and 15,16 for new OS/2 ?)
endstruc
; B/W bmp can also have palettes. The first for 0, second for 1.
struc CQuad
.b: resb 1
.g: resb 1
.r: resb 1
resb 1 ;reserved
endstruc
%if 0
int info_BMP(struct DLL_STRPICENV *env, int *info, int size, UCHAR *fp);
/* 1 (0?), 0 */
int decode0_BMP(struct DLL_STRPICENV *env, int size, UCHAR *fp,
int b_type, UCHAR *buf, int skip);
/* 0 */
envは64KBのワークエリアである8dwは返り値用になっている
, 使JPEG_initで
base_imgを作るのはdecodeのときだけでいいと思う
%endif
[absolute 0] ;naskなら[section .bss] org 0 かな win32だからダメか
bmpinfo:
.regs: resd 4
.reteip: resd 1
.env: resd 1
.info: resd 1
.size: resd 1
.module: resd 1
[absolute 0]
info:
.type: resd 1 ;1 for BMP, 2 for JPEG
resd 1 ;0
.width: resd 1
.height: resd 1
[section .text]
[global _info_BMP]
_info_BMP:
push ebx
push ebp
push esi
push edi
mov esi, [esp+bmpinfo.module]
mov eax, [esp+bmpinfo.size]
call bmpHeader
test edi, edi
jz .ret
mov esi, [esp+bmpinfo.info]
mov [esi+info.width], eax
mov [esi+info.height], ecx
mov [esi+info.type], edi ;=1
dec edi
mov [esi+info.type+4], edi ;=0
inc edi
.ret: mov eax, edi
pop edi
pop esi
pop ebp
pop ebx
ret
;in: esi=module, eax=size
;ret:eax=width, ecx=hegiht, edx=paletteSize, ebx=palette
; ebp=bpp, esi=endOfImage, edi=successFlag
bmpHeader:
lea edx, [esi+eax] ;moduleEnd
xor edi, edi
push edx
xor edx, edx
cmp eax, byte BMP.iSize+4
jbe ..@ret ;中継してしまう
cmp word[esi],'BM'
je .notMAC
sub esi, byte -128
add eax, byte -128
pop ebx
push eax
cmp eax, byte BMP.iSize+4
..@ret: jbe .ret
cmp word[esi], 'BM'
jne .ret
.notMAC:
;;MS,OS/2 フォーマット確認
mov ecx, [esi +BMP.iSize]
cmp ecx, byte 12 ;OS/2 format.
jne .MS
cmp eax, byte BMPOS2_size
jbe .ret ;coreヘッダなし
lea ebx, [esi+ecx+14] ;palette
movzx eax, word[esi+BMPOS2.iWidth] ;width
movzx ecx, word[esi+BMPOS2.iHeight] ;height
movzx ebp, word[esi+BMPOS2.iBitCount] ;bpp
mov dl, 3 ;paletteSize
jmp short .endif
.MS: cmp eax, byte BMP_size
jbe .ret ;infoヘッダなし
lea ebx, [esi+ecx+14]
sub ecx,byte 40
jne .ret ;unknownFormat
cmp ecx, [esi+BMP.iCompression]
jne .ret ;Compressed.
mov eax, [esi+BMP.iWidth] ;width
mov ecx, [esi+BMP.iHeight] ;height
movzx ebp, word[esi +BMP.iBitCount] ;bpp
mov dl, 4 ;paletteSize
.endif:
add esi, [esi +BMP.fOffBits]
;sizeが小さい場合heightを削ってで<E381A6>
;読めると答えるべきだろう。今はエラー
push edx
push eax
mul ebp ;eax=width*bpp
add eax, byte 7
shr eax, 3 ;lineSizeWithoutPudding
mov edx, eax
add eax, byte 3 ;size<1GBを仮定してedxを無<E38292>
and al, -4 ;lineSizeWithPudding
sub edx, eax ;-puddingSize
push edx
mul ecx
pop edx
add esi, eax
add esi, edx ;最終行の最後にはpuddingがないと見るべき
cmp esi, [esp+8] ;endOfModule
pop eax
ja .ret2
sub esi, edx ;esi=endOfImage
inc edi ;succeeded!
.ret2: pop edx
.ret: add esp, byte 4
ret
;***************************************************************
; 小ささ優先で作っているが, 速さ優先で作った昔のよりいいかも。
; 品質最低の高速モードのみ作ってい<E381A6>
[absolute 0]
decode:
.regs: resd 4
.reteip: resd 1
.env: resd 1
.size: resd 1
.module: resd 1
.outputType: resd 1
.dest: resd 1
.skip: resd 1
[section .text]
[global _decode0_BMP]
_decode0_BMP:
push ebx
push ebp
push esi
push edi
mov esi, [esp+decode.module]
mov eax, [esp+decode.size]
call bmpHeader
;ret:eax=width, ecx=hegiht, edx=paletteSize, ebx=palette
; ebp=bpp, esi=endOfImage, edi=successFlag
test edi,edi
jz .error
mov edi, [esp+decode.dest]
push dword[esp+decode.outputType]
push dword[esp+4+decode.skip]
push ecx ;height
push eax
push edx
mul ebp
add eax, byte 31
shr eax, 3
and al, -4
push eax
mov edx, ebp
mov ebp, esp
call bmp2beta ;ecx!=0 for error
add esp, byte bb.size
mov eax, ecx
test ecx, ecx
jz .ret
.error: push byte 1
pop eax
.ret: pop edi
pop esi
pop ebp
pop ebx
ret
[absolute -4*2]
bb:
.col0: resd 1 ;bpp1で使う
.reteip: resd 1
.sw: resd 1 ;byte
.paletteSize: resd 1 ;byte
.w: resd 1 ;pixel
.h: resd 1
.s: resd 1
.t: resd 1
.size: equ $-$$
[section .text]
;eax=?, ecx=height, edx=bpp, ebx=palette
;ebp=bb, esi=endOfImage, edi=dest
bmp2beta:
mov al, [ebp+bb.t]
and al, 0x7f
cmp al, 2
je near buf16
cmp al, 4
je buf32
mov ecx, esp ;!=0
ret
;===============================================================
; Buffer mode 4
;===============================================================
buf32:
dec edx
je near .bpp1
sub edx, byte 4-1
je .bpp4
sub edx, byte 8-4
je .bpp8
sub edx, byte 24-8
je .bpp24
mov ecx, esp ;!=0
ret
;---------------------------------------------------
; 24bpp BMP to 4byte bufer
;---------------------------------------------------
.bpp24:
;ecx=height, edx=0, ebx=palette
;esi=endOfImage, edi=destinationBuffer
;bb.w=width(pixel), bb.s=skipByte, bb.h=height
;bb.t=outputType, bb.sw=sourceWidthByte
.do24.1:
sub esi, [ebp+bb.sw] ;esi=startOfLine
push ecx
push esi
mov ecx, [ebp+bb.w]
.do24.2:
mov al, [esi]
mov [edi+3], dl
mov [edi], al
mov al, [esi+1]
mov [edi+1], al
mov al, [esi+2]
mov [edi+2], al
add esi, byte 3
add edi, byte 4
dec ecx
jnz .do24.2
pop esi
pop ecx
add edi, [ebp+bb.s]
dec ecx
jnz .do24.1
ret
;---------------------------------------------------
; 8bpp BMP to 4byte buffer
;---------------------------------------------------
.bpp8:
;ecx=height, edx=0, ebx=palette
;esi=endOfImage, edi=destinationBuffer
;pallete変換
mov dl, 255
mov eax, [ebp+bb.paletteSize]
sub ebx, eax
shl eax, 8
add ebx, eax ;ebx += paletteSize*255
.do8.1:
mov eax, [ebx]
sub ebx, [ebp+bb.paletteSize]
and eax, 0x00ffffff
dec edx
push eax
jns .do8.1
.do8.2:
sub esi, [ebp+bb.sw] ;esi=firstLineStart
push ecx
push esi
mov ecx, [ebp+bb.w]
.do8.3:
xor eax, eax
add edi, byte 4
mov al, [esi]
inc esi
;AGI stole
mov eax, [esp+eax*4+8]
dec ecx
mov [edi-4], eax
jnz .do8.3
pop esi
pop ecx
add edi, [ebp+bb.s]
dec ecx
jnz .do8.2
add esp, 256*4 ;palette
ret
;---------------------------------------------------
; 4bpp BMP to 4byte buffer
;---------------------------------------------------
.bpp4:
;ecx=height, edx=0, ebx=palette
;esi=endOfImage, edi=destinationBuffer
;pallete変換
mov dl, 16
mov eax, [ebp+bb.paletteSize]
sub ebx, eax
shl eax, 4
add ebx, eax ;ebx+=eax*15
.do4.1
mov eax, [ebx]
sub ebx, [ebp+bb.paletteSize]
and eax, 0x00ffffff
dec edx
push eax
jnz .do4.1
.do4.2:
sub esi, [ebp+bb.sw] ;esi=firstLineStart
push ecx
push esi
mov ecx, [ebp+bb.w]
.do4.3:
xor edx, edx
mov al, [esi]
mov dl, al
inc esi
shr dl, 4
and eax, byte 15
add edi, byte 4
dec ecx
mov edx, [esp+edx*4+8]
mov eax, [esp+eax*4+8]
mov [edi-4], edx
jz .wend
mov [edi], eax
add edi, byte 4
dec ecx
jnz .do4.3
.wend: pop esi
pop ecx
add edi, [ebp+bb.s]
dec ecx
jnz .do4.2
add esp, 16*4 ;palette
ret
;---------------------------------------------------
; 1bpp BMP to 4byte buffer
;---------------------------------------------------
.bpp1:
;ecx=height, edx=0, ebx=palette
;esi=endOfImage, edi=destinationBuffer
;pallete変換
mov eax, [ebx]
add ebx, [ebp+bb.paletteSize]
and eax, 0x00ffffff
mov ebx, [ebx]
and ebx, 0x00ffffff
xor ebx, eax
;push ebx
push eax
.do1.1:
sub esi, [ebp+bb.sw] ;esi=firstLineStart
push ecx
push esi
mov ecx, [ebp+bb.w]
.do1.2:
mov dl, [esi]
inc esi
push esi
mov esi, 8
.do1.3:
add edi, byte 4
add dl, dl
sbb eax, eax
and eax, ebx
xor eax, [ebp+bb.col0]
dec ecx
mov [edi-4], eax
jz .wend1bpp
dec esi
jnz .do1.3
pop esi
jmp short .do1.2
.wend1bpp:pop ecx
pop esi
pop ecx
add edi, [ebp+bb.s]
dec ecx
jnz .do1.1
pop eax
ret
;===============================================
; Buffer mode 2byte
;===============================================
buf16:
dec edx
je near .bpp1
sub edx, byte 4-1
je near .bpp4
sub edx, byte 8-4
je .bpp8
sub edx, byte 24-8
je .bpp24
mov ecx, esp
ret
;---------------------------------------------------
; 24bpp BMP to 2byte bufer
;---------------------------------------------------
.bpp24:
;ecx=height, edx=0, ebx=palette
;esi=endOfImage, edi=destinationBuffer
.do24.1:
sub esi, [ebp+bb.sw] ;esi=startOfLine
push ecx
push esi
mov ecx, [ebp+bb.w]
.do24.2:
mov al, [esi+2]
shl eax, 16
mov ax, [esi]
add esi, byte 3
;減色処理 eax=24bitColor, edx=work, ecx=counter, ebx=work
;川合さんの趣味で没ったルーチンを持ってくるもよし(ぉ
;誤差拡散ルーチンを持ってくるもよし
shr ah, 2 ;???????? RRRRRrrr 00GGGGGG BBBBBbbb
inc edi
shr eax, 3 ;000????? ???RRRRR rrr00GGG GGGBBBBB
shl ax, 5 ;000????? ???RRRRR GGGGGGBB BBB00000
inc edi
shr eax, 5 ;00000000 ???????? RRRRRGGG GGGBBBBB
dec ecx
mov [edi-2], ax
jnz .do24.2
pop esi
pop ecx
add edi, [ebp+bb.s]
dec ecx
jnz .do24.1
ret
;---------------------------------------------------
; 8bpp BMP to 2byte buffer
;---------------------------------------------------
.bpp8:
;ecx=height, edx=0, ebx=palette
;esi=endOfImage, edi=destinationBuffer
;pallete変換
mov dl, 255
mov eax, [ebp+bb.paletteSize]
sub ebx, eax
shl eax, 8
add ebx, eax ;ebx += paletteSize*255
.do8.1:
mov eax, [ebx]
sub ebx, [ebp+bb.paletteSize]
call .paletteConv
dec edx
push eax
jns .do8.1
.do8.2:
sub esi, [ebp+bb.sw] ;esi=firstLineStart
push ecx
push esi
mov ecx, [ebp+bb.w]
.do8.3:
xor eax, eax
add edi, byte 2
mov al, [esi]
inc esi
;AGI stole
mov eax, [esp+eax*4+8]
dec ecx
mov [edi-2], ax
jnz .do8.3
pop esi
pop ecx
add edi, [ebp+bb.s]
dec ecx
jnz .do8.2
add esp, 256*4 ;palette
ret
;---------------------------------------------------
; 4bpp BMP to 2byte buffer
;---------------------------------------------------
.bpp4:
;ecx=height, edx=0, ebx=palette
;esi=endOfImage, edi=destinationBuffer
;pallete変換
mov dl, 16
mov eax, [ebp+bb.paletteSize]
sub ebx, eax
shl eax, 4
add ebx, eax ;ebx+=eax*15
.do4.1:
mov eax, [ebx]
sub ebx, [ebp+bb.paletteSize]
call .paletteConv
dec edx
push eax
jnz .do4.1
.do4.2:
sub esi, [ebp+bb.sw] ;esi=firstLineStart
push ecx
push esi
mov ecx, [ebp+bb.w]
.do4.3:
xor edx, edx
mov al, [esi]
mov dl, al
inc esi
shr dl, 4
and eax, byte 15
add edi, byte 2
dec ecx
mov edx, [esp+edx*4+8]
mov eax, [esp+eax*4+8]
mov [edi-2], dx
jz .wend
mov [edi], ax
add edi, byte 2
dec ecx
jnz .do4.3
.wend: pop esi
pop ecx
add edi, [ebp+bb.s]
dec ecx
jnz .do4.2
add esp, 16*4 ;palette
ret
;---------------------------------------------------
; 1bpp BMP to 2byte buffer
;---------------------------------------------------
.bpp1:
;ecx=height, edx=0, ebx=palette
;esi=endOfImage, edi=destinationBuffer
;pallete変換
mov eax, [ebx]
add ebx, [ebp+bb.paletteSize]
call .paletteConv
push eax
mov eax, [ebx]
call .paletteConv
pop ebx
xchg eax, ebx
xor ebx, eax
push eax
.do1.1:
sub esi, [ebp+bb.sw] ;esi=firstLineStart
push ecx
push esi
mov ecx, [ebp+bb.w]
.do1.2:
mov dl, [esi]
inc esi
push esi
mov esi, 8
.do1.3:
add dl, dl
inc edi
sbb eax, eax
inc edi
and eax, ebx
xor eax, [ebp+bb.col0]
dec ecx
mov [edi-2], ax
jz .wend1bpp
dec esi
jnz .do1.3
pop esi
jmp short .do1.2
.wend1bpp:
pop ecx
pop esi
pop ecx
add edi, [ebp+bb.s]
dec ecx
jnz .do1.1
pop eax
ret
.paletteConv:
shr ah, 2
shr eax, 3
shl ax, 5
shr eax, 5
ret

BIN
30_day/gview/bmp.obj Normal file

Binary file not shown.

121
30_day/gview/gview.c Normal file
View File

@@ -0,0 +1,121 @@
#include "apilib.h"
struct DLL_STRPICENV { /* 64KB */
int work[64 * 1024 / 4];
};
struct RGB {
unsigned char b, g, r, t;
};
/* bmp.nasm */
int info_BMP(struct DLL_STRPICENV *env, int *info, int size, char *fp);
int decode0_BMP(struct DLL_STRPICENV *env, int size, char *fp, int b_type, char *buf, int skip);
/* jpeg.c */
int info_JPEG(struct DLL_STRPICENV *env, int *info, int size, char *fp);
int decode0_JPEG(struct DLL_STRPICENV *env, int size, char *fp, int b_type, char *buf, int skip);
unsigned char rgb2pal(int r, int g, int b, int x, int y);
void error(char *s);
void HariMain(void)
{
struct DLL_STRPICENV env;
char filebuf[512 * 1024], winbuf[1040 * 805];
char s[32], *p;
int win, i, j, fsize, xsize, info[8];
struct RGB picbuf[1024 * 768], *q;
/*命令行解析*/
api_cmdline(s, 30);
for (p = s; *p > ' '; p++) { } /*一直读到空格为止*/
for (; *p == ' '; p++) { } /*跳过空格*/
/*文件载入*/
i = api_fopen(p); if (i == 0) { error("file not found.\n"); }
fsize = api_fsize(i, 0);
if (fsize > 512 * 1024) {
error("file too large.\n");
}
api_fread(filebuf, fsize, i);
api_fclose(i);
/*检查文件类型*/
if (info_BMP(&env, info, fsize, filebuf) == 0) {
/*不是BMP */
if (info_JPEG(&env, info, fsize, filebuf) == 0) {
/*也不是JPEG */
api_putstr0("file type unknown.\n");
api_end();
}
}
/*上面其中一个info函数调用成功的话info中包含以下信息 */
/*info[0]文件类型1:BMP、2:JPEG*/
/*info[1]:颜色数信息*/
/*info[2]xsize */
/* info[3] : ysize */
if (info[2] > 1024 || info[3] > 768) {
error("picture too large.\n");
}
/*窗口准备*/
xsize = info[2] + 16;
if (xsize < 136) {
xsize = 136;
}
win = api_openwin(winbuf, xsize, info[3] + 37, -1, "gview");
/*将文件内容转换为图像数据*/
if (info[0] == 1) {
i = decode0_BMP (&env, fsize, filebuf, 4, (char *) picbuf, 0);
} else {
i = decode0_JPEG(&env, fsize, filebuf, 4, (char *) picbuf, 0);
}
/*b_type = 4表示struct RGB格式*/
/*skip设为0即可*/
if (i != 0) {
error("decode error.\n");
}
/*显示*/
for (i = 0; i < info[3]; i++) {
p = winbuf + (i + 29) * xsize + (xsize - info[2]) / 2;
q = picbuf + i * info[2];
for (j = 0; j < info[2]; j++) {
p[j] = rgb2pal(q[j].r, q[j].g, q[j].b, j, i);
}
}
api_refreshwin(win, (xsize - info[2]) / 2, 29, (xsize - info[2]) / 2 + info[2], 29 + info[3]);
/*等待结束*/
for (;;) {
i = api_getkey(1);
if (i == 'Q' || i == 'q') {
api_end();
}
}
}
unsigned char rgb2pal(int r, int g, int b, int x, int y)
{
static int table[4] = { 3, 1, 0, 2 };
int i;
x &= 1; /*判断是偶数还是奇数*/
y &= 1;
i = table[x + y * 2]; /*用于生成中间色的常量*/
r = (r * 21) / 256; /*结果为020*/
g = (g * 21) / 256;
b = (b * 21) / 256;
r = (r + i) / 4; /*结果为05*/
g = (g + i) / 4;
b = (b + i) / 4;
return 16 + r + g * 6 + b * 36;
}
void error(char *s)
{
api_putstr0(s);
api_end();
}

733
30_day/gview/jpeg.c Normal file
View File

@@ -0,0 +1,733 @@
/*
* JPEG decoding engine for DCT-baseline
*
* copyrights 2003 by nikq | nikq::club.
*
*
*/
typedef unsigned char UCHAR;
struct DLL_STRPICENV { int work[16384]; };
typedef struct
{
int elem;
unsigned short code[256];
unsigned char size[256];
unsigned char value[256];
}HUFF;
typedef struct
{
// SOF
int width;
int height;
// MCU
int mcu_width;
int mcu_height;
int max_h,max_v;
int compo_count;
int compo_id[3];
int compo_sample[3];
int compo_h[3];
int compo_v[3];
int compo_qt[3];
// SOS
int scan_count;
int scan_id[3];
int scan_ac[3];
int scan_dc[3];
int scan_h[3];
int scan_v[3];
int scan_qt[3];
// DRI
int interval;
int mcu_buf[32*32*4];
int *mcu_yuv[4];
int mcu_preDC[3];
// DQT
int dqt[3][64];
int n_dqt;
// DHT
HUFF huff[2][3];
// FILE i/o
unsigned char *fp, *fp1;
unsigned long bit_buff;
int bit_remain;
int width_buf;
int base_img[64][64];
/* for dll
JPEG *jpeg = (JPEG *)malloc(sizeof(JPEG) + 256);
*/
int dummy[64];
}JPEG;
/* for 16bit */
#ifndef PIXEL16
#define PIXEL16(r, g, b) ((r) << 11 | (g) << 5 | (b))
/* 0 <= r <= 31, 0 <= g <= 63, 0 <= b <= 31 */
#endif
int info_JPEG(struct DLL_STRPICENV *env, int *info, int size, UCHAR *fp);
int decode0_JPEG(struct DLL_STRPICENV *env, int size, UCHAR *fp, int b_type, UCHAR *buf, int skip);
void jpeg_idct_init(int base_img[64][64]);
int jpeg_init(JPEG *jpeg);
// int jpeg_header(JPEG *jpge);
void jpeg_decode(JPEG *jpeg, unsigned char *rgb,int b_type);
/* ----------------- start main section ----------------- */
int info_JPEG(struct DLL_STRPICENV *env,int *info, int size, UCHAR *fp0)
{
JPEG *jpeg = (JPEG *) (((int *) env) + 128);
jpeg->fp = fp0;
jpeg->fp1 = fp0 + size;
// if (512 + sizeof (JPEG) > 64 * 1024)
// return 0;
if (jpeg_init(jpeg))
return 0;
// jpeg_header(jpeg);
if (jpeg->width == 0)
return 0;
info[0] = 0x0002;
info[1] = 0x0000;
info[2] = jpeg->width;
info[3] = jpeg->height;
/* OK */
return 1;
}
int decode0_JPEG(struct DLL_STRPICENV *env,int size, UCHAR *fp0, int b_type, UCHAR *buf, int skip)
{
JPEG *jpeg = (JPEG *) (((int *) env) + 128);
jpeg->fp = fp0;
jpeg->fp1 = fp0 + size;
jpeg_idct_init(jpeg->base_img);
jpeg_init(jpeg);
// jpeg_header(jpeg);
// if (jpeg->width == 0)
// return 8;
/* decode*/
jpeg->width_buf = skip / (b_type & 0x7f) + jpeg->width;
jpeg_decode(jpeg, buf, b_type);
/* OK */
return 0;
}
// -------------------------- I/O ----------------------------
unsigned short get_bits(JPEG *jpeg, int bit)
{
unsigned char c, c2;
unsigned short ret;
unsigned long buff;
int remain;
buff = jpeg->bit_buff;
remain = jpeg->bit_remain;
while (remain <= 16) {
if (jpeg->fp >= jpeg->fp1) {
ret = 0;
goto fin;
}
c = *jpeg->fp++;
if (c == 0xff) {
if (jpeg->fp >= jpeg->fp1) {
ret = 0;
goto fin;
}
jpeg->fp++; /* 00 skip */
}
buff = (buff << 8) | c;
remain += 8;
}
ret = (buff >> (remain - bit)) & ((1 << bit) - 1);
remain -= bit;
jpeg->bit_remain = remain;
jpeg->bit_buff = buff;
fin:
return ret;
}
// ------------------------ JPEG -----------------
// start of frame
int jpeg_sof(JPEG *jpeg)
{
unsigned char c;
int i, h, v, n;
if (jpeg->fp + 8 > jpeg->fp1)
goto err;
/* fp[2] ‚Í bpp */
jpeg->height = jpeg->fp[3] << 8 | jpeg->fp[4];
jpeg->width = jpeg->fp[5] << 8 | jpeg->fp[6];
n = jpeg->compo_count = jpeg->fp[7]; // Num of compo, nf
jpeg->fp += 8;
if (jpeg->fp + n * 3 > jpeg->fp1)
goto err;
for (i = 0; i < n; i++) {
jpeg->compo_id[i] = jpeg->fp[0];
jpeg->compo_sample[i] = c = jpeg->fp[1];
h = jpeg->compo_h[i] = (c >> 4) & 0x0f;
v = jpeg->compo_v[i] = c & 0x0f;
if (jpeg->max_h < h)
jpeg->max_h = h;
if (jpeg->max_v < v)
jpeg->max_v = v;
jpeg->compo_qt[i] = jpeg->fp[2];
jpeg->fp += 3;
}
return 0;
err:
jpeg->fp = jpeg->fp1;
return 1;
}
// define quantize table
int jpeg_dqt(JPEG *jpeg)
{
unsigned char c;
int i, j, v, size;
if (jpeg->fp + 2 > jpeg->fp1)
goto err;
size = (jpeg->fp[0] << 8 | jpeg->fp[1]) - 2;
jpeg->fp += 2;
if (jpeg->fp + size > jpeg->fp1)
goto err;
while (size > 0) {
c = *jpeg->fp++;
size--;
j = c & 7;
if (j > jpeg->n_dqt)
jpeg->n_dqt = j;
if (c & 0xf8) {
// 16 bit DQT
for (i = 0; i < 64; i++) {
jpeg->dqt[j][i] = jpeg->fp[0];
jpeg->fp += 2;
}
size += -64 * 2;
} else {
// 8 bit DQT
for (i = 0; i < 64; i++)
jpeg->dqt[j][i] = *jpeg->fp++;
size -= 64;
}
}
return 0;
err:
jpeg->fp = jpeg->fp1;
return 1;
}
// define huffman table
int jpeg_dht(JPEG *jpeg)
{
unsigned tc, th;
unsigned code = 0;
unsigned char val;
int i, j, k, num, Li[17];
int len, max_val;
HUFF *table;
if (jpeg->fp + 2 > jpeg->fp1)
goto err;
len = (jpeg->fp[0] << 8 | jpeg->fp[1]) - 2;
jpeg->fp += 2;
while (len > 0) {
if (jpeg->fp + 17 > jpeg->fp1)
goto err;
val = jpeg->fp[0];
tc = (val >> 4) & 0x0f;
th = val & 0x0f;
table = &(jpeg->huff[tc][th]);
num = 0;
k = 0;
for (i = 1; i <= 16; i++) {
Li[i] = jpeg->fp[i];
num += Li[i];
for (j = 0; j < Li[i]; j++)
table->size[k++] = i;
}
table->elem = num;
jpeg->fp += 17;
k=0;
code=0;
i = table->size[0];
while (k < num) {
while (table->size[k] == i)
table->code[k++] = code++;
if (k >= num)
break;
do {
code <<= 1;
i++;
} while (table->size[k] != i);
}
if (jpeg->fp + num > jpeg->fp1)
goto err;
for (k = 0; k < num; k++)
table->value[k] = jpeg->fp[k];
jpeg->fp += num;
len -= 18 + num;
}
return 0;
err:
jpeg->fp = jpeg->fp1;
return 1;
}
int jpeg_init(JPEG *jpeg)
{
unsigned char c;
int r = 0, i;
jpeg->width = 0;
jpeg->mcu_preDC[0] = 0;
jpeg->mcu_preDC[1] = 0;
jpeg->mcu_preDC[2] = 0;
jpeg->n_dqt = 0;
jpeg->max_h = 0;
jpeg->max_v = 0;
jpeg->bit_remain = 0;
jpeg->bit_buff = 0;
jpeg->interval = 0;
// return;
//}
//
//int jpeg_header(JPEG *jpeg)
//{
// unsigned char c;
// int r = 0, i;
for (;;) {
if (jpeg->fp + 2 > jpeg->fp1)
goto err;
if (jpeg->fp[0] != 0xff)
goto err0;
c = jpeg->fp[1];
jpeg->fp += 2;
if (c == 0xd8)
continue; /* SOI */
if (c == 0xd9)
goto err; /* EOI */
if (c == 0xc0)
jpeg_sof(jpeg);
else if (c == 0xc4)
jpeg_dht(jpeg);
else if (c == 0xdb)
jpeg_dqt(jpeg);
else if (c == 0xdd) {
if (jpeg->fp + 4 > jpeg->fp1)
goto err;
jpeg->interval = jpeg->fp[2] << 8 | jpeg->fp[3];
jpeg->fp += 4;
} else if (c == 0xda) {
if (jpeg->fp + 3 > jpeg->fp1)
goto err;
jpeg->scan_count = jpeg->fp[2];
jpeg->fp += 3;
if (jpeg->fp + jpeg->scan_count * 2 > jpeg->fp1)
goto err;
for (i = 0; i < jpeg->scan_count; i++) {
jpeg->scan_id[i] = jpeg->fp[0];
jpeg->scan_dc[i] = jpeg->fp[1] >> 4; // DC Huffman Table
jpeg->scan_ac[i] = jpeg->fp[1] & 0x0F; // AC Huffman Table
jpeg->fp += 2;
}
jpeg->fp += 3; /* 3bytes skip */
goto fin;
} else {
if (jpeg->fp + 2 > jpeg->fp1)
goto err;
jpeg->fp += jpeg->fp[0] << 8 | jpeg->fp[1];
}
}
err:
jpeg->fp = jpeg->fp1;
err0:
r++;
fin:
return r;
}
// MCU decode
void jpeg_decode_init(JPEG *jpeg)
{
int i, j;
for (i = 0; i < jpeg->scan_count; i++) {
// i:scan
for (j = 0; j < jpeg->compo_count; j++) {
// j:frame
if (jpeg->scan_id[i] == jpeg->compo_id[j]) {
jpeg->scan_h[i] = jpeg->compo_h[j];
jpeg->scan_v[i] = jpeg->compo_v[j];
jpeg->scan_qt[i] = jpeg->compo_qt[j];
break;
}
}
// if (j >= jpeg->compo_count)
// return 1;
}
jpeg->mcu_width = jpeg->max_h * 8;
jpeg->mcu_height = jpeg->max_v * 8;
for (i = 0; i < 32 * 32 * 4; i++)
jpeg->mcu_buf[i] = 0x80;
for (i = 0; i < jpeg->scan_count; i++)
jpeg->mcu_yuv[i] = jpeg->mcu_buf + (i << 10);
return;
}
int jpeg_huff_decode(JPEG *jpeg,int tc,int th)
{
HUFF *h = &(jpeg->huff[tc][th]);
int code,size,k,v;
code = 0;
size = 0;
k = 0;
while( size < 16 ){
size++;
v = get_bits(jpeg,1);
if(v < 0){
return v;
}
code = (code << 1) | v;
while(h->size[k] == size){
if(h->code[k] == code){
return h->value[k];
}
k++;
}
}
return -1;
}
void jpeg_idct_init(int base_img[64][64])
{
int u, v, m, n;
int tmpm[8], tmpn[8];
int cost[32];
cost[ 0] = 32768; cost[ 1] = 32138; cost[ 2] = 30274; cost[ 3] = 27246; cost[ 4] = 23170; cost[ 5] = 18205; cost[ 6] = 12540; cost[ 7] = 6393;
cost[ 8] = 0; cost[ 9] = -6393; cost[10] = -12540; cost[11] = -18205; cost[12] = -23170; cost[13] = -27246; cost[14] = -30274; cost[15] = -32138;
for (u = 0; u < 16; u++)
cost[u + 16] = - cost[u];
for (u = 0; u < 8; u++) {
{
int i=u, d=u*2;
if (d == 0)
i = 4;
for (m = 0; m < 8; m++){
tmpm[m] = cost[i];
i=(i+d)&31;
}
}
for (v = 0; v < 8; v++) {
{
int i=v,d=v*2;
if (d == 0)
i=4;
for (n = 0; n < 8; n++){
tmpn[n] = cost[i];
i=(i+d)&31;
}
}
for (m = 0; m < 8; m++) {
for (n = 0; n < 8; n++) {
base_img[u * 8 + v][m * 8 + n] = (tmpm[m] * tmpn[n])>>15;
}
}
}
}
return;
}
void jpeg_idct(int *block, int *dest, int base_img[64][64])
{
int i, j ,k;
for (i = 0; i < 64; i++)
dest[i] = 0;
for (i = 0; i < 64; i++) {
k = block[i];
if(k) {
for (j = 0; j < 64; j++) {
dest[j] += k * base_img[i][j];
}
}
}
for (i = 0; i < 64; i++)
dest[i] >>= 17;
return;
}
int jpeg_get_value(JPEG *jpeg,int size)
{
int val = 0;
if (size) {
val = get_bits(jpeg,size);
if (val < 0)
val = 0x10000 | (0 - val);
else if (!(val & (1<<(size-1))))
val -= (1 << size) - 1;
}
return val;
}
int jpeg_decode_huff(JPEG *jpeg,int scan,int *block, UCHAR *zigzag_table)
{
int size, len, val, run, index;
int *pQt = (int *)(jpeg->dqt[jpeg->scan_qt[scan]]);
// DC
size = jpeg_huff_decode(jpeg,0,jpeg->scan_dc[scan]);
if(size < 0)
return 0;
val = jpeg_get_value(jpeg,size);
jpeg->mcu_preDC[scan] += val;
block[0] = jpeg->mcu_preDC[scan] * pQt[0];
//AC
index = 1;
while(index<64)
{
size = jpeg_huff_decode(jpeg,1,jpeg->scan_ac[scan]);
if(size < 0)
break;
// EOB
if(size == 0)
break;
// RLE
run = (size>>4)&0xF;
size = size & 0x0F;
val = jpeg_get_value(jpeg,size);
if(val>=0x10000) {
return val;
}
// ZRL
while (run-- > 0)
block[ zigzag_table[index++] ] = 0;
block[ zigzag_table[index] ] = val * pQt[index];
index++;
}
while(index<64)
block[zigzag_table[index++]]=0;
return 0;
}
void jpeg_mcu_bitblt(int *src, int *dest, int width,
int x0, int y0, int x1, int y1)
{
int w, h;
int x, y, x2, y2;
w = x1 - x0;
h = y1 - y0;
dest += y0 * width + x0;
width -= w;
for (y = 0; y < h; y++) {
y2 = (y * 8 / h) * 8;
for (x = 0; x < w; x++)
*dest++ = src[y2 + (x * 8 / w)];
dest += width;
}
}
int jpeg_decode_mcu(JPEG *jpeg, UCHAR *zigzag_table)
{
int scan, val;
int unit, i, h, v;
int *p, hh, vv;
int block[64], dest[64];
// mcu_width x mcu_height
for (scan = 0; scan < jpeg->scan_count; scan++) {
hh = jpeg->scan_h[scan];
vv = jpeg->scan_v[scan];
for (v = 0; v < vv; v++) {
for (h = 0; h < hh; h++) {
// ƒuƒƒbƒN(8x8)̃fƒR[ƒh
val = jpeg_decode_huff(jpeg, scan, block, zigzag_table);
// if(val>=0x10000){
// printf("marker found:%02x\n",val);
// }
jpeg_idct(block, dest, jpeg->base_img);
p = jpeg->mcu_buf + (scan << 10);
jpeg_mcu_bitblt(dest, p, jpeg->mcu_width,
jpeg->mcu_width * h / hh, jpeg->mcu_height * v / vv,
jpeg->mcu_width * (h + 1) / hh, jpeg->mcu_height * (v + 1) / vv);
}
}
}
}
// YCrCb=>RGB
int jpeg_decode_yuv(JPEG *jpeg, int h, int v, unsigned char *rgb, int b_type)
{
int x0, y0, x, y, x1, y1;
int *py;
int Y12, V;
int mw, mh, w;
int i;
mw = jpeg->mcu_width;
mh = jpeg->mcu_height;
x0 = h * jpeg->max_h * 8;
y0 = v * jpeg->max_v * 8;
x1 = jpeg->width - x0;
if (x1 > mw)
x1 = mw;
y1 = jpeg->height - y0;
if (y1 > mh)
y1 = mh;
py = jpeg->mcu_buf;
rgb += (y0 * jpeg->width_buf + x0) * (b_type & 0x7f);
w = (jpeg->width_buf - x1) * (b_type & 0x7f);
for (y = 0; y < y1; y++) {
for (x = 0; x < x1; x++) {
int b, g, r;
Y12 = py[0] << 12;
// U = py[1024]; /* pu */
V = py[2048]; /* pv */
/* blue */
b = 128 + ((Y12 - V * 4 + py[1024] /* pu */ * 0x1C59) >> 12);
if (b & 0xffffff00)
b = (~b) >> 24;
/* green */
g = 128 + ((Y12 - V * 0x0B6C) >> 12);
if (g & 0xffffff00)
g = (~g) >> 24;
/* red */
r = 128 + ((Y12 + V * 0x166E) >> 12);
if (r & 0xffffff00)
r = (~r) >> 24;
if (b_type == 0x0004) {
rgb[0] = b;
rgb[1] = g;
rgb[2] = r;
py++;
rgb += 4;
} else if (b_type == 0x0002) {
r &= 0xff;
g &= 0xff;
b &= 0xff;
*(short *) rgb = PIXEL16(r >> 3, g >> 2, b >> 3);
rgb += 2;
}
}
py += mw - x1;
rgb += w;
}
return;
}
#define INIT_ZTABLE(i, b0, b1, b2, b3) *(int *) &zigzag_table[i] = b0 | b1 << 8 | b2 << 16 | b3 << 24
void jpeg_decode(JPEG *jpeg, UCHAR *rgb, int b_type)
{
int h_unit, v_unit;
int mcu_count, h, v;
int val;
unsigned char m;
UCHAR zigzag_table[64];
INIT_ZTABLE( 0, 0, 1, 8, 16); INIT_ZTABLE( 4, 9, 2, 3, 10);
INIT_ZTABLE( 8, 17, 24, 32, 25); INIT_ZTABLE(12, 18, 11, 4, 5);
INIT_ZTABLE(16, 12, 19, 26, 33); INIT_ZTABLE(20, 40, 48, 41, 34);
INIT_ZTABLE(24, 27, 20, 13, 6); INIT_ZTABLE(28, 7, 14, 21, 28);
INIT_ZTABLE(32, 35, 42, 49, 56); INIT_ZTABLE(36, 57, 50, 43, 36);
INIT_ZTABLE(40, 29, 22, 15, 23); INIT_ZTABLE(44, 30, 37, 44, 51);
INIT_ZTABLE(48, 58, 59, 52, 45); INIT_ZTABLE(52, 38, 31, 39, 46);
INIT_ZTABLE(56, 53, 60, 61, 54); INIT_ZTABLE(60, 47, 55, 62, 63);
jpeg_decode_init(jpeg);
h_unit = (jpeg->width + jpeg->mcu_width - 1) / jpeg->mcu_width;
v_unit = (jpeg->height + jpeg->mcu_height - 1) / jpeg->mcu_height;
mcu_count = 0;
for (v = 0; v < v_unit; v++) {
for (h = 0; h < h_unit; h++) {
mcu_count++;
jpeg_decode_mcu(jpeg, zigzag_table);
jpeg_decode_yuv(jpeg, h, v, rgb, b_type & 0x7fff);
if (jpeg->interval > 0 && mcu_count >= jpeg->interval) {
jpeg->bit_remain -= (jpeg->bit_remain & 7);
jpeg->bit_remain -= 8;
jpeg->mcu_preDC[0] = 0;
jpeg->mcu_preDC[1] = 0;
jpeg->mcu_preDC[2] = 0;
mcu_count = 0;
}
}
}
return;
}

1
30_day/gview/make.bat Normal file
View File

@@ -0,0 +1 @@
..\..\z_tools\make.exe %1 %2 %3 %4 %5 %6 %7 %8 %9

10
30_day/haribote.rul Normal file
View File

@@ -0,0 +1,10 @@
format:
code(align:1, logic:0x24, file:0x24);
data(align:4, logic:stack_end, file:code_end);
file:
../../z_tools/haribote/harilibc.lib;
../../z_tools/haribote/golibc.lib;
label:
_HariStartup;

View File

@@ -0,0 +1 @@
command

View File

@@ -0,0 +1 @@
cmd.exe

79
30_day/haribote/Makefile Normal file
View File

@@ -0,0 +1,79 @@
OBJS_BOOTPACK = bootpack.obj naskfunc.obj hankaku.obj graphic.obj dsctbl.obj \
int.obj fifo.obj keyboard.obj mouse.obj memory.obj sheet.obj timer.obj \
mtask.obj window.obj console.obj file.obj tek.obj
TOOLPATH = ../../z_tools/
INCPATH = ../../z_tools/haribote/
MAKE = $(TOOLPATH)make.exe -r
NASK = $(TOOLPATH)nask.exe
CC1 = $(TOOLPATH)cc1.exe -I$(INCPATH) -Os -Wall -quiet
GAS2NASK = $(TOOLPATH)gas2nask.exe -a
OBJ2BIM = $(TOOLPATH)obj2bim.exe
MAKEFONT = $(TOOLPATH)makefont.exe
BIN2OBJ = $(TOOLPATH)bin2obj.exe
BIM2HRB = $(TOOLPATH)bim2hrb.exe
RULEFILE = ../haribote.rul
EDIMG = $(TOOLPATH)edimg.exe
IMGTOL = $(TOOLPATH)imgtol.com
GOLIB = $(TOOLPATH)golib00.exe
COPY = copy
DEL = del
#默认动作
default :
$(MAKE) ipl10.bin
$(MAKE) haribote.sys
# 镜像文件生成
ipl10.bin : ipl10.nas Makefile
$(NASK) ipl10.nas ipl10.bin ipl10.lst
asmhead.bin : asmhead.nas Makefile
$(NASK) asmhead.nas asmhead.bin asmhead.lst
hankaku.bin : hankaku.txt Makefile
$(MAKEFONT) hankaku.txt hankaku.bin
hankaku.obj : hankaku.bin Makefile
$(BIN2OBJ) hankaku.bin hankaku.obj _hankaku
bootpack.bim : $(OBJS_BOOTPACK) Makefile
$(OBJ2BIM) @$(RULEFILE) out:bootpack.bim stack:3136k map:bootpack.map \
$(OBJS_BOOTPACK)
# 3MB+64KB=3136KB
bootpack.hrb : bootpack.bim Makefile
$(BIM2HRB) bootpack.bim bootpack.hrb 0
haribote.sys : asmhead.bin bootpack.hrb Makefile
copy /B asmhead.bin+bootpack.hrb haribote.sys
# 其他指令
%.gas : %.c bootpack.h Makefile
$(CC1) -o $*.gas $*.c
%.nas : %.gas Makefile
$(GAS2NASK) $*.gas $*.nas
%.obj : %.nas Makefile
$(NASK) $*.nas $*.obj $*.lst
# 运行程序
clean :
-$(DEL) asmhead.bin
-$(DEL) hankaku.bin
-$(DEL) *.lst
-$(DEL) *.obj
-$(DEL) *.map
-$(DEL) *.bim
-$(DEL) *.hrb
src_only :
$(MAKE) clean
-$(DEL) ipl10.bin
-$(DEL) haribote.sys

202
30_day/haribote/asmhead.nas Normal file
View File

@@ -0,0 +1,202 @@
; haribote-os boot asm
; TAB=4
[INSTRSET "i486p"]
VBEMODE EQU 0x105 ; 1024 x 768 x 8bit 彩色
; 显示模式
; 0x100 : 640 x 400 x 8bit 彩色
; 0x101 : 640 x 480 x 8bit 彩色
; 0x103 : 800 x 600 x 8bit 彩色
; 0x105 : 1024 x 768 x 8bit 彩色
; 0x107 : 1280 x 1024 x 8bit 彩色
BOTPAK EQU 0x00280000 ; 加载bootpack
DSKCAC EQU 0x00100000 ; 磁盘缓存的位置
DSKCAC0 EQU 0x00008000 ; 磁盘缓存的位置(实模式)
; BOOT_INFO 相关
CYLS EQU 0x0ff0 ; 引导扇区设置
LEDS EQU 0x0ff1
VMODE EQU 0x0ff2 ; 关于颜色的信息
SCRNX EQU 0x0ff4 ; 分辨率X
SCRNY EQU 0x0ff6 ; 分辨率Y
VRAM EQU 0x0ff8 ; 图像缓冲区的起始地址
ORG 0xc200 ; 这个的程序要被装载的内存地址
; 确认VBE是否存在
MOV AX,0x9000
MOV ES,AX
MOV DI,0
MOV AX,0x4f00
INT 0x10
CMP AX,0x004f
JNE scrn320
; 检查VBE的版本
MOV AX,[ES:DI+4]
CMP AX,0x0200
JB scrn320 ; if (AX < 0x0200) goto scrn320
; 取得画面模式信息
MOV CX,VBEMODE
MOV AX,0x4f01
INT 0x10
CMP AX,0x004f
JNE scrn320
; 画面模式信息的确认
CMP BYTE [ES:DI+0x19],8 ;颜色数必须为8
JNE scrn320
CMP BYTE [ES:DI+0x1b],4 ;颜色的指定方法必须为4(4是调色板模式)
JNE scrn320
MOV AX,[ES:DI+0x00] ;模式属性bit7不是1就不能加上0x4000
AND AX,0x0080
JZ scrn320 ; 模式属性的bit7是0所以放弃
; 画面设置
MOV BX,VBEMODE+0x4000
MOV AX,0x4f02
INT 0x10
MOV BYTE [VMODE],8 ; 屏幕的模式参考C语言的引用
MOV AX,[ES:DI+0x12]
MOV [SCRNX],AX
MOV AX,[ES:DI+0x14]
MOV [SCRNY],AX
MOV EAX,[ES:DI+0x28] ;VRAM的地址
MOV [VRAM],EAX
JMP keystatus
scrn320:
MOV AL,0x13 ; VGA图、320x200x8bit彩色
MOV AH,0x00
INT 0x10
MOV BYTE [VMODE],8 ; 记下画面模式参考C语言
MOV WORD [SCRNX],320
MOV WORD [SCRNY],200
MOV DWORD [VRAM],0x000a0000
; 通过 BIOS 获取指示灯状态
keystatus:
MOV AH,0x02
INT 0x16 ; keyboard BIOS
MOV [LEDS],AL
; PIC关闭一切中断
; 根据AT兼容机的规格如果要初始化PIC
; 必须在CLI之前进行否则有时会挂起。
; 随后进行PIC的初始化。
MOV AL,0xff
OUT 0x21,AL
NOP ; 如果连续执行OUT指令有些机种会无法正常运行
OUT 0xa1,AL
CLI ; 禁止CPU级别的中断
; 为了让CPU能够访问1MB以上的内存空间设定A20GATE
CALL waitkbdout
MOV AL,0xd1
OUT 0x64,AL
CALL waitkbdout
MOV AL,0xdf ; enable A20
OUT 0x60,AL
CALL waitkbdout
; 切换到保护模式
[INSTRSET "i486p"] ; 说明使用486指令
LGDT [GDTR0] ; 设置临时GDT
MOV EAX,CR0
AND EAX,0x7fffffff ; 设bit31为0禁用分页
OR EAX,0x00000001 ; bit0到1转换保护模式过渡
MOV CR0,EAX
JMP pipelineflush
pipelineflush:
MOV AX,1*8 ; 可读写的段 32bit
MOV DS,AX
MOV ES,AX
MOV FS,AX
MOV GS,AX
MOV SS,AX
; bootpack传递
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 ; 从柱面数变换为字节数/4
SUB ECX,512/4 ; 减去 IPL 偏移量
CALL memcpy
; 必须由asmhead来完成的工作至此全部完毕
; 以后就交由bootpack来完成
; bootpack启动
MOV EBX,BOTPAK
MOV ECX,[EBX+16]
ADD ECX,3 ; ECX += 3;
SHR ECX,2 ; ECX /= 4;
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 ; AND的结果如果不是0就跳到waitkbdout
RET
memcpy:
MOV EAX,[ESI]
ADD ESI,4
MOV [EDI],EAX
ADD EDI,4
SUB ECX,1
JNZ memcpy ; 减法运算的结果如果不是0就跳转到memcpy
RET
; memcpy地址前缀大小
ALIGNB 16
GDT0:
RESB 8 ; 初始值
DW 0xffff,0x0000,0x9200,0x00cf ; 可以读写的段segment32bit
DW 0xffff,0x0000,0x9a28,0x0047 ; 可执行的文件的32bit寄存器bootpack用
DW 0
GDTR0:
DW 8*3-1
DD GDT0
ALIGNB 16
bootpack:

412
30_day/haribote/bootpack.c Normal file
View File

@@ -0,0 +1,412 @@
/* bootpack */
#include "bootpack.h"
#include <stdio.h>
#define KEYCMD_LED 0xed
void keywin_off(struct SHEET *key_win);
void keywin_on(struct SHEET *key_win);
void close_console(struct SHEET *sht);
void close_constask(struct TASK *task);
void HariMain(void)
{
struct BOOTINFO *binfo = (struct BOOTINFO *) ADR_BOOTINFO;
struct SHTCTL *shtctl;
char s[40];
struct FIFO32 fifo, keycmd;
int fifobuf[128], keycmd_buf[32];
int mx, my, i, new_mx = -1, new_my = 0, new_wx = 0x7fffffff, new_wy = 0;
unsigned int memtotal;
struct MOUSE_DEC mdec;
struct MEMMAN *memman = (struct MEMMAN *) MEMMAN_ADDR;
unsigned char *buf_back, buf_mouse[256];
struct SHEET *sht_back, *sht_mouse;
struct TASK *task_a, *task;
static char keytable0[0x80] = {
0, 0, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '^', 0x08, 0,
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '@', '[', 0x0a, 0, 'A', 'S',
'D', 'F', 'G', 'H', 'J', 'K', 'L', ';', ':', 0, 0, ']', 'Z', 'X', 'C', 'V',
'B', 'N', 'M', ',', '.', '/', 0, '*', 0, ' ', 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1',
'2', '3', '0', '.', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0x5c, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x5c, 0, 0
};
static char keytable1[0x80] = {
0, 0, '!', 0x22, '#', '$', '%', '&', 0x27, '(', ')', '~', '=', '~', 0x08, 0,
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '`', '{', 0x0a, 0, 'A', 'S',
'D', 'F', 'G', 'H', 'J', 'K', 'L', '+', '*', 0, 0, '}', 'Z', 'X', 'C', 'V',
'B', 'N', 'M', '<', '>', '?', 0, '*', 0, ' ', 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1',
'2', '3', '0', '.', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, '_', 0, 0, 0, 0, 0, 0, 0, 0, 0, '|', 0, 0
};
int key_shift = 0, key_leds = (binfo->leds >> 4) & 7, keycmd_wait = -1;
int j, x, y, mmx = -1, mmy = -1, mmx2 = 0;
struct SHEET *sht = 0, *key_win, *sht2;
int *fat;
unsigned char *nihongo;
struct FILEINFO *finfo;
extern char hankaku[4096];
init_gdtidt();
init_pic();
io_sti(); /* IDT/PIC的初始化已经完成于是开放CPU的中断 */
fifo32_init(&fifo, 128, fifobuf, 0);
*((int *) 0x0fec) = (int) &fifo;
init_pit();
init_keyboard(&fifo, 256);
enable_mouse(&fifo, 512, &mdec);
io_out8(PIC0_IMR, 0xf8); /* 设定PIT和PIC1以及键盘为许可(11111000) */
io_out8(PIC1_IMR, 0xef); /* 开放鼠标中断(11101111) */
fifo32_init(&keycmd, 32, keycmd_buf, 0);
memtotal = memtest(0x00400000, 0xbfffffff);
memman_init(memman);
memman_free(memman, 0x00001000, 0x0009e000); /* 0x00001000 - 0x0009efff */
memman_free(memman, 0x00400000, memtotal - 0x00400000);
init_palette();
shtctl = shtctl_init(memman, binfo->vram, binfo->scrnx, binfo->scrny);
task_a = task_init(memman);
fifo.task = task_a;
task_run(task_a, 1, 2);
*((int *) 0x0fe4) = (int) shtctl;
task_a->langmode = 0;
/* sht_back */
sht_back = sheet_alloc(shtctl);
buf_back = (unsigned char *) memman_alloc_4k(memman, binfo->scrnx * binfo->scrny);
sheet_setbuf(sht_back, buf_back, binfo->scrnx, binfo->scrny, -1); /* 无透明色 */
init_screen8(buf_back, binfo->scrnx, binfo->scrny);
/* sht_cons */
key_win = open_console(shtctl, memtotal);
/* sht_mouse */
sht_mouse = sheet_alloc(shtctl);
sheet_setbuf(sht_mouse, buf_mouse, 16, 16, 99);
init_mouse_cursor8(buf_mouse, 99);
mx = (binfo->scrnx - 16) / 2; /* 计算坐标使其位于画面中央 */
my = (binfo->scrny - 28 - 16) / 2;
sheet_slide(sht_back, 0, 0);
sheet_slide(key_win, 32, 4);
sheet_slide(sht_mouse, mx, my);
sheet_updown(sht_back, 0);
sheet_updown(key_win, 1);
sheet_updown(sht_mouse, 2);
keywin_on(key_win);
/* 为了避免和键盘当前状态冲突,在一开始先进行设置 */
fifo32_put(&keycmd, KEYCMD_LED);
fifo32_put(&keycmd, key_leds);
/* 载入nihongo.fnt */
fat = (int *) memman_alloc_4k(memman, 4 * 2880);
file_readfat(fat, (unsigned char *) (ADR_DISKIMG + 0x000200));
finfo = file_search("nihongo.fnt", (struct FILEINFO *) (ADR_DISKIMG + 0x002600), 224);
if (finfo != 0) {
i = finfo->size;
nihongo = file_loadfile2(finfo->clustno, &i, fat);
} else {
nihongo = (unsigned char *) memman_alloc_4k(memman, 16 * 256 + 32 * 94 * 47);
for (i = 0; i < 16 * 256; i++) {
nihongo[i] = hankaku[i]; /*没有字库,半角部分直接复制英文字库*/
}
for (i = 16 * 256; i < 16 * 256 + 32 * 94 * 47; i++) {
nihongo[i] = 0xff; /* 没有字库全角部分以0xff填充 */
}
}
*((int *) 0x0fe8) = (int) nihongo;
memman_free_4k(memman, (int) fat, 4 * 2880);
for (;;) {
if (fifo32_status(&keycmd) > 0 && keycmd_wait < 0) {
/* 如果存在向键盘控制器发送的数据,则发送它 */
keycmd_wait = fifo32_get(&keycmd);
wait_KBC_sendready();
io_out8(PORT_KEYDAT, keycmd_wait);
}
io_cli();
if (fifo32_status(&fifo) == 0) {
/* FIFO为空当存在搁置的绘图操作时立即执行*/
if (new_mx >= 0) {
io_sti();
sheet_slide(sht_mouse, new_mx, new_my);
new_mx = -1;
} else if (new_wx != 0x7fffffff) {
io_sti();
sheet_slide(sht, new_wx, new_wy);
new_wx = 0x7fffffff;
} else {
task_sleep(task_a);
io_sti();
}
} else {
i = fifo32_get(&fifo);
io_sti();
if (key_win != 0 && key_win->flags == 0) { /*窗口被关闭*/
if (shtctl->top == 1) { /*当画面上只剩鼠标和背景时*/
key_win = 0;
} else {
key_win = shtctl->sheets[shtctl->top - 1];
keywin_on(key_win);
}
}
if (256 <= i && i <= 511) { /* 键盘数据*/
if (i < 0x80 + 256) { /*将按键编码转换为字符编码*/
if (key_shift == 0) {
s[0] = keytable0[i - 256];
} else {
s[0] = keytable1[i - 256];
}
} else {
s[0] = 0;
}
if ('A' <= s[0] && s[0] <= 'Z') { /*当输入字符为英文字母时*/
if (((key_leds & 4) == 0 && key_shift == 0) ||
((key_leds & 4) != 0 && key_shift != 0)) {
s[0] += 0x20; /*将大写字母转换为小写字母*/
}
}
if (s[0] != 0 && key_win != 0) { /*一般字符、退格键、回车键*/
fifo32_put(&key_win->task->fifo, s[0] + 256);
}
if (i == 256 + 0x0f && key_win != 0) { /* Tab键 */
keywin_off(key_win);
j = key_win->height - 1;
if (j == 0) {
j = shtctl->top - 1;
}
key_win = shtctl->sheets[j];
keywin_on(key_win);
}
if (i == 256 + 0x2a) { /*左Shift ON */
key_shift |= 1;
}
if (i == 256 + 0x36) { /*右Shift ON */
key_shift |= 2;
}
if (i == 256 + 0xaa) { /*左Shift OFF */
key_shift &= ~1;
}
if (i == 256 + 0xb6) { /*右Shift OFF */
key_shift &= ~2;
}
if (i == 256 + 0x3a) { /* CapsLock */
key_leds ^= 4;
fifo32_put(&keycmd, KEYCMD_LED);
fifo32_put(&keycmd, key_leds);
}
if (i == 256 + 0x45) { /* NumLock */
key_leds ^= 2;
fifo32_put(&keycmd, KEYCMD_LED);
fifo32_put(&keycmd, key_leds);
}
if (i == 256 + 0x46) { /* ScrollLock */
key_leds ^= 1;
fifo32_put(&keycmd, KEYCMD_LED);
fifo32_put(&keycmd, key_leds);
}
if (i == 256 + 0x3b && key_shift != 0 && key_win != 0) { /* Shift+F1 */
task = key_win->task;
if (task != 0 && task->tss.ss0 != 0) {
cons_putstr0(task->cons, "\nBreak(key) :\n");
io_cli(); /*强制结束处理时禁止任务切换*/
task->tss.eax = (int) &(task->tss.esp0);
task->tss.eip = (int) asm_end_app;
io_sti();
task_run(task, -1, 0); /*为了确实执行结束处理,如果处于休眠状态则唤醒*/
}
}
if (i == 256 + 0x3c && key_shift != 0) { /* Shift+F2 */
if (key_win != 0) {
keywin_off(key_win);
}
key_win = open_console(shtctl, memtotal);
sheet_slide(key_win, 32, 4);
sheet_updown(key_win, shtctl->top);
keywin_on(key_win);
}
if (i == 256 + 0x57) { /* F11 */
sheet_updown(shtctl->sheets[1], shtctl->top - 1);
}
if (i == 256 + 0xfa) { /*键盘成功接收到数据*/
keycmd_wait = -1;
}
if (i == 256 + 0xfe) { /*键盘没有成功接收到数据*/
wait_KBC_sendready();
io_out8(PORT_KEYDAT, keycmd_wait);
}
} else if (512 <= i && i <= 767) { /* 鼠标数据*/
if (mouse_decode(&mdec, i - 512) != 0) {
/* 已经收集了3字节的数据移动光标 */
mx += mdec.x;
my += mdec.y;
if (mx < 0) {
mx = 0;
}
if (my < 0) {
my = 0;
}
if (mx > binfo->scrnx - 1) {
mx = binfo->scrnx - 1;
}
if (my > binfo->scrny - 1) {
my = binfo->scrny - 1;
}
new_mx = mx;
new_my = my;
if ((mdec.btn & 0x01) != 0) { /* 按下左键 */
if (mmx < 0) {
/*如果处于通常模式*/
/*按照从上到下的顺序寻找鼠标所指向的图层*/
for (j = shtctl->top - 1; j > 0; j--) {
sht = shtctl->sheets[j];
x = mx - sht->vx0;
y = my - sht->vy0;
if (0 <= x && x < sht->bxsize && 0 <= y && y < sht->bysize) {
if (sht->buf[y * sht->bxsize + x] != sht->col_inv) {
sheet_updown(sht, shtctl->top - 1);
if (sht != key_win) {
keywin_off(key_win);
key_win = sht;
keywin_on(key_win);
}
if (3 <= x && x < sht->bxsize - 3 && 3 <= y && y < 21) {
mmx = mx; /*进入窗口移动模式*/
mmy = my;
mmx2 = sht->vx0;
new_wy = sht->vy0;
}
if (sht->bxsize - 21 <= x && x < sht->bxsize - 5 && 5 <= y && y < 19) {
/*点击“×”按钮*/
if ((sht->flags & 0x10) != 0) { /*该窗口是否为应用程序窗口?*/
task = sht->task;
cons_putstr0(task->cons, "\nBreak(mouse) :\n");
io_cli(); /*强制结束处理时禁止任务切换*/
task->tss.eax = (int) &(task->tss.esp0);
task->tss.eip = (int) asm_end_app;
io_sti();
task_run(task, -1, 0);
} else { /*命令行窗口*/
task = sht->task;
sheet_updown(sht, -1); /*暂且隐藏该图层*/
keywin_off(key_win);
key_win = shtctl->sheets[shtctl->top - 1];
keywin_on(key_win);
io_cli();
fifo32_put(&task->fifo, 4);
io_sti();
}
}
break;
}
}
}
} else {
/*如果处于窗口移动模式*/
x = mx - mmx; /*计算鼠标指针移动量*/
y = my - mmy;
new_wx = (mmx2 + x + 2) & ~3;
new_wy = new_wy + y;
mmy = my;
}
} else {
/*没有按下左键*/
mmx = -1; /*切换到一般模式*/
if (new_wx != 0x7fffffff) {
sheet_slide(sht, new_wx, new_wy); /*固定图层位置*/
new_wx = 0x7fffffff;
}
}
}
} else if (768 <= i && i <= 1023) { /*命令行窗口关闭处理*/
close_console(shtctl->sheets0 + (i - 768));
} else if (1024 <= i && i <= 2023) {
close_constask(taskctl->tasks0 + (i - 1024));
} else if (2024 <= i && i <= 2279) { /*只关闭命令行窗口*/
sht2 = shtctl->sheets0 + (i - 2024);
memman_free_4k(memman, (int) sht2->buf, 256 * 165);
sheet_free(sht2);
}
}
}
}
void keywin_off(struct SHEET *key_win)
{
change_wtitle8(key_win, 0);
if ((key_win->flags & 0x20) != 0) {
fifo32_put(&key_win->task->fifo, 3); /*命令行窗口光标OFF */
}
return;
}
void keywin_on(struct SHEET *key_win)
{
change_wtitle8(key_win, 1);
if ((key_win->flags & 0x20) != 0) {
fifo32_put(&key_win->task->fifo, 2); /*命令行窗口光标ON */
}
return;
}
struct TASK *open_constask(struct SHEET *sht, unsigned int memtotal)
{
struct MEMMAN *memman = (struct MEMMAN *) MEMMAN_ADDR;
struct TASK *task = task_alloc();
int *cons_fifo = (int *) memman_alloc_4k(memman, 128 * 4);
task->cons_stack = memman_alloc_4k(memman, 64 * 1024);
task->tss.esp = task->cons_stack + 64 * 1024 - 12;
task->tss.eip = (int) &console_task;
task->tss.es = 1 * 8;
task->tss.cs = 2 * 8;
task->tss.ss = 1 * 8;
task->tss.ds = 1 * 8;
task->tss.fs = 1 * 8;
task->tss.gs = 1 * 8;
*((int *) (task->tss.esp + 4)) = (int) sht;
*((int *) (task->tss.esp + 8)) = memtotal;
task_run(task, 2, 2); /* level=2, priority=2 */
fifo32_init(&task->fifo, 128, cons_fifo, task);
return task;
}
struct SHEET *open_console(struct SHTCTL *shtctl, unsigned int memtotal)
{
struct MEMMAN *memman = (struct MEMMAN *) MEMMAN_ADDR;
struct SHEET *sht = sheet_alloc(shtctl);
unsigned char *buf = (unsigned char *) memman_alloc_4k(memman, 256 * 165);
sheet_setbuf(sht, buf, 256, 165, -1); /*无透明色*/
make_window8(buf, 256, 165, "console", 0);
make_textbox8(sht, 8, 28, 240, 128, COL8_000000);
sht->task = open_constask(sht, memtotal);
sht->flags |= 0x20; /*有光标*/
return sht;
}
void close_constask(struct TASK *task)
{
struct MEMMAN *memman = (struct MEMMAN *) MEMMAN_ADDR;
task_sleep(task);
memman_free_4k(memman, task->cons_stack, 64 * 1024);
memman_free_4k(memman, (int) task->fifo.buf, 128 * 4);
task->flags = 0; /*用来替代task_free(task); */
return;
}
void close_console(struct SHEET *sht)
{
struct MEMMAN *memman = (struct MEMMAN *) MEMMAN_ADDR;
struct TASK *task = sht->task;
memman_free_4k(memman, (int) sht->buf, 256 * 165);
sheet_free(sht);
close_constask(task);
return;
}

297
30_day/haribote/bootpack.h Normal file
View File

@@ -0,0 +1,297 @@
/* asmhead.nas */
struct BOOTINFO { /* 0x0ff0-0x0fff */
char cyls; /* 启动区读磁盘读到此为止 */
char leds; /* 启动时键盘的LED的状态 */
char vmode; /* 显卡模式为多少位彩色 */
char reserve;
short scrnx, scrny; /* 画面分辨率 */
char *vram;
};
#define ADR_BOOTINFO 0x00000ff0
#define ADR_DISKIMG 0x00100000
/* naskfunc.nas */
void io_hlt(void);
void io_cli(void);
void io_sti(void);
void io_stihlt(void);
int io_in8(int port);
void io_out8(int port, int data);
int io_load_eflags(void);
void io_store_eflags(int eflags);
void load_gdtr(int limit, int addr);
void load_idtr(int limit, int addr);
int load_cr0(void);
void store_cr0(int cr0);
void load_tr(int tr);
void asm_inthandler0c(void);
void asm_inthandler0d(void);
void asm_inthandler20(void);
void asm_inthandler21(void);
void asm_inthandler2c(void);
unsigned int memtest_sub(unsigned int start, unsigned int end);
void farjmp(int eip, int cs);
void farcall(int eip, int cs);
void asm_hrb_api(void);
void start_app(int eip, int cs, int esp, int ds, int *tss_esp0);
void asm_end_app(void);
/* fifo.c */
struct FIFO32 {
int *buf;
int p, q, size, free, flags;
struct TASK *task;
};
void fifo32_init(struct FIFO32 *fifo, int size, int *buf, struct TASK *task);
int fifo32_put(struct FIFO32 *fifo, int data);
int fifo32_get(struct FIFO32 *fifo);
int fifo32_status(struct FIFO32 *fifo);
/* graphic.c */
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_screen8(char *vram, int x, int y);
void putfont8(char *vram, int xsize, int x, int y, char c, char *font);
void putfonts8_asc(char *vram, int xsize, int x, int y, char c, unsigned char *s);
void init_mouse_cursor8(char *mouse, char bc);
void putblock8_8(char *vram, int vxsize, int pxsize,
int pysize, int px0, int py0, char *buf, int bxsize);
#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
/* dsctbl.c */
struct SEGMENT_DESCRIPTOR {
short limit_low, base_low;
char base_mid, access_right;
char limit_high, base_high;
};
struct GATE_DESCRIPTOR {
short offset_low, selector;
char dw_count, access_right;
short offset_high;
};
void init_gdtidt(void);
void set_segmdesc(struct SEGMENT_DESCRIPTOR *sd, unsigned int limit, int base, int ar);
void set_gatedesc(struct GATE_DESCRIPTOR *gd, int offset, int selector, int ar);
#define ADR_IDT 0x0026f800
#define LIMIT_IDT 0x000007ff
#define ADR_GDT 0x00270000
#define LIMIT_GDT 0x0000ffff
#define ADR_BOTPAK 0x00280000
#define LIMIT_BOTPAK 0x0007ffff
#define AR_DATA32_RW 0x4092
#define AR_CODE32_ER 0x409a
#define AR_LDT 0x0082
#define AR_TSS32 0x0089
#define AR_INTGATE32 0x008e
/* int.c */
void init_pic(void);
#define PIC0_ICW1 0x0020
#define PIC0_OCW2 0x0020
#define PIC0_IMR 0x0021
#define PIC0_ICW2 0x0021
#define PIC0_ICW3 0x0021
#define PIC0_ICW4 0x0021
#define PIC1_ICW1 0x00a0
#define PIC1_OCW2 0x00a0
#define PIC1_IMR 0x00a1
#define PIC1_ICW2 0x00a1
#define PIC1_ICW3 0x00a1
#define PIC1_ICW4 0x00a1
/* keyboard.c */
void inthandler21(int *esp);
void wait_KBC_sendready(void);
void init_keyboard(struct FIFO32 *fifo, int data0);
#define PORT_KEYDAT 0x0060
#define PORT_KEYCMD 0x0064
/* mouse.c */
struct MOUSE_DEC {
unsigned char buf[3], phase;
int x, y, btn;
};
void inthandler2c(int *esp);
void enable_mouse(struct FIFO32 *fifo, int data0, struct MOUSE_DEC *mdec);
int mouse_decode(struct MOUSE_DEC *mdec, unsigned char dat);
/* memory.c */
#define MEMMAN_FREES 4090 /* <20><><EFBFBD><EFBFBD>Ŗ<EFBFBD>32KB */
#define MEMMAN_ADDR 0x003c0000
struct FREEINFO { /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
unsigned int addr, size;
};
struct MEMMAN { /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǘ<EFBFBD> */
int frees, maxfrees, lostsize, losts;
struct FREEINFO free[MEMMAN_FREES];
};
unsigned int memtest(unsigned int start, unsigned int end);
void memman_init(struct MEMMAN *man);
unsigned int memman_total(struct MEMMAN *man);
unsigned int memman_alloc(struct MEMMAN *man, unsigned int size);
int memman_free(struct MEMMAN *man, unsigned int addr, unsigned int size);
unsigned int memman_alloc_4k(struct MEMMAN *man, unsigned int size);
int memman_free_4k(struct MEMMAN *man, unsigned int addr, unsigned int size);
/* sheet.c */
#define MAX_SHEETS 256
struct SHEET {
unsigned char *buf;
int bxsize, bysize, vx0, vy0, col_inv, height, flags;
struct SHTCTL *ctl;
struct TASK *task;
};
struct SHTCTL {
unsigned char *vram, *map;
int xsize, ysize, top;
struct SHEET *sheets[MAX_SHEETS];
struct SHEET sheets0[MAX_SHEETS];
};
struct SHTCTL *shtctl_init(struct MEMMAN *memman, unsigned char *vram, int xsize, int ysize);
struct SHEET *sheet_alloc(struct SHTCTL *ctl);
void sheet_setbuf(struct SHEET *sht, unsigned char *buf, int xsize, int ysize, int col_inv);
void sheet_updown(struct SHEET *sht, int height);
void sheet_refresh(struct SHEET *sht, int bx0, int by0, int bx1, int by1);
void sheet_slide(struct SHEET *sht, int vx0, int vy0);
void sheet_free(struct SHEET *sht);
/* timer.c */
#define MAX_TIMER 500
struct TIMER {
struct TIMER *next;
unsigned int timeout;
char flags, flags2;
struct FIFO32 *fifo;
int data;
};
struct TIMERCTL {
unsigned int count, next;
struct TIMER *t0;
struct TIMER timers0[MAX_TIMER];
};
extern struct TIMERCTL timerctl;
void init_pit(void);
struct TIMER *timer_alloc(void);
void timer_free(struct TIMER *timer);
void timer_init(struct TIMER *timer, struct FIFO32 *fifo, int data);
void timer_settime(struct TIMER *timer, unsigned int timeout);
void inthandler20(int *esp);
int timer_cancel(struct TIMER *timer);
void timer_cancelall(struct FIFO32 *fifo);
/* mtask.c */
#define MAX_TASKS 1000 /*最大任务数量*/
#define TASK_GDT0 3 /*定义从GDT的几号开始分配给TSS */
#define MAX_TASKS_LV 100
#define MAX_TASKLEVELS 10
struct TSS32 {
int backlink, esp0, ss0, esp1, ss1, esp2, ss2, cr3;
int eip, eflags, eax, ecx, edx, ebx, esp, ebp, esi, edi;
int es, cs, ss, ds, fs, gs;
int ldtr, iomap;
};
struct TASK {
int sel, flags; /* sel用来存放GDT的编号*/
int level, priority; /* 优先级 */
struct FIFO32 fifo;
struct TSS32 tss;
struct SEGMENT_DESCRIPTOR ldt[2];
struct CONSOLE *cons;
int ds_base, cons_stack;
struct FILEHANDLE *fhandle;
int *fat;
char *cmdline;
unsigned char langmode, langbyte1;
};
struct TASKLEVEL {
int running; /*正在运行的任务数量*/
int now; /*这个变量用来记录当前正在运行的是哪个任务*/
struct TASK *tasks[MAX_TASKS_LV];
};
struct TASKCTL {
int now_lv; /*现在活动中的LEVEL */
char lv_change; /*在下次任务切换时是否需要改变LEVEL */
struct TASKLEVEL level[MAX_TASKLEVELS];
struct TASK tasks0[MAX_TASKS];
};
extern struct TASKCTL *taskctl;
extern struct TIMER *task_timer;
struct TASK *task_now(void);
struct TASK *task_init(struct MEMMAN *memman);
struct TASK *task_alloc(void);
void task_run(struct TASK *task, int level, int priority);
void task_switch(void);
void task_sleep(struct TASK *task);
/* window.c */
void make_window8(unsigned char *buf, int xsize, int ysize, char *title, char act);
void putfonts8_asc_sht(struct SHEET *sht, int x, int y, int c, int b, char *s, int l);
void make_textbox8(struct SHEET *sht, int x0, int y0, int sx, int sy, int c);
void make_wtitle8(unsigned char *buf, int xsize, char *title, char act);
void change_wtitle8(struct SHEET *sht, char act);
/* console.c */
struct CONSOLE {
struct SHEET *sht;
int cur_x, cur_y, cur_c;
struct TIMER *timer;
};
struct FILEHANDLE {
char *buf;
int size;
int pos;
};
void console_task(struct SHEET *sheet, int memtotal);
void cons_putchar(struct CONSOLE *cons, int chr, char move);
void cons_newline(struct CONSOLE *cons);
void cons_putstr0(struct CONSOLE *cons, char *s);
void cons_putstr1(struct CONSOLE *cons, char *s, int l);
void cons_runcmd(char *cmdline, struct CONSOLE *cons, int *fat, int memtotal);
void cmd_mem(struct CONSOLE *cons, int memtotal);
void cmd_cls(struct CONSOLE *cons);
void cmd_dir(struct CONSOLE *cons);
void cmd_exit(struct CONSOLE *cons, int *fat);
void cmd_start(struct CONSOLE *cons, char *cmdline, int memtotal);
void cmd_ncst(struct CONSOLE *cons, char *cmdline, int memtotal);
void cmd_langmode(struct CONSOLE *cons, char *cmdline);
int cmd_app(struct CONSOLE *cons, int *fat, char *cmdline);
int *hrb_api(int edi, int esi, int ebp, int esp, int ebx, int edx, int ecx, int eax);
int *inthandler0d(int *esp);
int *inthandler0c(int *esp);
void hrb_api_linewin(struct SHEET *sht, int x0, int y0, int x1, int y1, int col);
/* file.c */
struct FILEINFO {
unsigned char name[8], ext[3], type;
char reserve[10];
unsigned short time, date, clustno;
unsigned int size;
};
void file_readfat(int *fat, unsigned char *img);
void file_loadfile(int clustno, int size, char *buf, int *fat, char *img);
struct FILEINFO *file_search(char *name, struct FILEINFO *finfo, int max);
char *file_loadfile2(int clustno, int *psize, int *fat);
/* tek.c */
int tek_getsize(unsigned char *p);
int tek_decomp(unsigned char *p, char *q, int size);
/* bootpack.c */
struct TASK *open_constask(struct SHEET *sht, unsigned int memtotal);
struct SHEET *open_console(struct SHTCTL *shtctl, unsigned int memtotal);

706
30_day/haribote/console.c Normal file
View File

@@ -0,0 +1,706 @@
/* 命令行窗口相关 */
#include "bootpack.h"
#include <stdio.h>
#include <string.h>
void console_task(struct SHEET *sheet, int memtotal)
{
struct TASK *task = task_now();
struct MEMMAN *memman = (struct MEMMAN *) MEMMAN_ADDR;
int i, *fat = (int *) memman_alloc_4k(memman, 4 * 2880);
struct CONSOLE cons;
struct FILEHANDLE fhandle[8];
char cmdline[30];
unsigned char *nihongo = (char *) *((int *) 0x0fe8);
cons.sht = sheet;
cons.cur_x = 8;
cons.cur_y = 28;
cons.cur_c = -1;
task->cons = &cons;
task->cmdline = cmdline;
if (cons.sht != 0) {
cons.timer = timer_alloc();
timer_init(cons.timer, &task->fifo, 1);
timer_settime(cons.timer, 50);
}
file_readfat(fat, (unsigned char *) (ADR_DISKIMG + 0x000200));
for (i = 0; i < 8; i++) {
fhandle[i].buf = 0; /*未使用标记*/
}
task->fhandle = fhandle;
task->fat = fat;
if (nihongo[4096] != 0xff) { /* 是否载入了字库?*/
task->langmode = 1;
} else {
task->langmode = 0;
}
task->langbyte1 = 0;
/*显示提示符*/
cons_putchar(&cons, '>', 1);
for (;;) {
io_cli();
if (fifo32_status(&task->fifo) == 0) {
task_sleep(task);
io_sti();
} else {
i = fifo32_get(&task->fifo);
io_sti();
if (i <= 1 && cons.sht != 0) { /*光标用定时器*/
if (i != 0) {
timer_init(cons.timer, &task->fifo, 0); /*下次置0 */
if (cons.cur_c >= 0) {
cons.cur_c = COL8_FFFFFF;
}
} else {
timer_init(cons.timer, &task->fifo, 1); /*下次置1 */
if (cons.cur_c >= 0) {
cons.cur_c = COL8_000000;
}
}
timer_settime(cons.timer, 50);
}
if (i == 2) { /*光标ON */
cons.cur_c = COL8_FFFFFF;
}
if (i == 3) { /*光标OFF */
if (cons.sht != 0) {
boxfill8(cons.sht->buf, cons.sht->bxsize, COL8_000000,
cons.cur_x, cons.cur_y, cons.cur_x + 7, cons.cur_y + 15);
}
cons.cur_c = -1;
}
if (i == 4) { /*点击命令行窗口的“×”按钮*/
cmd_exit(&cons, fat);
}
if (256 <= i && i <= 511) { /*键盘数据通过任务A*/
if (i == 8 + 256) {
/*退格键*/
if (cons.cur_x > 16) {
/*用空格擦除光标后将光标前移一位*/
cons_putchar(&cons, ' ', 0);
cons.cur_x -= 8;
}
} else if (i == 10 + 256) {
/*回车键*/
/*将光标用空格擦除后换行 */
cons_putchar(&cons, ' ', 0);
cmdline[cons.cur_x / 8 - 2] = 0;
cons_newline(&cons);
cons_runcmd(cmdline, &cons, fat, memtotal); /*运行命令*/
if (cons.sht == 0) {
cmd_exit(&cons, fat);
}
/*显示提示符*/
cons_putchar(&cons, '>', 1);
} else {
/*一般字符*/
if (cons.cur_x < 240) {
/*显示一个字符之后将光标后移一位*/
cmdline[cons.cur_x / 8 - 2] = i - 256;
cons_putchar(&cons, i - 256, 1);
}
}
}
/*重新显示光标*/
if (cons.sht != 0) {
if (cons.cur_c >= 0) {
boxfill8(cons.sht->buf, cons.sht->bxsize, cons.cur_c,
cons.cur_x, cons.cur_y, cons.cur_x + 7, cons.cur_y + 15);
}
sheet_refresh(cons.sht, cons.cur_x, cons.cur_y, cons.cur_x + 8, cons.cur_y + 16);
}
}
}
}
void cons_putchar(struct CONSOLE *cons, int chr, char move)
{
char s[2];
s[0] = chr;
s[1] = 0;
if (s[0] == 0x09) { /*制表符*/
for (;;) {
if (cons->sht != 0) {
putfonts8_asc_sht(cons->sht, cons->cur_x, cons->cur_y, COL8_FFFFFF, COL8_000000, " ", 1);
}
cons->cur_x += 8;
if (cons->cur_x == 8 + 240) {
cons_newline(cons);
}
if (((cons->cur_x - 8) & 0x1f) == 0) {
break; /* 32<33>Ŋ<EFBFBD><C58A><EFBFBD>؂ꂽ<D882><EA82BD>break */
}
}
} else if (s[0] == 0x0a) { /* <20><><EFBFBD>s */
cons_newline(cons);
} else if (s[0] == 0x0d) { /* <20><><EFBFBD>A */
/* <20>Ƃ肠<C682><E882A0><EFBFBD><EFBFBD><EFBFBD>Ȃɂ<C882><C982><EFBFBD><EFBFBD>Ȃ<EFBFBD> */
} else { /* <20><><EFBFBD>ʂ̕<CA82><CC95><EFBFBD> */
if (cons->sht != 0) {
putfonts8_asc_sht(cons->sht, cons->cur_x, cons->cur_y, COL8_FFFFFF, COL8_000000, s, 1);
}
if (move != 0) {
/* move<76><65>0<EFBFBD>̂Ƃ<CC82><C682>̓J<CD83>[<5B>\<5C><><EFBFBD><EFBFBD>i<EFBFBD>߂Ȃ<DF82> */
cons->cur_x += 8;
if (cons->cur_x == 8 + 240) {
cons_newline(cons);
}
}
}
return;
}
void cons_newline(struct CONSOLE *cons)
{
int x, y;
struct SHEET *sheet = cons->sht;
struct TASK *task = task_now();
if (cons->cur_y < 28 + 112) {
cons->cur_y += 16; /* <20><><EFBFBD>̍s<CC8D><73> */
} else {
/* <20>X<EFBFBD>N<EFBFBD><4E><EFBFBD>[<5B><> */
if (sheet != 0) {
for (y = 28; y < 28 + 112; y++) {
for (x = 8; x < 8 + 240; x++) {
sheet->buf[x + y * sheet->bxsize] = sheet->buf[x + (y + 16) * sheet->bxsize];
}
}
for (y = 28 + 112; y < 28 + 128; y++) {
for (x = 8; x < 8 + 240; x++) {
sheet->buf[x + y * sheet->bxsize] = COL8_000000;
}
}
sheet_refresh(sheet, 8, 28, 8 + 240, 28 + 128);
}
}
cons->cur_x = 8;
if (task->langmode == 1 && task->langbyte1 != 0) {
cons->cur_x = 16;
}
return;
}
void cons_putstr0(struct CONSOLE *cons, char *s)
{
for (; *s != 0; s++) {
cons_putchar(cons, *s, 1);
}
return;
}
void cons_putstr1(struct CONSOLE *cons, char *s, int l)
{
int i;
for (i = 0; i < l; i++) {
cons_putchar(cons, s[i], 1);
}
return;
}
void cons_runcmd(char *cmdline, struct CONSOLE *cons, int *fat, int memtotal)
{
if (strcmp(cmdline, "mem") == 0 && cons->sht != 0) {
cmd_mem(cons, memtotal);
} else if (strcmp(cmdline, "cls") == 0 && cons->sht != 0) {
cmd_cls(cons);
} else if (strcmp(cmdline, "dir") == 0 && cons->sht != 0) {
cmd_dir(cons);
} else if (strcmp(cmdline, "exit") == 0) {
cmd_exit(cons, fat);
} else if (strncmp(cmdline, "start ", 6) == 0) {
cmd_start(cons, cmdline, memtotal);
} else if (strncmp(cmdline, "ncst ", 5) == 0) {
cmd_ncst(cons, cmdline, memtotal);
} else if (strncmp(cmdline, "langmode ", 9) == 0) {
cmd_langmode(cons, cmdline);
} else if (cmdline[0] != 0) {
if (cmd_app(cons, fat, cmdline) == 0) {
/* <20>R<EFBFBD>}<7D><><EFBFBD>h<EFBFBD>ł͂Ȃ<CD82><C882>A<EFBFBD>A<EFBFBD>v<EFBFBD><76><EFBFBD>ł<EFBFBD><C582>Ȃ<EFBFBD><C882>A<EFBFBD><41><EFBFBD><EFBFBD>ɋ<EFBFBD>s<EFBFBD>ł<EFBFBD><C582>Ȃ<EFBFBD> */
cons_putstr0(cons, "Bad command.\n\n");
}
}
return;
}
void cmd_mem(struct CONSOLE *cons, int memtotal)
{
struct MEMMAN *memman = (struct MEMMAN *) MEMMAN_ADDR;
char s[60];
sprintf(s, "total %dMB\nfree %dKB\n\n", memtotal / (1024 * 1024), memman_total(memman) / 1024);
cons_putstr0(cons, s);
return;
}
void cmd_cls(struct CONSOLE *cons)
{
int x, y;
struct SHEET *sheet = cons->sht;
for (y = 28; y < 28 + 128; y++) {
for (x = 8; x < 8 + 240; x++) {
sheet->buf[x + y * sheet->bxsize] = COL8_000000;
}
}
sheet_refresh(sheet, 8, 28, 8 + 240, 28 + 128);
cons->cur_y = 28;
return;
}
void cmd_dir(struct CONSOLE *cons)
{
struct FILEINFO *finfo = (struct FILEINFO *) (ADR_DISKIMG + 0x002600);
int i, j;
char s[30];
for (i = 0; i < 224; i++) {
if (finfo[i].name[0] == 0x00) {
break;
}
if (finfo[i].name[0] != 0xe5) {
if ((finfo[i].type & 0x18) == 0) {
sprintf(s, "filename.ext %7d\n", finfo[i].size);
for (j = 0; j < 8; j++) {
s[j] = finfo[i].name[j];
}
s[ 9] = finfo[i].ext[0];
s[10] = finfo[i].ext[1];
s[11] = finfo[i].ext[2];
cons_putstr0(cons, s);
}
}
}
cons_newline(cons);
return;
}
void cmd_exit(struct CONSOLE *cons, int *fat)
{
struct MEMMAN *memman = (struct MEMMAN *) MEMMAN_ADDR;
struct TASK *task = task_now();
struct SHTCTL *shtctl = (struct SHTCTL *) *((int *) 0x0fe4);
struct FIFO32 *fifo = (struct FIFO32 *) *((int *) 0x0fec);
if (cons->sht != 0) {
timer_cancel(cons->timer);
}
memman_free_4k(memman, (int) fat, 4 * 2880);
io_cli();
if (cons->sht != 0) {
fifo32_put(fifo, cons->sht - shtctl->sheets0 + 768); /* 768<36>`1023 */
} else {
fifo32_put(fifo, task - taskctl->tasks0 + 1024); /* 1024<32>`2023 */
}
io_sti();
for (;;) {
task_sleep(task);
}
}
void cmd_start(struct CONSOLE *cons, char *cmdline, int memtotal)
{
struct SHTCTL *shtctl = (struct SHTCTL *) *((int *) 0x0fe4);
struct SHEET *sht = open_console(shtctl, memtotal);
struct FIFO32 *fifo = &sht->task->fifo;
int i;
sheet_slide(sht, 32, 4);
sheet_updown(sht, shtctl->top);
/* <20>R<EFBFBD>}<7D><><EFBFBD>h<EFBFBD><68><EFBFBD>C<EFBFBD><43><EFBFBD>ɓ<EFBFBD><C993>͂<EFBFBD><CD82><EFBFBD><EA82BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>A<EFBFBD><EFBFBD><EA95B6><EFBFBD><EFBFBD><EFBFBD>V<C290><56><EFBFBD><EFBFBD><EFBFBD>R<EFBFBD><52><EFBFBD>\<5C>[<5B><><EFBFBD>ɓ<EFBFBD><C993><EFBFBD> */
for (i = 6; cmdline[i] != 0; i++) {
fifo32_put(fifo, cmdline[i] + 256);
}
fifo32_put(fifo, 10 + 256); /* Enter */
cons_newline(cons);
return;
}
void cmd_ncst(struct CONSOLE *cons, char *cmdline, int memtotal)
{
struct TASK *task = open_constask(0, memtotal);
struct FIFO32 *fifo = &task->fifo;
int i;
/* <20>R<EFBFBD>}<7D><><EFBFBD>h<EFBFBD><68><EFBFBD>C<EFBFBD><43><EFBFBD>ɓ<EFBFBD><C993>͂<EFBFBD><CD82><EFBFBD><EA82BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>A<EFBFBD><EFBFBD><EA95B6><EFBFBD><EFBFBD><EFBFBD>V<C290><56><EFBFBD><EFBFBD><EFBFBD>R<EFBFBD><52><EFBFBD>\<5C>[<5B><><EFBFBD>ɓ<EFBFBD><C993><EFBFBD> */
for (i = 5; cmdline[i] != 0; i++) {
fifo32_put(fifo, cmdline[i] + 256);
}
fifo32_put(fifo, 10 + 256); /* Enter */
cons_newline(cons);
return;
}
void cmd_langmode(struct CONSOLE *cons, char *cmdline)
{
struct TASK *task = task_now();
unsigned char mode = cmdline[9] - '0';
if (mode <= 2) {
task->langmode = mode;
} else {
cons_putstr0(cons, "mode number error.\n");
}
cons_newline(cons);
return;
}
int cmd_app(struct CONSOLE *cons, int *fat, char *cmdline)
{
struct MEMMAN *memman = (struct MEMMAN *) MEMMAN_ADDR;
struct FILEINFO *finfo;
char name[18], *p, *q;
struct TASK *task = task_now();
int i, segsiz, datsiz, esp, dathrb, appsiz;
struct SHTCTL *shtctl;
struct SHEET *sht;
/* <20>R<EFBFBD>}<7D><><EFBFBD>h<EFBFBD><68><EFBFBD>C<EFBFBD><43><EFBFBD><EFBFBD><EFBFBD><EFBFBD>t<EFBFBD>@<40>C<EFBFBD><43><EFBFBD><EFBFBD><EFBFBD>𐶐<EFBFBD> */
for (i = 0; i < 13; i++) {
if (cmdline[i] <= ' ') {
break;
}
name[i] = cmdline[i];
}
name[i] = 0; /* <20>Ƃ肠<C682><E882A0><EFBFBD><EFBFBD><EFBFBD>t<EFBFBD>@<40>C<EFBFBD><43><EFBFBD><EFBFBD><EFBFBD>̌<EFBFBD><CC8C><EFBFBD>0<EFBFBD>ɂ<EFBFBD><C982><EFBFBD> */
/* <20>t<EFBFBD>@<40>C<EFBFBD><43><EFBFBD><EFBFBD>T<EFBFBD><54> */
finfo = file_search(name, (struct FILEINFO *) (ADR_DISKIMG + 0x002600), 224);
if (finfo == 0 && name[i - 1] != '.') {
/* <20><><EFBFBD>‚<EFBFBD><C282><EFBFBD>Ȃ<EFBFBD><C882><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̂Ō<CC82><C58C><EFBFBD>".HRB"<22><><EFBFBD>‚<EFBFBD><C282>Ă<EFBFBD><C482><EFBFBD><EFBFBD><EFBFBD>x<EFBFBD>T<EFBFBD><54><EFBFBD>Ă݂<C482> */
name[i ] = '.';
name[i + 1] = 'H';
name[i + 2] = 'R';
name[i + 3] = 'B';
name[i + 4] = 0;
finfo = file_search(name, (struct FILEINFO *) (ADR_DISKIMG + 0x002600), 224);
}
if (finfo != 0) {
/* <20>t<EFBFBD>@<40>C<EFBFBD><43><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>‚<EFBFBD><C282><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ꍇ */
appsiz = finfo->size;
p = file_loadfile2(finfo->clustno, &appsiz, fat);
if (appsiz >= 36 && strncmp(p + 4, "Hari", 4) == 0 && *p == 0x00) {
segsiz = *((int *) (p + 0x0000));
esp = *((int *) (p + 0x000c));
datsiz = *((int *) (p + 0x0010));
dathrb = *((int *) (p + 0x0014));
q = (char *) memman_alloc_4k(memman, segsiz);
task->ds_base = (int) q;
set_segmdesc(task->ldt + 0, appsiz - 1, (int) p, AR_CODE32_ER + 0x60);
set_segmdesc(task->ldt + 1, segsiz - 1, (int) q, AR_DATA32_RW + 0x60);
for (i = 0; i < datsiz; i++) {
q[esp + i] = p[dathrb + i];
}
start_app(0x1b, 0 * 8 + 4, esp, 1 * 8 + 4, &(task->tss.esp0));
shtctl = (struct SHTCTL *) *((int *) 0x0fe4);
for (i = 0; i < MAX_SHEETS; i++) {
sht = &(shtctl->sheets0[i]);
if ((sht->flags & 0x11) == 0x11 && sht->task == task) {
/* <20>A<EFBFBD>v<EFBFBD><76><EFBFBD><EFBFBD><EFBFBD>J<EFBFBD><4A><EFBFBD><EFBFBD><EFBFBD>ςȂ<CF82><C882>ɂ<EFBFBD><C982><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>𔭌<EFBFBD> */
sheet_free(sht); /* <20>‚<EFBFBD><C282><EFBFBD> */
}
}
for (i = 0; i < 8; i++) { /* <20>N<EFBFBD><4E><EFBFBD>[<5B>Y<EFBFBD><59><EFBFBD>ĂȂ<C482><C882>t<EFBFBD>@<40>C<EFBFBD><43><EFBFBD><EFBFBD><EFBFBD>N<EFBFBD><4E><EFBFBD>[<5B>Y */
if (task->fhandle[i].buf != 0) {
memman_free_4k(memman, (int) task->fhandle[i].buf, task->fhandle[i].size);
task->fhandle[i].buf = 0;
}
}
timer_cancelall(&task->fifo);
memman_free_4k(memman, (int) q, segsiz);
task->langbyte1 = 0;
} else {
cons_putstr0(cons, ".hrb file format error.\n");
}
memman_free_4k(memman, (int) p, appsiz);
cons_newline(cons);
return 1;
}
/* <20>t<EFBFBD>@<40>C<EFBFBD><43><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>‚<EFBFBD><C282><EFBFBD>Ȃ<EFBFBD><C882><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ꍇ */
return 0;
}
int *hrb_api(int edi, int esi, int ebp, int esp, int ebx, int edx, int ecx, int eax)
{
struct TASK *task = task_now();
int ds_base = task->ds_base;
struct CONSOLE *cons = task->cons;
struct SHTCTL *shtctl = (struct SHTCTL *) *((int *) 0x0fe4);
struct SHEET *sht;
struct FIFO32 *sys_fifo = (struct FIFO32 *) *((int *) 0x0fec);
int *reg = &eax + 1; /* eax<61>̎<EFBFBD><CC8E>̔Ԓn */
/* <20>ۑ<EFBFBD><DB91>̂<EFBFBD><CC82>߂<EFBFBD>PUSHAD<41><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɏ<EFBFBD><C98F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
/* reg[0] : EDI, reg[1] : ESI, reg[2] : EBP, reg[3] : ESP */
/* reg[4] : EBX, reg[5] : EDX, reg[6] : ECX, reg[7] : EAX */
int i;
struct FILEINFO *finfo;
struct FILEHANDLE *fh;
struct MEMMAN *memman = (struct MEMMAN *) MEMMAN_ADDR;
if (edx == 1) {
cons_putchar(cons, eax & 0xff, 1);
} else if (edx == 2) {
cons_putstr0(cons, (char *) ebx + ds_base);
} else if (edx == 3) {
cons_putstr1(cons, (char *) ebx + ds_base, ecx);
} else if (edx == 4) {
return &(task->tss.esp0);
} else if (edx == 5) {
sht = sheet_alloc(shtctl);
sht->task = task;
sht->flags |= 0x10;
sheet_setbuf(sht, (char *) ebx + ds_base, esi, edi, eax);
make_window8((char *) ebx + ds_base, esi, edi, (char *) ecx + ds_base, 0);
sheet_slide(sht, ((shtctl->xsize - esi) / 2) & ~3, (shtctl->ysize - edi) / 2);
sheet_updown(sht, shtctl->top); /* <20><><EFBFBD>̃}<7D>E<EFBFBD>X<EFBFBD>Ɠ<EFBFBD><C693><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɂȂ<C982><EFBFBD>Ɏw<C98E><77>F <20>}<7D>E<EFBFBD>X<EFBFBD>͂<EFBFBD><CD82>̏<EFBFBD>ɂȂ<C982> */
reg[7] = (int) sht;
} else if (edx == 6) {
sht = (struct SHEET *) (ebx & 0xfffffffe);
putfonts8_asc(sht->buf, sht->bxsize, esi, edi, eax, (char *) ebp + ds_base);
if ((ebx & 1) == 0) {
sheet_refresh(sht, esi, edi, esi + ecx * 8, edi + 16);
}
} else if (edx == 7) {
sht = (struct SHEET *) (ebx & 0xfffffffe);
boxfill8(sht->buf, sht->bxsize, ebp, eax, ecx, esi, edi);
if ((ebx & 1) == 0) {
sheet_refresh(sht, eax, ecx, esi + 1, edi + 1);
}
} else if (edx == 8) {
memman_init((struct MEMMAN *) (ebx + ds_base));
ecx &= 0xfffffff0; /* 16<31>o<EFBFBD>C<EFBFBD>g<EFBFBD>P<EFBFBD>ʂ<EFBFBD> */
memman_free((struct MEMMAN *) (ebx + ds_base), eax, ecx);
} else if (edx == 9) {
ecx = (ecx + 0x0f) & 0xfffffff0; /* 16<31>o<EFBFBD>C<EFBFBD>g<EFBFBD>P<EFBFBD>ʂɐ؂<C990>グ */
reg[7] = memman_alloc((struct MEMMAN *) (ebx + ds_base), ecx);
} else if (edx == 10) {
ecx = (ecx + 0x0f) & 0xfffffff0; /* 16<31>o<EFBFBD>C<EFBFBD>g<EFBFBD>P<EFBFBD>ʂɐ؂<C990>グ */
memman_free((struct MEMMAN *) (ebx + ds_base), eax, ecx);
} else if (edx == 11) {
sht = (struct SHEET *) (ebx & 0xfffffffe);
sht->buf[sht->bxsize * edi + esi] = eax;
if ((ebx & 1) == 0) {
sheet_refresh(sht, esi, edi, esi + 1, edi + 1);
}
} else if (edx == 12) {
sht = (struct SHEET *) ebx;
sheet_refresh(sht, eax, ecx, esi, edi);
} else if (edx == 13) {
sht = (struct SHEET *) (ebx & 0xfffffffe);
hrb_api_linewin(sht, eax, ecx, esi, edi, ebp);
if ((ebx & 1) == 0) {
if (eax > esi) {
i = eax;
eax = esi;
esi = i;
}
if (ecx > edi) {
i = ecx;
ecx = edi;
edi = i;
}
sheet_refresh(sht, eax, ecx, esi + 1, edi + 1);
}
} else if (edx == 14) {
sheet_free((struct SHEET *) ebx);
} else if (edx == 15) {
for (;;) {
io_cli();
if (fifo32_status(&task->fifo) == 0) {
if (eax != 0) {
task_sleep(task); /* FIFO<46><4F><EFBFBD><EFBFBD>Ȃ̂ŐQ<C590>đ҂<C491> */
} else {
io_sti();
reg[7] = -1;
return 0;
}
}
i = fifo32_get(&task->fifo);
io_sti();
if (i <= 1 && cons->sht != 0) { /* <20>J<EFBFBD>[<5B>\<5C><><EFBFBD>p<EFBFBD>^<5E>C<EFBFBD>} */
/* <20>A<EFBFBD>v<EFBFBD><76><EFBFBD><EFBFBD><EFBFBD>s<EFBFBD><73><EFBFBD>̓J<CD83>[<5B>\<5C><><EFBFBD><EFBFBD><EFBFBD>o<EFBFBD>Ȃ<EFBFBD><C882>̂ŁA<C581><41><EFBFBD>‚<EFBFBD><C282><EFBFBD><EFBFBD>͕\<5C><><EFBFBD>p<EFBFBD><70>1<EFBFBD>𒍕<EFBFBD><F0928D95><EFBFBD><EFBFBD>Ă<EFBFBD><C482><EFBFBD> */
timer_init(cons->timer, &task->fifo, 1); /* <20><><EFBFBD><EFBFBD>1<EFBFBD><31> */
timer_settime(cons->timer, 50);
}
if (i == 2) { /* <20>J<EFBFBD>[<5B>\<5C><>ON */
cons->cur_c = COL8_FFFFFF;
}
if (i == 3) { /* <20>J<EFBFBD>[<5B>\<5C><>OFF */
cons->cur_c = -1;
}
if (i == 4) { /* <20>R<EFBFBD><52><EFBFBD>\<5C>[<5B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>‚<EFBFBD><C282><EFBFBD> */
timer_cancel(cons->timer);
io_cli();
fifo32_put(sys_fifo, cons->sht - shtctl->sheets0 + 2024); /* 2024<32>`2279 */
cons->sht = 0;
io_sti();
}
if (i >= 256) { /* <20>L<EFBFBD>[<5B>{<7B>[<5B>h<EFBFBD>f<EFBFBD>[<5B>^<5E>i<EFBFBD>^<5E>X<EFBFBD>NA<4E>o<EFBFBD>R<EFBFBD>j<EFBFBD>Ȃ<EFBFBD> */
reg[7] = i - 256;
return 0;
}
}
} else if (edx == 16) {
reg[7] = (int) timer_alloc();
((struct TIMER *) reg[7])->flags2 = 1; /* <20><><EFBFBD><EFBFBD><EFBFBD>L<EFBFBD><4C><EFBFBD><EFBFBD><EFBFBD>Z<EFBFBD><5A><EFBFBD>L<EFBFBD><4C> */
} else if (edx == 17) {
timer_init((struct TIMER *) ebx, &task->fifo, eax + 256);
} else if (edx == 18) {
timer_settime((struct TIMER *) ebx, eax);
} else if (edx == 19) {
timer_free((struct TIMER *) ebx);
} else if (edx == 20) {
if (eax == 0) {
i = io_in8(0x61);
io_out8(0x61, i & 0x0d);
} else {
i = 1193180000 / eax;
io_out8(0x43, 0xb6);
io_out8(0x42, i & 0xff);
io_out8(0x42, i >> 8);
i = io_in8(0x61);
io_out8(0x61, (i | 0x03) & 0x0f);
}
} else if (edx == 21) {
for (i = 0; i < 8; i++) {
if (task->fhandle[i].buf == 0) {
break;
}
}
fh = &task->fhandle[i];
reg[7] = 0;
if (i < 8) {
finfo = file_search((char *) ebx + ds_base,
(struct FILEINFO *) (ADR_DISKIMG + 0x002600), 224);
if (finfo != 0) {
reg[7] = (int) fh;
fh->size = finfo->size;
fh->pos = 0;
fh->buf = file_loadfile2(finfo->clustno, &fh->size, task->fat);
}
}
} else if (edx == 22) {
fh = (struct FILEHANDLE *) eax;
memman_free_4k(memman, (int) fh->buf, fh->size);
fh->buf = 0;
} else if (edx == 23) {
fh = (struct FILEHANDLE *) eax;
if (ecx == 0) {
fh->pos = ebx;
} else if (ecx == 1) {
fh->pos += ebx;
} else if (ecx == 2) {
fh->pos = fh->size + ebx;
}
if (fh->pos < 0) {
fh->pos = 0;
}
if (fh->pos > fh->size) {
fh->pos = fh->size;
}
} else if (edx == 24) {
fh = (struct FILEHANDLE *) eax;
if (ecx == 0) {
reg[7] = fh->size;
} else if (ecx == 1) {
reg[7] = fh->pos;
} else if (ecx == 2) {
reg[7] = fh->pos - fh->size;
}
} else if (edx == 25) {
fh = (struct FILEHANDLE *) eax;
for (i = 0; i < ecx; i++) {
if (fh->pos == fh->size) {
break;
}
*((char *) ebx + ds_base + i) = fh->buf[fh->pos];
fh->pos++;
}
reg[7] = i;
} else if (edx == 26) {
i = 0;
for (;;) {
*((char *) ebx + ds_base + i) = task->cmdline[i];
if (task->cmdline[i] == 0) {
break;
}
if (i >= ecx) {
break;
}
i++;
}
reg[7] = i;
} else if (edx == 27) {
reg[7] = task->langmode;
}
return 0;
}
int *inthandler0c(int *esp)
{
struct TASK *task = task_now();
struct CONSOLE *cons = task->cons;
char s[30];
cons_putstr0(cons, "\nINT 0C :\n Stack Exception.\n");
sprintf(s, "EIP = %08X\n", esp[11]);
cons_putstr0(cons, s);
return &(task->tss.esp0); /* <20>ُ<EFBFBD>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
}
int *inthandler0d(int *esp)
{
struct TASK *task = task_now();
struct CONSOLE *cons = task->cons;
char s[30];
cons_putstr0(cons, "\nINT 0D :\n General Protected Exception.\n");
sprintf(s, "EIP = %08X\n", esp[11]);
cons_putstr0(cons, s);
return &(task->tss.esp0); /* <20>ُ<EFBFBD>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
}
void hrb_api_linewin(struct SHEET *sht, int x0, int y0, int x1, int y1, int col)
{
int i, x, y, len, dx, dy;
dx = x1 - x0;
dy = y1 - y0;
x = x0 << 10;
y = y0 << 10;
if (dx < 0) {
dx = - dx;
}
if (dy < 0) {
dy = - dy;
}
if (dx >= dy) {
len = dx + 1;
if (x0 > x1) {
dx = -1024;
} else {
dx = 1024;
}
if (y0 <= y1) {
dy = ((y1 - y0 + 1) << 10) / len;
} else {
dy = ((y1 - y0 - 1) << 10) / len;
}
} else {
len = dy + 1;
if (y0 > y1) {
dy = -1024;
} else {
dy = 1024;
}
if (x0 <= x1) {
dx = ((x1 - x0 + 1) << 10) / len;
} else {
dx = ((x1 - x0 - 1) << 10) / len;
}
}
for (i = 0; i < len; i++) {
sht->buf[(y >> 10) * sht->bxsize + (x >> 10)] = col;
x += dx;
y += dy;
}
return;
}

59
30_day/haribote/dsctbl.c Normal file
View File

@@ -0,0 +1,59 @@
/* GDT、IDT、descriptor table 关系处理 */
#include "bootpack.h"
void init_gdtidt(void)
{
struct SEGMENT_DESCRIPTOR *gdt = (struct SEGMENT_DESCRIPTOR *) ADR_GDT;
struct GATE_DESCRIPTOR *idt = (struct GATE_DESCRIPTOR *) ADR_IDT;
int i;
/* GDT初始化 */
for (i = 0; i <= LIMIT_GDT / 8; i++) {
set_segmdesc(gdt + i, 0, 0, 0);
}
set_segmdesc(gdt + 1, 0xffffffff, 0x00000000, AR_DATA32_RW);
set_segmdesc(gdt + 2, LIMIT_BOTPAK, ADR_BOTPAK, AR_CODE32_ER);
load_gdtr(LIMIT_GDT, ADR_GDT);
/* IDT初始化 */
for (i = 0; i <= LIMIT_IDT / 8; i++) {
set_gatedesc(idt + i, 0, 0, 0);
}
load_idtr(LIMIT_IDT, ADR_IDT);
/* IDT设置*/
set_gatedesc(idt + 0x0c, (int) asm_inthandler0c, 2 * 8, AR_INTGATE32);
set_gatedesc(idt + 0x0d, (int) asm_inthandler0d, 2 * 8, AR_INTGATE32);
set_gatedesc(idt + 0x20, (int) asm_inthandler20, 2 * 8, AR_INTGATE32);
set_gatedesc(idt + 0x21, (int) asm_inthandler21, 2 * 8, AR_INTGATE32);
set_gatedesc(idt + 0x2c, (int) asm_inthandler2c, 2 * 8, AR_INTGATE32);
set_gatedesc(idt + 0x40, (int) asm_hrb_api, 2 * 8, AR_INTGATE32 + 0x60);
return;
}
void set_segmdesc(struct SEGMENT_DESCRIPTOR *sd, unsigned int limit, int base, int ar)
{
if (limit > 0xfffff) {
ar |= 0x8000; /* G_bit = 1 */
limit /= 0x1000;
}
sd->limit_low = limit & 0xffff;
sd->base_low = base & 0xffff;
sd->base_mid = (base >> 16) & 0xff;
sd->access_right = ar & 0xff;
sd->limit_high = ((limit >> 16) & 0x0f) | ((ar >> 8) & 0xf0);
sd->base_high = (base >> 24) & 0xff;
return;
}
void set_gatedesc(struct GATE_DESCRIPTOR *gd, int offset, int selector, int ar)
{
gd->offset_low = offset & 0xffff;
gd->selector = selector;
gd->dw_count = (ar >> 8) & 0xff;
gd->access_right = ar & 0xff;
gd->offset_high = (offset >> 16) & 0xffff;
return;
}

63
30_day/haribote/fifo.c Normal file
View File

@@ -0,0 +1,63 @@
/* FIFO */
#include "bootpack.h"
#define FLAGS_OVERRUN 0x0001
void fifo32_init(struct FIFO32 *fifo, int size, int *buf, struct TASK *task)
/* FIFO缓冲区的初始化*/
{
fifo->size = size;
fifo->buf = buf;
fifo->free = size; /*空*/
fifo->flags = 0;
fifo->p = 0; /*写入位置*/
fifo->q = 0; /*读取位置*/
fifo->task = task; /*有数据写入时需要唤醒的任务*/
return;
}
int fifo32_put(struct FIFO32 *fifo, int data)
/*向FIFO写入数据并累积起来*/
{
if (fifo->free == 0) {
/*没有空余空间,溢出*/
fifo->flags |= FLAGS_OVERRUN;
return -1;
}
fifo->buf[fifo->p] = data;
fifo->p++;
if (fifo->p == fifo->size) {
fifo->p = 0;
}
fifo->free--;
if (fifo->task != 0) {
if (fifo->task->flags != 2) { /*如果任务处于休眠状态*/
task_run(fifo->task, -1, 0); /*将任务唤醒*/
}
}
return 0;
}
int fifo32_get(struct FIFO32 *fifo)
/*从FIFO取得一个数据*/
{
int data;
if (fifo->free == fifo->size) {
/*当缓冲区为空的情况下返回-1*/
return -1;
}
data = fifo->buf[fifo->q];
fifo->q++;
if (fifo->q == fifo->size) {
fifo->q = 0;
}
fifo->free++;
return data;
}
int fifo32_status(struct FIFO32 *fifo)
/*报告已经存储了多少数据*/
{
return fifo->size - fifo->free;
}

94
30_day/haribote/file.c Normal file
View File

@@ -0,0 +1,94 @@
/* 文件相关函数 */
#include "bootpack.h"
void file_readfat(int *fat, unsigned char *img)
/*将磁盘映像中的FAT解压缩 */
{
int i, j = 0;
for (i = 0; i < 2880; i += 2) {
fat[i + 0] = (img[j + 0] | img[j + 1] << 8) & 0xfff;
fat[i + 1] = (img[j + 1] >> 4 | img[j + 2] << 4) & 0xfff;
j += 3;
}
return;
}
void file_loadfile(int clustno, int size, char *buf, int *fat, char *img)
{
int i;
for (;;) {
if (size <= 512) {
for (i = 0; i < size; i++) {
buf[i] = img[clustno * 512 + i];
}
break;
}
for (i = 0; i < 512; i++) {
buf[i] = img[clustno * 512 + i];
}
size -= 512;
buf += 512;
clustno = fat[clustno];
}
return;
}
struct FILEINFO *file_search(char *name, struct FILEINFO *finfo, int max)
{
int i, j;
char s[12];
for (j = 0; j < 11; j++) {
s[j] = ' ';
}
j = 0;
for (i = 0; name[i] != 0; i++) {
if (j >= 11) { return 0; /*没有找到*/ }
if (name[i] == '.' && j <= 8) {
j = 8;
} else {
s[j] = name[i];
if ('a' <= s[j] && s[j] <= 'z') {
/*将小写字母转换为大写字母*/
s[j] -= 0x20;
}
j++;
}
}
for (i = 0; i < max; ) {
if (finfo->name[0] == 0x00) {
break;
}
if ((finfo[i].type & 0x18) == 0) {
for (j = 0; j < 11; j++) {
if (finfo[i].name[j] != s[j]) {
goto next;
}
}
return finfo + i; /*找到文件*/
}
next:
i++;
}
return 0; /*没有找到*/
}
char *file_loadfile2(int clustno, int *psize, int *fat)
{
int size = *psize, size2;
struct MEMMAN *memman = (struct MEMMAN *) MEMMAN_ADDR;
char *buf, *buf2;
buf = (char *) memman_alloc_4k(memman, size);
file_loadfile(clustno, size, buf, fat, (char *) (ADR_DISKIMG + 0x003e00));
if (size >= 17) {
size2 = tek_getsize(buf);
if (size2 > 0) { /*使用tek格式压缩的文件*/
buf2 = (char *) memman_alloc_4k(memman, size2);
tek_decomp(buf, buf2, size2);
memman_free_4k(memman, (int) buf, size);
buf = buf2;
*psize = size2;
}
}
return buf;
}

Some files were not shown because too many files have changed in this diff Show More