
;	'VIEW' command for native-mode
;	run386 VIEW
	.386p



SIZE_X	equ	160
SIZE_Y	equ	160

MOVE_X	equ	4
MOVE_Y	equ	4



CODE	segment
	assume	cs:CODE
	org 0


MAIN	proc

	pusha

;	mov	eax,0
;	push	eax
;	mov	eax,1
;	push	eax
;	mov	eax,0
;	push	eax
;	call	analog
;	add	esp,12



	sub	esp,256*1024	;vram buffer wo stack ni tukuru
	mov	ebp,esp
	
	mov	ax,120h		;vram wo save
	mov	fs,ax
	mov	ax,ss
	mov	es,ax
	mov	ecx,256*1024/4
	mov	edi,ebp
	mov	esi,0
	cld
	rep movs	DWORD PTR [edi],DWORD PTR fs:[esi]
	
	
	sub	esp,1536	;EGB buffer wo stack ni toru
	
	push	ss		;graphic no initialize
	pop	gs
	mov	edi,esp
	mov	ecx,1536
	mov	ah,0
	push	0110h
	pop	fs
	call	PWORD PTR fs:[20h]
	
	mov	ah,01h		;page = 0  wo gamen mode 5
	mov	al,0
	mov	dx,5
	call	PWORD PTR fs:[20h]
	
	mov	ah,02h		;hyouji ichi wo mannnakanisuru
	mov	al,0
	mov	dx,80
	mov	bx,0
	call	PWORD PTR fs:[20h]
	
	mov	ah,02h		;kakudairitu wo setteisuru
	mov	al,2
	mov	dx,3
	mov	bx,3
	call	PWORD PTR fs:[20h]
	
	mov	ah,02h		;hyoujino ookisa wo setteisuru
	mov	al,3
	mov	dx,160
	mov	bx,160
	call	PWORD PTR fs:[20h]
	
	mov	ah,06h		;hyoujino settei
	mov	al,1
	mov	edx,1
	call	PWORD PTR fs:[20h]
	
	
	push	0120h		;test test test
	pop	es
	mov	edi,0

	call	dsp
	call	chg_scr




#looptop:
	
	push	edi
	mov	eax,offset aj_ch
	push	eax
	mov	eax,1
	push	eax
	mov	eax,2
	push	eax
	call	analog
	add	esp,12
	pop	edi
	
	
;	cmp	dl,0ffh
;	je	#looptop
	
	mov	ecx,ds:[aj_ch+0]
	cmp	ecx,128
	ja	#l1
	neg	ecx
	add	ecx,128
	jmp	#l2
	
#l1:	sub	ecx,128
	
#l2:	
	mov	eax,ds:[SIN1]
	mov	ebx,SIZE_X*256/1024
	mul	ebx
	mul	ecx
	shrd	eax,edx,8
	mov	ds:[GX],eax
	
	mov	eax,ds:[COS1]
	mov	ebx,SIZE_X*256/1024
	mul	ebx
	mul	ecx
	shrd	eax,edx,7
	mov	ds:[GY],eax
	
	
	
	
;	test	ds:[JOYDAT],02h
;	jne	#not_down
	
	cmp	dword ptr ds:[aj_ch],128
	jb	#not_down
	
	cmp	ds:[TH],64
	jae	#down_2
	
	mov	eax,ds:[GX]
	sub	ds:[CNX],eax
	mov	eax,ds:[GY]
	add	ds:[CNY],eax
	jmp	#not_down
	
#down_2:
	cmp	ds:[TH],128
	jae	#down_3
	
	mov	eax,ds:[GX]
	sub	ds:[CNX],eax
	mov	eax,ds:[GY]
	sub	ds:[CNY],eax
	jmp	#not_down
	
#down_3:
	cmp	ds:[TH],192
	jae	#down_4
	
	mov	eax,ds:[GX]
	add	ds:[CNX],eax
	mov	eax,ds:[GY]
	sub	ds:[CNY],eax
	jmp	#not_down
	
#down_4:
	
	mov	eax,ds:[GX]
	add	ds:[CNX],eax
	mov	eax,ds:[GY]
	add	ds:[CNY],eax
	
	
	
	
