#uselib "msvcrt"
#cfunc strtol "strtol" str, nullptr, int

#include "enum_NM.hsp"
#include "enum_type.hsp"

#module mnemonic id,string,p_num,p_type,comment
#modfunc local registration int i,str s,int n,int m,str c
	sdim string,256
	sdim comment,256
	id=i
	string=s
	p_num=n
	p_type=m
	comment=c
	return
#modcfunc local get_id
	return id
#modcfunc local get_string
	return string
#modcfunc local get_p_num
	return p_num
#modcfunc local get_p_type
	return p_type
#modcfunc local get_comment
	return comment
#modcfunc local dump
	s="id:"+id+" name:"+string+" p_num:"+p_num+" comment:"+comment
	return s
#global

//	ꂪ1NMsDISP
#define DISP_ON 1


	repeat DISP+1
		newmod nm,mnemonic
		loop
	registration@mnemonic nm(ASTOP),ASTOP,"&+ASTOP",0,0,"stop"
	registration@mnemonic nm(NOP),NOP,"&+NOP",0,0,"Ȃ"
	registration@mnemonic nm(LD), LD, "&+LD", 1,type_int_address,"[h"
	registration@mnemonic nm(ST), ST, "&+ST", 1,type_int_address,"XgA"
	registration@mnemonic nm(LDI),LDI,"&+LDI",1,type_int,"l[h"
	registration@mnemonic nm(LDX),LDX,"&+LDX",1,0,"[hiԐځj"
	registration@mnemonic nm(STX),STX,"&+STX",1,0,"XgAiԐځj"
	registration@mnemonic nm(ADD),ADD,"&+ADD",1,type_int_address,"Z"
	registration@mnemonic nm(SUB),SUB,"&+SUB",1,type_int_address,"Z"
	registration@mnemonic nm(ADD_I),ADD_I,"&+ADD_I",1,type_int,"lZ"
	registration@mnemonic nm(SUB_I),SUB_I,"&+SUB_I",1,type_int,"lZ"
	registration@mnemonic nm(MUL),MUL,"&+MUL",1,type_int_address,"Z"
	registration@mnemonic nm(DIV),DIV,"&+DIV",1,type_int_address,"Z"
	registration@mnemonic nm(MOD),MOD,"&+MOD",1,type_int_address,"]"
	registration@mnemonic nm(MUL_I),MUL_I,"&+MUL_I",1,type_int,"lZ"
	registration@mnemonic nm(DIV_I),DIV_I,"&+DIV_I",1,type_int,"lZ"
	registration@mnemonic nm(MOD_I),MOD_I,"&+MOD_I",1,type_int,"l]"
	registration@mnemonic nm(ADDX),ADDX,"&+ADDX",1,0,"ZiԐځj"
	registration@mnemonic nm(SUBX),SUBX,"&+SUBX",1,0,"ZiԐځj"
	registration@mnemonic nm(BZ),BZ,"&+BZ",1,0,"[̂Ƃ"
	registration@mnemonic nm(BNZ),BNZ,"&+BNZ",1,0,"[̂Ƃ"
	registration@mnemonic nm(AWAIT1),AWAIT1,"&+AWAIT1",0,0,"await 1"
	registration@mnemonic nm(AWAIT_X),AWAIT_X,"&+AWAIT_X",1,type_int,"await"
	registration@mnemonic nm(WAIT_X),WAIT_X,"&+WAIT_X",1,type_int,"wait"
	registration@mnemonic nm(DISP),DISP,"&+DISP",0,0,"WX^̓e\"

////////////////////////////////////////////////////////////////////////////////
//	R[h
////////////////////////////////////////////////////////////////////////////////
	
