
#include "ide.h"

/* 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_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 clts(void);
void fnsave(int *addr);
void frstor(int *addr);
void asm_inthandler07(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);

struct RGB getRGB8(unsigned char c);
struct RGB getRGB16(unsigned short c);
struct RGB getRGBA(unsigned int c);

struct RGB getpixelA(void *vram, int xsize, int x, int y);
void setpixelA(void *vram, int xsize, int r, int g, int b, int x, int y);
void boxfillA(void *vram, int xsize, int r, int g, int b, int x0, int y0, int x1, int y1);
int loadimageA(void *vram, char *filename, int x, int y, int bufx, int *fat);
void putfontA(void *vram, int xsize, int x, int y, int r, int g, int b, char *font);
void putstringA(void *vram, int xsize, int x, int y, int r, int g, int b, unsigned char *s);
void putblockA(void *vram, int vxsize, int pxsize, int pysize, int px0, int py0,
			   void *buf, int bxsize);
void init_mouse_cursor(short *mouse, char bc);
void init_mouse_cursor_hide(short *mouse, char bc);

#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

#define COL16_000000	table_8_565[0]
#define COL16_FF0000	table_8_565[1]
#define COL16_00FF00	table_8_565[2]
#define COL16_FFFF00	table_8_565[3]
#define COL16_0000FF	table_8_565[4]
#define COL16_FF00FF	table_8_565[5]
#define COL16_00FFFF	table_8_565[6]
#define COL16_FFFFFF	table_8_565[7]
#define COL16_C6C6C6	table_8_565[8]
#define COL16_840000	table_8_565[9]
#define COL16_008400	table_8_565[10]
#define COL16_848400	table_8_565[11]
#define COL16_000084	table_8_565[12]
#define COL16_840084	table_8_565[13]
#define COL16_008484	table_8_565[14]
#define COL16_848484	table_8_565[15]

#define COLA_000000		0,0,0
#define COLA_FF0000		0xff,0,0
#define COLA_00FF00		0,0xff,0
#define COLA_FFFF00		0xff,0xff,0
#define COLA_0000FF		0,0,0xff
#define COLA_FF00FF		0xff,0,0xff
#define COLA_00FFFF		0xff,0xff,0
#define COLA_FFFFFF		0xff,0xff,0xff
#define COLA_C6C6C6		0xc6,0xc6,0xc6
#define COLA_840000		0x84,0,0
#define COLA_008400		0,0x84,0
#define COLA_848400		0x84,0x84,0
#define COLA_000084		0,0,0x84
#define COLA_840084		0x84,0,0x84
#define COLA_008484		0,0x84,0x84
#define COLA_848484		0x84,0x84,0x84
#define COLA_RGB(rgb)	rgb.r,rgb.g,rgb.b


/* 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	/* これで約32KB */
#define MEMMAN_ADDR			0x003c0000
struct FREEINFO {	/* あき情報 */
	unsigned int addr, size;
};
struct MEMMAN {		/* メモリ管理 */
	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 short *buf;
	char *name;
	int bxsize, bysize, vx0, vy0, col_inv, height, flags, isact;
	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 short *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		/* TSSをGDTの何番から割り当てるのか */
#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;
	int fpu[108 / 4];
	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; /* 現在動作中のレベル */
	struct TASK *task_fpu;
	char lv_change; /* 次回タスクスイッチのときに、レベルも変えたほうがいいかどうか */
	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);
int *inthandler07(int *esp);

/* window.c */
struct HSPRGB {
	int r, g, b;
};
void putfonts8_asc_sht(struct SHEET *sht, int x, int y, struct HSPRGB c, int b, char *s, int l);

/* file.c */
struct FILEINFO {
	unsigned char name[8], ext[3], type;
	char reserve[10];
	unsigned short time, date, clustno;
	unsigned int size;
};
struct FILEHANDLE {
	char *buf;
	int size;
	int bsize;
	int pos;
	int bpos;
	int clustno;
	struct FILEINFO *finfo;
};
typedef struct {
	const char *name;
	int mode;
	int size;
	IDEDriver *driver;
	PrimaryVolumeDescriptor *pvd;
} FILE;

FILE *fopen(const char *filename, const char *mode);
int fclose(FILE *fp);
int fread(void *buf, int size, int count, FILE *fp);

void file_readfat(int *fat, unsigned char *img);
void file_loadfile(int clustno, int size, char *buf, int *fat, char *img);
char *file_loadfile2(int clustno, int *psize, int *fat);
struct FILEINFO *file_search(char *name, struct FILEINFO *finfo, int max);
int file_searchfat(int *fat);
void file_clearfat(int *fat, int clustno);
int file_savefile(struct FILEHANDLE *fh, int size, int *fat, char *img);
int file_skipfile(struct FILEHANDLE *fh, int size, int *fat, char *img, int mode);
struct FILEINFO *file_insert(char *name, struct FILEINFO *finfo, int max, int *fat);
void file_move(char *name, struct FILEINFO *finfo);
void file_inittbl(void);
void file_list(int *fat);

/* tek.c */
int tek_getsize(unsigned char *p);
int tek_decomp(unsigned char *p, char *q, int size);

/* jpeg.c */
struct DLL_STRPICENV {	/* 64KB */
	int work[64 * 1024 / 4];
};
struct RGB {
	unsigned char b, g, r, t;
};
int info_JPEG(struct DLL_STRPICENV *env, int *info, int size, unsigned char *fp);
int decode0_JPEG(struct DLL_STRPICENV *env, int size, unsigned char *fp, int b_type, unsigned char *buf, int skip);

/* 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);

/* hsp.c */
void init_hsp_task(void);

/* shutdown.c */
int initAcpi(void);
void acpiPowerOff(void);

/* bootpack.c */
void mouse(int x, int y, int mode);