#not_down:
;	test	ds:[JOYDAT],01h
;	jne	#not_up
	cmp	dword ptr ds:[aj_ch],128
	jnb	#not_up
	
	cmp	ds:[TH],64
	jae	#up_2
	
	mov	eax,ds:[GX]
	add	ds:[CNX],eax
	mov	eax,ds:[GY]
	sub	ds:[CNY],eax
	jmp	#not_up
	
#up_2:
	cmp	ds:[TH],128
	jae	#up_3
	
	mov	eax,ds:[GX]
	add	ds:[CNX],eax
	mov	eax,ds:[GY]
	add	ds:[CNY],eax
	jmp	#not_up
	
#up_3:
	cmp	ds:[TH],192
	jae	#up_4
	
	mov	eax,ds:[GX]
	sub	ds:[CNX],eax
	mov	eax,ds:[GY]
	add	ds:[CNY],eax
	jmp	#not_up
	
#up_4:
	
	mov	eax,ds:[GX]
	sub	ds:[CNX],eax
	mov	eax,ds:[GY]
	sub	ds:[CNY],eax
	
	
	
	
#not_up:
;	test	ds:[JOYDAT],04h
;	jne	#not_left
;	
;	sub	ds:[TH],2
;	
;	
#not_left:
;	test	ds:[JOYDAT],08h
;	jne	#not_right
;	
;	add	ds:[TH],2
	
	mov	eax,ds:[aj_ch+4]
	add	ds:[TH2],eax
	sub	ds:[TH2],128
	mov	eax,ds:[TH2]
	shr	eax,5
	mov	ds:[TH],al
	
	
#not_right:
;	test	ds:[aj_ch+16],02h
;	jne	#not_a
	
;	mov	ax,word ptr ds:[ZM]
;	mov	bx,0110h
;	mul	bx
;	shrd	ax,dx,8
;	mov	word ptr ds:[ZM],ax
;	cmp	word ptr ds:[ZM],1F00h
;	jle	#not_a
;	mov	ds:[ZM],1F00h
	
	mov	eax,ds:[aj_ch+8]
	mov	ebx,128*8
	sub	ebx,eax
	add	ebx,128
	mov	eax,ds:[ZM2]
	mul	ebx
	shr	eax,10
	mov	ds:[ZM2],eax
	cmp	ds:[ZM2],100
	ja	#l3
	mov	ds:[ZM2],100
#l3:
	cmp	ds:[ZM2],30000
	jna	#l4
	mov	ds:[ZM2],30000
#l4:
	mov	eax,ds:[ZM2]
;	mov	edx,0
;	mov	eax,512*256
;	div	ebx
	shr	eax,3
	mov	ds:[ZM],ax
	
#not_a:
;	test	ds:[JOYDAT],20h
;	jne	#not_b
;	
;	mov	ax,word ptr ds:[ZM]
;	mov	bx,00f0h
;	mul	bx
;	shrd	ax,dx,8
;	mov	word ptr ds:[ZM],ax
;	cmp	word ptr ds:[ZM],0010h
;	jg	#not_b
;	mov	ds:[ZM],0010h
	
	
#not_b:
	test	ds:[aj_ch+16],010h
	jne	#not_select
	
	jmp	#exit
	
	
#not_select:
	test	ds:[aj_ch+16],020h
	jne	#not_run
	
	mov	ds:[ZM2],0100h*8
	mov	ds:[TH2],0
	mov	ds:[CNX],(SIZE_X/2)*00800000h
	mov	ds:[CNY],(SIZE_Y/2)*01000000h
	
	
	
#not_run:
	
	call	dsp
	call	chg_scr
	
	jmp	#looptop
	













	