sdim sc,2000
/*
sdim sc_A_pre,2000
sc_A_pre={"type_mnemonic,&+NOP
MN,&+LD,	type_int,0x8000
MN,&+ST,	int,0x8001"}
sdim sc_A,2000
sc_A={"type_mnemonic,&+NOP
MN,&+LD,	type_int_address,0x8000
MN,&+ST,	type_int_address,0x8001"}

//	LD,ST,LDITEST
sdim sc_B,2000
sc_B={"MN,&+LDI,	int,12345
MN,&+ST,	int,0x7000
MN,&+LD,	int,0x7000
MN,&+DISP
MN,&+LDI,	int,23456
MN,&+ST,	int,0x4000
MN,&+LD,	int,0x4000
MN,&+DISP"}
//	LD,ST,LDITEST(DISP ON)
sdim sc_B_B,2000
sc_B_B={"MN,&+LDI,	int,12345
MN,&+ST,	int,0x7000
MN,&+LD,	int,0x7000
MN,&+LDI,	int,23456
MN,&+ST,	int,0x4000
MN,&+LD,	int,0x4000"}
//	LD,ST,LDITEST(int_adr ver)
sdim sc_B_C,2000
sc_B_C={"MN,&+LDI,	int,12345
MN,&+ST,	type_int_address,0x7000
MN,&+LD,	type_int_address,0x7000
MN,&+LDI,	int,23456
MN,&+ST,	int_adr,0x4000
MN,&+LD,	int_adr,0x4000"}

// addsubTEST
sdim sc_C,2000
sc_C={"MN,&+LDI,	int,10
MN,&+DISP
MN,&+ST,	int,0x2000
MN,&+LDI,	int,20
MN,&+DISP
MN,&+ADD,	int,0x2000
MN,&+DISP
MN,&+LDI,	int,30
MN,&+DISP
MN,&+ST,	int,0x3000
MN,&+LDI,	int,50
MN,&+DISP
MN,&+SUB,	int,0x3000
MN,&+DISP"}

// muldivmodTEST
sdim sc_D,2000
sc_D={"MN,&+LDI,	int,10
MN,&+DISP
MN,&+ST,	int,0x2000
MN,&+LDI,	int,20
MN,&+DISP
MN,&+MUL,	int,0x2000
MN,&+DISP
MN,&+LDI,	int,300
MN,&+DISP
MN,&+ST,	int,0x3000
MN,&+LDI,	int,50
MN,&+DISP
MN,&+DIV,	int,0x3000
MN,&+DISP
MN,&+LDI,	int,350
MN,&+DISP
MN,&+ST,	int,0x4000
MN,&+LDI,	int,50
MN,&+DISP
MN,&+MOD,	int,0x4000
MN,&+DISP"}*/
sdim sc_A,2000
sc_A={"type_mnemonic,&+NOP
MN,&+LD,	type_int_address,0x8000
MN,&+ST,	type_int_address,0x8001"}
sc=sc_A
//	LD,ST,LDITEST
sdim sc_B,2000
sc_B={"MN,&+LDI,	int,12345
MN,&+ST,	type_int_address,0x7000
MN,&+LD,	type_int_address,0x7000
MN,&+LDI,	int,23456
MN,&+ST,	int_adr,0x4000
MN,&+LD,	int_adr,0x4000"}
// addsubTEST
/*sdim sc_C,2000
sc_C={"MN,&+LDI,	int,10
MN,&+DISP
MN,&+ST,	int,0x2000
MN,&+LDI,	int,20
MN,&+DISP
MN,&+ADD,	int,0x2000
MN,&+DISP
MN,&+LDI,	int,30
MN,&+DISP
MN,&+ST,	int,0x3000
MN,&+LDI,	int,50
MN,&+DISP
MN,&+SUB,	int,0x3000
MN,&+DISP"}*/
sdim sc_C,2000
/*sc_C={"MN,&+LDI,	int,10
MN,&+ST,	int_adr,0x2000
MN,&+LDI,	int,20
MN,&+ADD_I,	int_adr,0x2000
MN,&+LDI,	int,30
MN,&+ST,	int_adr,0x3000
MN,&+LDI,	int,50
MN,&+SUB_I,	int_adr,0x3000"}*/
sc_C={"MN,&+LDI,	int,10
MN,&+ST,	int_adr,0x2000
MN,&+LDI,	int,20
MN,&+ADD,	int_adr,0x2000
MN,&+LDI,	int,30
MN,&+ST,	int_adr,0x3000
MN,&+LDI,	int,50
MN,&+SUB,	int_adr,0x3000"}
sdim sc_C_B,2000
sc_C_B={"
MN,&+LDI,	int,20
MN,&+ADD_I,	int,10
MN,&+LDI,	int,50
MN,&+SUB_I,	int,30
MN,&+LDI,	int,10
MN,&+SUB_I,	int,40"}
// muldivmodTEST
sdim sc_D,2000
/*sc_D={"MN,&+LDI,	int,10
MN,&+DISP
MN,&+ST,	int,0x2000
MN,&+LDI,	int,20
MN,&+DISP
MN,&+MUL,	int,0x2000
MN,&+DISP
MN,&+LDI,	int,300
MN,&+DISP
MN,&+ST,	int,0x3000
MN,&+LDI,	int,50
MN,&+DISP
MN,&+DIV,	int,0x3000
MN,&+DISP
MN,&+LDI,	int,350
MN,&+DISP
MN,&+ST,	int,0x4000
MN,&+LDI,	int,50
MN,&+DISP
MN,&+MOD,	int,0x4000
MN,&+DISP"}*/
sc_D={"MN,&+LDI,	int,10
MN,&+ST,	int_adr,0x2000
MN,&+LDI,	int,20
MN,&+MUL,	int_adr,0x2000
MN,&+LDI,	int,300
MN,&+ST,	int_adr,0x3000
MN,&+LDI,	int,50
MN,&+DIV,	int_adr,0x3000
MN,&+LDI,	int,350
MN,&+ST,	int_adr,0x4000
MN,&+LDI,	int,50
MN,&+MOD,	int_adr,0x4000"}
sc_D_B={"MN,&+LDI,	int,20
MN,&+MUL_I,	int,0x10
MN,&+LDI,	int,300
MN,&+DIV_I,	int,70
MN,&+LDI,	int,500
MN,&+MOD_I,	int,350"}
// await1 await_x wait_x TEST
sdim sc_E,2000
sc_E={"MN,&+AWAIT1
MN,&+AWAIT_X,	int,2000
MN,&+AWAIT_X,	int,2000
MN,&+AWAIT_X,	int,2000
MN,&+WAIT_X,	int,200
MN,&+WAIT_X,	int,200
MN,&+WAIT_X,	int,200"}
button "TEST A",	*push_TEST_A
button "TEST B",	*push_TEST_B
button "TEST C",	*push_TEST_C
button "TEST C B",	*push_TEST_C_B
button "TEST D",	*push_TEST_D
button "TEST D B",	*push_TEST_D_B
button "TEST E",	*push_TEST_E
button "",*analyze
pos 150,0
sdim sel_code
mesbox sel_code,640-150,480
ID4sel_code_MB=stat
stop
*push_TEST_A
	gsel 0
	sel_code=sc_A
	objprm ID4sel_code_MB,sel_code
	stop
*push_TEST_B
	gsel 0
	sel_code=sc_B
	objprm ID4sel_code_MB,sel_code
	stop
*push_TEST_C
	gsel 0
	sel_code=sc_C
	objprm ID4sel_code_MB,sel_code
	stop
*push_TEST_C_B
	gsel 0
	sel_code=sc_C_B
	objprm ID4sel_code_MB,sel_code
	stop
*push_TEST_D
	gsel 0
	sel_code=sc_D
	objprm ID4sel_code_MB,sel_code
	stop
*push_TEST_D_B
	gsel 0
	sel_code=sc_D_B
	objprm ID4sel_code_MB,sel_code
	stop
*push_TEST_E
	gsel 0
	sel_code=sc_E
	objprm ID4sel_code_MB,sel_code
	stop
////////////////////////////////////////////////////////////////////////////////
//	R[h
////////////////////////////////////////////////////////////////////////////////
*analyze
	sc=sel_code
dim  type    ,$10000
;sdim type_b  ,256,$10000
sdim contents,256,$10000
dim int_contents,$10000
dim  p_number,$10000
dim lines,$10000

read_line=0
this_line=0
sdim s,256
sdim t_1st,256
sdim t_2nd,256

#define youso_NM_begin 0
#define youso_NM 1
#define youso_P1_begin 2
#define youso_P1 3

notesel sc
repeat notemax
	noteget s,read_line
	t_1st=strtrim(s,3,' ')
	t_2nd=strtrim(t_1st,3,'\t')

	sdim u,256,16
	split t_2nd,",",u
	l_u=stat
//	logmes "cm num:"+l_u
//	repeat l_u
//		logmes "cm"+cnt+":"+u.cnt
//		loop
		
	tmp_type=""
;	tmp_type_b=""
	tmp_contents=""
	tmp_p_number=""
	
	tmp_type=u.youso_NM_begin
	if tmp_type=="type_mnemonic"||tmp_type=="MN"{
		tmp_type=type_mnemonic
;		tmp_type_b=""+type_mnemonic+":type_mnemonic"
	}else{
		if tmp_type!=0{
			dialog "\error\ns̐擪̗vftype_mnemonicMN0ɂĉB\nline:"+(read_line+1),1
		}
	}
;	if tmp_type=="type_int"		:tmp_type=""+type_int+":type_int"
	tmp_contents=u.youso_NM
	switch tmp_contents
	case get_string@mnemonic(nm(NOP))
		tmp_int_contents=NOP
		swbreak
	case get_string@mnemonic(nm(LD))
		tmp_int_contents=LD
		swbreak
	case get_string@mnemonic(nm(ST))
		tmp_int_contents=ST
		swbreak
	case get_string@mnemonic(nm(LDI))
		tmp_int_contents=LDI
		swbreak
	case get_string@mnemonic(nm(ADD))
		tmp_int_contents=ADD
		swbreak
	case get_string@mnemonic(nm(SUB))
		tmp_int_contents=SUB
		swbreak
	case get_string@mnemonic(nm(ADD_I))
		tmp_int_contents=ADD_I
		swbreak
	case get_string@mnemonic(nm(SUB_I))
		tmp_int_contents=SUB_I
		swbreak
	case get_string@mnemonic(nm(MUL))
		tmp_int_contents=MUL
		swbreak
	case get_string@mnemonic(nm(DIV))
		tmp_int_contents=DIV
		swbreak
	case get_string@mnemonic(nm(MOD))
		tmp_int_contents=MOD
		swbreak
	case get_string@mnemonic(nm(MUL_I))
		tmp_int_contents=MUL_I
		swbreak
	case get_string@mnemonic(nm(DIV_I))
		tmp_int_contents=DIV_I
		swbreak
	case get_string@mnemonic(nm(MOD_I))
		tmp_int_contents=MOD_I
		swbreak
	case get_string@mnemonic(nm(AWAIT1))
		tmp_int_contents=AWAIT1
		swbreak
	case get_string@mnemonic(nm(AWAIT_X))
		tmp_int_contents=AWAIT_X
		swbreak
	case get_string@mnemonic(nm(WAIT_X))
		tmp_int_contents=WAIT_X
		swbreak
	case get_string@mnemonic(nm(DISP))
		tmp_int_contents=DISP
		swbreak
	swend
	tmp_p_number=0
	
	tmp_type    =int(tmp_type)
	tmp_p_number=int(tmp_p_number)
	
	type(    this_line)=tmp_type
;	type_b(  this_line)=tmp_type_b
	contents(this_line)=tmp_contents
	int_contents(this_line)=tmp_int_contents
	p_number(this_line)=tmp_p_number
	lines(this_line)=read_line+1
	
//	logmes ""+tmp_type
//	if tmp_type==1{
//		logmes ""+type_mnemonic+":type_mnemonic"
//	}
//	if tmp_type==4{
//			logmes ""+type_int+":type_int"
//	}
//	logmes tmp_contents
//	logmes ""+tmp_p_number
	
	tmp_p1_type=""
	tmp_p1_type_b=""
	tmp_p1_contents=""
	tmp_p1_p_number=""
	if l_u>=3{
		tmp_p1_type=u.youso_P1_begin
;		if tmp_p1_type=="type_mnemonic"{
;			tmp_p1_type=""+type_mnemonic+":type_mnemonic"
;		}
		if tmp_p1_type=="type_int"||tmp_p1_type=="int"{
			tmp_p1_type=type_int
;			tmp_p1_type_b=""+type_int+":type_int"
		}
		if tmp_p1_type=="type_int_address"||tmp_p1_type=="int_adr"{
			tmp_p1_type=type_int_address
;			logmes ""+type_int_address
		}
		if get_p_type@mnemonic(nm(tmp_int_contents))!=tmp_p1_type{
;		if tmp_p1_type==0{
			dialog "\error\ň^słB\nline:"+(read_line+1),1
		}
		
		tmp_p1_contents=u.youso_P1
		tmp_p1_int_contents=strtol(u.youso_P1)
		tmp_p1_p_number=1
		tmp_p1_type    =int(tmp_p1_type)
		tmp_p1_p_number=int(tmp_p1_p_number)
	
		this_line++
		
		type(    this_line)=tmp_p1_type
;		type_b(  this_line)=tmp_p1_type_b
		contents(this_line)=tmp_p1_contents
		int_contents(this_line)=tmp_p1_int_contents
		p_number(this_line)=tmp_p1_p_number
		lines(this_line)=read_line+1
		
//		logmes ""+tmp_p1_type
//		if tmp_p1_type==1{
//			logmes ""+type_mnemonic+":type_mnemonic"
//		}
//		if tmp_p1_type==4{
//			logmes ""+type_int+":type_int"
//		}
;		logmes    tmp_p1_type_b
//		logmes    tmp_p1_contents
//		logmes ""+tmp_p1_p_number
	}
	read_line++
	this_line++
loop

/////////////////////////////////////////////////////////////////////////////////
//	͂R[h\
/////////////////////////////////////////////////////////////////////////////////

screen 2
sdim _4analyzeMB,128*256
_4analyzeMB="code begin\n\n"
mesbox _4analyzeMB,640,480,1
ID4analyzeMB=stat
goto *@f
#deffunc add_analyze_MB str p_add
	_4analyzeMB+=p_add
	return
*@
	tmp_show_line=0
	str_line=""
	add_analyze_MB "line:"+notemax+"\n"
	repeat this_line
		if tmp_show_line!=lines(cnt){
			noteget str_line,lines(cnt)-1
			add_analyze_MB "\n"+strf("%4.4d:",lines(cnt))+str_line+"\n"
			tmp_show_line++
		}
//		logmes ""+type.cnt
		if type.cnt==type_mnemonic{
			add_analyze_MB ""+type_mnemonic+":type_mnemonic "+"\n"
		}
		if type.cnt==type_int{
			add_analyze_MB ""+type_int+":type_int \n"
		}
		if type.cnt==type_int_address{
			add_analyze_MB ""+type_int_address+":type_int_address \n"
		}
;		logmes ""+type_b.cnt
		if type.cnt==type_mnemonic{
			add_analyze_MB ""+int_contents.cnt+":"+get_string@mnemonic(nm(int_contents.cnt))+"\n"
		}
		if type.cnt==type_int{
			add_analyze_MB ""+int_contents.cnt+strf(" : %x",int_contents.cnt)+"\n"
	
		}
		if type.cnt==type_int_address{
			add_analyze_MB ""+int_contents.cnt+strf(" : %x",int_contents.cnt)+"\n"
	
		}
		add_analyze_MB "p_num:"+p_number.cnt+"\n"
		add_analyze_MB "line:"+lines(cnt)+"\n"
	loop
	add_analyze_MB "\ncode end\n"
	objprm ID4analyzeMB,_4analyzeMB
//dim  type    ,$10000
;sdim type_b  ,256,$10000
//sdim contents,256,$10000
//dim int_contents,$10000
//dim  p_number,$10000

////////////////////////////////////////////////////////////////////////////////
//	oCi\
///////////////////////////////////////////////////////////////////////////////
#define use_NM
#define bin_screen 3
#define bin_MB_y 20
#define bin_button_y 50

#include "subr_bin.hsp"

////////////////////////////////////////////////////////////////////////////////
//	R[hs
////////////////////////////////////////////////////////////////////////////////

#define execution_screen 1
	
#include "subr_execution.hsp"