#exit:
	mov	edi,esp
	push	0110h
	pop	fs

	mov	ah,01h		;page = 0  wo gamen mode 10
	mov	al,0
	mov	dx,10
	call	PWORD PTR fs:[20h]
	
	mov	ah,02h		;hyouji ichi wo modosu
	mov	al,0
	mov	dx,0
	mov	bx,0
	call	PWORD PTR fs:[20h]
	
	mov	ah,02h		;kakudairitu wo setteisuru
	mov	al,2
	mov	dx,2
	mov	bx,2
	call	PWORD PTR fs:[20h]
	
	mov	ah,02h		;hyoujino ookisa wo setteisuru
	mov	al,3
	mov	dx,320
	mov	bx,240
	call	PWORD PTR fs:[20h]
	
	mov	ah,06h		;hyoujino settei
	mov	al,1
	mov	edx,3
	call	PWORD PTR fs:[20h]
	
	mov	ax,120h		;vram wo modosu
	mov	es,ax
	mov	ax,ss
	mov	fs,ax
	mov	ecx,256*1024/4
	mov	edi,0
	mov	esi,ebp
	cld
	rep movs	DWORD PTR [edi],DWORD PTR fs:[esi]
	
	
	add	esp,1536	;EGB buffer wo kaihou
	add	esp,256*1024	;vram buffer wo kaihou
	
	popa
	mov	ah,4Ch
	int	21h


MAIN	endp


dsp	proc

	call	tri
	
	mov	ax,ds:[COS]
	mov	bx,ds:[ZM]
	mul	bx
	mov	word ptr ds:[COS1+2],dx
	mov	word ptr ds:[COS1+0],ax
	shl	eax,16
	shld	edx,eax,16
	mov	ds:[DDX],edx
	
	mov	ax,ds:[SIN]
	mov	bx,ds:[ZM]
	mul	bx
	mov	word ptr ds:[SIN1+2],dx
	mov	word ptr ds:[SIN1+0],ax
	shl	eax,16
	shld	edx,eax,17
	mov	ds:[DDY],edx
	
	
	
	mov	eax,ds:[COS1]
	mov	ebx,SIZE_X/2*256
	mul	ebx
	shrd	eax,edx,8
	mov	ds:[XCOS],eax
	
	mov	eax,ds:[SIN1]
	mov	ebx,SIZE_X/2*256
	mul	ebx
	shrd	eax,edx,8
	mov	ds:[XSIN],eax
	
	
	
	
	mov	eax,ds:[COS1]
	mov	ebx,SIZE_Y/2*256
	mul	ebx
	shrd	eax,edx,8
	mov	ds:[YCOS],eax
	
	mov	eax,ds:[SIN1]
	mov	ebx,SIZE_Y/2*256
	mul	ebx
	shrd	eax,edx,8
	mov	ds:[YSIN],eax
	
	
	
	mov	eax,ds:[CNX]
	mov	ds:[GX],eax
	mov	eax,ds:[CNY]
	mov	ds:[GY],eax
	
	
	
	cmp	ds:[TH],63
	jbe	case1
	
	cmp	ds:[TH],127
	jbe	case2
	
	cmp	ds:[TH],191
	jbe	case3
	
	jmp	case4

dsp	endp




case1	proc
	
	mov	eax,ds:[XCOS]
	sub	ds:[GX],eax
	mov	eax,ds:[YSIN]
	add	ds:[GX],eax
	
	mov	eax,ds:[XSIN]
	shl	eax,1
	sub	ds:[GY],eax
	mov	eax,ds:[YCOS]
	shl	eax,1
	sub	ds:[GY],eax
	
	
	
	mov	ebx,ds:[GY]
	mov	esi,ds:[GX]
	
	mov	ecx,ds:[DDX]
	mov	edx,ds:[DDY]
	shl	ecx,1
	mov	ds:[DDY1],ecx
	shr	edx,1
	mov	ds:[DDX1],edx
	
	mov	ecx,ds:[DDX]
	mov	edx,ds:[DDY]
	
	mov	ds:[LOPCNT1],SIZE_Y
	
	align	4
#l1:
	
	
	mov	ds:[LOPCNT2],SIZE_X
	
	align	4
#l2:
	xor	eax,eax
	shld	eax,ebx,8
	shld	eax,esi,9
	mov	ax,[ebp+eax*2]
	stosw
	add	esi,ecx
	add	ebx,edx
	
	dec	DWORD PTR ds:[LOPCNT2]
	jne	short #l2
	
	
	mov	ebx,ds:[GY]
	mov	esi,ds:[GX]
	sub	esi,ds:[DDX1]
	add	ebx,ds:[DDY1]
	mov	ds:[GY],ebx
	mov	ds:[GX],esi
	add	edi,(256-SIZE_X)*2
	
	dec	DWORD PTR ds:[LOPCNT1]
	jne	#l1
	
	
	
	
	ret
case1	endp


case2	proc
	
	mov	eax,ds:[XCOS]
	add	ds:[GX],eax
	mov	eax,ds:[YSIN]
	add	ds:[GX],eax
	
	mov	eax,ds:[XSIN]
	shl	eax,1
	sub	ds:[GY],eax
	mov	eax,ds:[YCOS]
	shl	eax,1
	add	ds:[GY],eax
	
	
	
	mov	ebx,ds:[GY]
	mov	esi,ds:[GX]
	
	mov	ecx,ds:[DDX]
	mov	edx,ds:[DDY]
	shl	ecx,1
	mov	ds:[DDY1],ecx
	shr	edx,1
	mov	ds:[DDX1],edx
	
	mov	ecx,ds:[DDX]
	mov	edx,ds:[DDY]
	
	mov	ds:[LOPCNT1],SIZE_Y
	
	align	4
#l1:
	
	
	mov	ds:[LOPCNT2],SIZE_X
	
	align	4
#l2:
	xor	eax,eax
	shld	eax,ebx,8
	shld	eax,esi,9
	mov	ax,[ebp+eax*2]
	stosw
	sub	esi,ecx
	add	ebx,edx
	
	dec	DWORD PTR ds:[LOPCNT2]
	jne	short #l2
	
	
	mov	ebx,ds:[GY]
	mov	esi,ds:[GX]
	sub	esi,ds:[DDX1]
	sub	ebx,ds:[DDY1]
	mov	ds:[GY],ebx
	mov	ds:[GX],esi
	add	edi,(256-SIZE_X)*2
	
	dec	DWORD PTR ds:[LOPCNT1]
	jne	#l1
	
	
	
	
	ret
case2	endp

case3	proc
	
	mov	eax,ds:[XCOS]
	add	ds:[GX],eax
	mov	eax,ds:[YSIN]
	sub	ds:[GX],eax
	
	mov	eax,ds:[XSIN]
	shl	eax,1
	add	ds:[GY],eax
	mov	eax,ds:[YCOS]
	shl	eax,1
	add	ds:[GY],eax
	
	
	
	mov	ebx,ds:[GY]
	mov	esi,ds:[GX]
	
	mov	ecx,ds:[DDX]
	mov	edx,ds:[DDY]
	shl	ecx,1
	mov	ds:[DDY1],ecx
	shr	edx,1
	mov	ds:[DDX1],edx
	
	mov	ecx,ds:[DDX]
	mov	edx,ds:[DDY]
	
	mov	ds:[LOPCNT1],SIZE_Y
	
	align	4
#l1:
	
	
	mov	ds:[LOPCNT2],SIZE_X
	
	align	4
#l2:
	xor	eax,eax
	shld	eax,ebx,8
	shld	eax,esi,9
	mov	ax,[ebp+eax*2]
	stosw
	sub	esi,ecx
	sub	ebx,edx
	
	dec	DWORD PTR ds:[LOPCNT2]
	jne	short #l2
	
	
	mov	ebx,ds:[GY]
	mov	esi,ds:[GX]
	add	esi,ds:[DDX1]
	sub	ebx,ds:[DDY1]
	mov	ds:[GY],ebx
	mov	ds:[GX],esi
	add	edi,(256-SIZE_X)*2
	
	dec	DWORD PTR ds:[LOPCNT1]
	jne	#l1
	
	
	
	
	ret
case3	endp

case4	proc
	
	mov	eax,ds:[XCOS]
	sub	ds:[GX],eax
	mov	eax,ds:[YSIN]
	sub	ds:[GX],eax
	
	mov	eax,ds:[XSIN]
	shl	eax,1
	add	ds:[GY],eax
	mov	eax,ds:[YCOS]
	shl	eax,1
	sub	ds:[GY],eax
	
	
	
	mov	ebx,ds:[GY]
	mov	esi,ds:[GX]
	
	mov	ecx,ds:[DDX]
	mov	edx,ds:[DDY]
	shl	ecx,1
	mov	ds:[DDY1],ecx
	shr	edx,1
	mov	ds:[DDX1],edx
	
	mov	ecx,ds:[DDX]
	mov	edx,ds:[DDY]
	
	mov	ds:[LOPCNT1],SIZE_Y
	
	align	4
#l1:
	
	
	mov	ds:[LOPCNT2],SIZE_X
	
	align	4
#l2:
	xor	eax,eax
	shld	eax,ebx,8
	shld	eax,esi,9
	mov	ax,[ebp+eax*2]
	stosw
	add	esi,ecx
	sub	ebx,edx
	
	dec	DWORD PTR ds:[LOPCNT2]
	jne	short #l2
	
	
	mov	ebx,ds:[GY]
	mov	esi,ds:[GX]
	add	esi,ds:[DDX1]
	add	ebx,ds:[DDY1]
	mov	ds:[GY],ebx
	mov	ds:[GX],esi
	add	edi,(256-SIZE_X)*2
	
	dec	DWORD PTR ds:[LOPCNT1]
	jne	#l1
	
	
	
	
	ret
case4	endp


tri	proc

	mov	al,ds:[TH]
	cmp	al,128
	jb	#l1
	
	sub	al,128
	
#l1:
	cmp	al,64
	jbe	#l2
	
	sub	al,128
	neg	al
	
#l2:
	movzx	ax,al
	movzx	eax,ax
	mov	bx,ds:[TRI_TBL+eax*2]
	mov	ds:[SIN],bx
	
	
	
	mov	al,ds:[TH]
	add	al,64
	cmp	al,128
	jb	#l3
	
	sub	al,128
	
#l3:
	cmp	al,64
	jbe	#l4
	
	sub	al,128
	neg	al
	
#l4:
	movzx	ax,al
	movzx	eax,ax
	mov	bx,ds:[TRI_TBL+eax*2]
	mov	ds:[COS],bx
	
	
	
	ret

tri	endp




chg_scr	proc

	cmp	ds:[scr_pg],0
	je	#l1
	
	mov	edi,00000h
	mov     dx,0440h
        mov     al,17
        out     dx,al
        mov     dx,0442h
        mov     ax,8000h
        out     dx,ax
	mov	ds:[scr_pg],0
	ret
#l1:
	
	mov	edi,20000h
	mov     dx,0440h
        mov     al,17
        out     dx,al
        mov     dx,0442h
        mov     ax,0
        out     dx,ax
	mov	ds:[scr_pg],1
	ret

chg_scr	endp





;   void analog(int fnction,int port,int *ch)
;            in       fnction = 0 : set analog mode
;                              = 1 : set digital mode
;                              = 2 : get analog data
;                     port = 1
;                          = 2
;            out      ch[0],ch[1],ch[2],ch[3] : 0---255
;                     ch[4] = trg : A,B,C,D,E1,E2,F,G,A',B'

JOY1_IN		equ	04D0h
JOY2_IN		equ	04D2h
JOY_OUT		equ	04D6h

COM1_ON		equ	1Fh
COM1_OFF	equ	0Fh
COM2_ON		equ	2Fh
COM2_OFF	equ	0Fh



analog	proc	near
	mov	al,ss:[esp+4]
	cmp	al,0
	je	#set_a_mode
	cmp	al,1
	je	#set_d_mode

#get_a_data:
	mov	dx,JOY_OUT
	mov	al,ss:[esp+8]
	cmp	al,1
	jne	#get_a_data_1

	mov	al,COM1_OFF
	out	dx,al
	mov	al,COM1_ON
	out	dx,al
	mov	dx,JOY1_IN
	jmp	#get_a_data_2

#get_a_data_1:
	mov	al,COM2_OFF
	out	dx,al
	mov	al,COM2_ON
	out	dx,al
	mov	dx,JOY2_IN

#get_a_data_2:
	mov	ecx,6
	mov	edi,offset temp


#get_a_data_3:


	mov	ebx,1000
#get_a_data_4:
	in	al,dx
	dec	ebx
	je	#exit
	test	al,10h
	jne	#get_a_data_4

	mov	ebx,1000
#get_a_data_5:
	in	al,dx
	dec	ebx
	je	#exit
	test	al,20h
	jne	#get_a_data_5

	in	al,dx
	and	al,0Fh
	mov	ss:[edi],al
	inc	edi

	mov	ebx,1000
#get_a_data_6:
	in	al,dx
	dec	ebx
	je	#exit
	test	al,10h
	je	#get_a_data_6

	mov	ebx,1000
#get_a_data_7:
	in	al,dx
	dec	ebx
	je	#exit
	test	al,20h
	jne	#get_a_data_7

	in	al,dx
	and	al,0Fh
	mov	ss:[edi],al
	inc	edi

	loop	#get_a_data_3




	mov	edi,ss:[esp+12]

	mov	eax,0
	mov	al,ds:[temp+1]
	shl	al,4
	or	al,ss:[temp+10]
	mov	ah,ss:[temp+0]
	mov	ds:[edi+16],eax

	mov	al,ds:[temp+5]
	shl	al,4
	or	al,ds:[temp+9]
	movzx	eax,al
	mov	ds:[edi+12],eax

	mov	al,ds:[temp+4]
	shl	al,4
	or	al,ds:[temp+8]
	movzx	eax,al
	mov	ds:[edi+8],eax

	mov	al,ds:[temp+3]
	shl	al,4
	or	al,ds:[temp+7]
	movzx	eax,al
	mov	ds:[edi+4],eax

	mov	al,ds:[temp+2]
	shl	al,4
	or	al,ds:[temp+6]
	movzx	eax,al
	mov	ds:[edi+0],eax

	jmp	#exit





#set_a_mode:
	mov	al,ss:[esp+8]
	mov	dx,JOY_OUT
	cmp	al,1
	jne	#set_a_mode_1
	mov	al,COM1_ON
	jmp	#set_a_mode_exit
#set_a_mode_1:
	mov	al,COM2_ON
#set_a_mode_exit:
	out	dx,al
	jmp	#exit


#set_d_mode:
	mov	al,ss:[esp+8]
	mov	dx,JOY_OUT
	cmp	al,1
	jne	#set_d_mode_1
	mov	al,COM1_OFF
	jmp	#set_d_mode_exit
#set_d_mode_1:
	mov	al,COM2_OFF
#set_d_mode_exit:
	out	dx,al
	jmp	#exit


#exit:
	ret
analog	endp








CODE	ends

DATA	segment
	assume	ds:DATA

credit	db	'Copy right T.Higashi 1990'

	align 4
TH2	dd	0
JOYDAT	db	0
TH	db	0
	align 4
ZM2	dd	0100h*8
ZM	dw	0100h

	align 4
CNX	dd	(SIZE_X/2)*00800000h
CNY	dd	(SIZE_Y/2)*01000000h


XSIN	dd	0
XCOS	dd	0
YSIN	dd	0
YCOS	dd	0

SIN	dw	0
COS	dw	0
SIN1	dd	0
COS1	dd	0

GX	dd	0
GY	dd	0
DDX	dd	0
DDY	dd	0

LOPCNT1 dd	0
LOPCNT2 dd	0
DDX1	dd	0
DDY1	dd	0

scr_pg	dd	0


TRI_TBL	dw	0000h,0324h,0648h,096Bh,0C8Ch,0FABh,12C8h,15E2h
	dw	18F9h,1C0Ch,1F1Ah,2224h,2528h,2827h,2B1Fh,2E11h
	dw	30FCh,33DFh,36BAh,398Dh,3C57h,3F17h,41CEh,447Bh
	dw	471Dh,49B4h,4C40h,4EC0h,5134h,539Bh,55F6h,5843h
	dw	5A82h,5CB4h,5ED7h,60ECh,62F2h,64E9h,66CFh,68A7h
	dw	6A6Eh,6C24h,6DCAh,6F5Fh,70E3h,7255h,73B6h,7505h
	dw	7642h,776Ch,7885h,798Ah,7A7Dh,7B5Dh,7C2Ah,7CE4h
	dw	7D8Ah,7E1Eh,7E9Dh,7F0Ah,7F62h,7FA7h,7FD9h,7FF6h
	dw	8000h








temp	db	11 dup(0)


aj_ch	dd	4 dup(0),0ffffffh


DATA	ends


STACK	segment stack
	assume	ss:STACK
	db	300000 dup(?)
STACK	ends

	end	MAIN



