
;---------------------------------------------------------------------
;		ＧＰＰレジスタ定義
;---------------------------------------------------------------------
Data_RG		rgs	0
PlotX_RG	rgs	1
PlotY_RG	rgs	2
;		rgs	3
MultAnsLow_RG	rgs	4
;		rgs	5
Mult_RG		rgs	6
MergeHigh_RG	rgs	7
MergeLow_RG	rgs	8
;		rgs	9
;		rgs	10
LinkAddress_RG	rgs	11
LoopCount_RG	rgs	12
LoopAddress_RG	rgs	13
RomAddress_RG	rgs	14
PrgAddress_RG	rgs	15


;---------------------------------------------------------------------
;		ＣＲＡＭ上に１６色キャラクタデータを展開
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;		入力：
;		R0 <= データバンク
;		R1 <= データアドレス
;		R3 <= 展開ライン数 ( ドット単位 1 - 512 )
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;		破壊：
;		ＲＯＭバンクレジスタ
;		プロットオプションレジスタ
;		ＣＲＡＭ 0000h - 007Fh
;		ＣＲＡＭ スクリーンベース
;---------------------------------------------------------------------
DEF_CHR

Ymax_RG		rgs	3
Flag_RG		rgs	4
Temp_RG		rgs	4
Color_RG	rgs	5
Palet10_RG	rgs	6
Palet32_RG	rgs	7
Palet54_RG	rgs	8
Palet76_RG	rgs	9
BitCount_RG	rgs	10
Cmask_RG	rgs	11


		romb
		move	RomAddress_RG,r1

		ibt	Data_RG,#10001b
		cmode

		ibt	PlotY_RG,#0
		ibt	Cmask_RG,#15

;		パレットの初期化
;---------------------------------------
		iwt	Temp_RG,#00F0Fh

		getb
		add	Data_RG
		add	Data_RG
		add	Data_RG
		to	Palet10_RG
		add	Data_RG

		getb
		inc	RomAddress_RG
		or	Palet10_RG

		to	Palet10_RG
		and	Temp_RG

;- - - - - - - - - - - - - - - - - - - -
		getb
		add	Data_RG
		add	Data_RG
		add	Data_RG
		to	Palet32_RG
		add	Data_RG

		getb
		inc	RomAddress_RG
		or	Palet32_RG

		to	Palet32_RG
		and	Temp_RG

;- - - - - - - - - - - - - - - - - - - -
		getb
		add	Data_RG
		add	Data_RG
		add	Data_RG
		to	Palet54_RG
		add	Data_RG

		getb
		inc	RomAddress_RG
		or	Palet54_RG

		to	Palet54_RG
		and	Temp_RG

;- - - - - - - - - - - - - - - - - - - -
		getb

		to	Palet76_RG
		and	Temp_RG

;		プロットバッファの初期化
;---------------------------------------
		sub	Data_RG
		ibt	PlotX_RG,#0
		ibt	LoopCount_RG,#64

		cache

		move	LoopAddress_RG,PrgAddress_RG

		stw	(PlotX_RG)
		inc	PlotX_RG
		loop
		inc	PlotX_RG

		dec	PlotX_RG	; <= 127

;		ビットデータの初期化
;---------------------------------------
		ibt	BitCount_RG,#5

		getb
		inc	RomAddress_RG
		lsr
		lsr
		lsr
		lsr
		bra	READ_COUNT
		db	0ACh		;ibt	LoopCount_RG,#0

;		区間処理フラグ読み込み
;- - - - - - - - - - - - - - - - - - - - - - - - - - - -
FLAG_X1
		bne	Read$
		lsr

		ibt	BitCount_RG,#8
		getb
		inc	RomAddress_RG
		lsr
Read$
		move	Temp_RG,Data_RG			;ビットデータ退避

		to	Color_RG
		ldb	(PlotX_RG)

		bcs	FLAG_11
		dec	PlotX_RG

;		X ドット区間を伸ばす
;-------------------------------------------------------
FLAG_01
		ldb	(PlotX_RG)
		cmp	Color_RG
		beq	FLAG_01
		dec	PlotX_RG
		inc	PlotX_RG

		move	LoopAddress_RG,PrgAddress_RG

		from	Color_RG
		stb	(PlotX_RG)
		loop
		dec	PlotX_RG

		bpl	SET_GOMA
		alt1
		nop

		bra	PLOT_BUFF2
		inc	PlotX_RG	;<= 0

;		X ドット区間を縮ます
;-------------------------------------------------------
Zero11
		and	r1
		bra	Loop11
		sub	Data_RG
FLAG_11
		bmi	Zero11
		ldb	(PlotX_RG)
		cmp	Color_RG
		beq	FLAG_11
		dec	PlotX_RG
		inc	PlotX_RG
Loop11
		with	PlotX_RG
		add	LoopCount_RG

;		ゴマを置く
;- - - - - - - - - - - - - - - - - - - - - - - - - - - -
		alt1
SET_GOMA
		stw	(PlotX_RG)
		move	Data_RG,Temp_RG

		db	0ACh		;ibt	LoopCount_RG,#0

;		カウント読み込み
;-------------------------------------------------------
READ_COUNT
		db	0
		ibt	Flag_RG,#1
		bra	ReadF$
		dec	BitCount_RG

;		有効ビット読み込み
;- - - - - - - - - - - - - - - - - - - -
ReadX$
		dec	BitCount_RG
		bne	Readn$
		lsr

		ibt	BitCount_RG,#8
		getb
		inc	RomAddress_RG
		lsr
Readn$
		bcc	NextBit$

		with	LoopCount_RG
		or	Flag_RG

;		次のビット
;- - - - - - - - - - - - - - - - - - - -
NextBit$
		with	Flag_RG
		add	Flag_RG

;		次のビットが有効か判断
;- - - - - - - - - - - - - - - - - - - -
		dec	BitCount_RG
ReadF$
		bne	Read$
		lsr

		ibt	BitCount_RG,#8
		getb
		inc	RomAddress_RG
		lsr
Read$
		bcs	ReadX$

;		カウント決定
;- - - - - - - - - - - - - - - - - - - -
SetCount$
		with	LoopCount_RG
		or	Flag_RG

;		区間処理フラグ読み込み
;-------------------------------------------------------
READ_FLAG
		dec	BitCount_RG
		bne	Read$
		lsr

		ibt	BitCount_RG,#8
		getb
		inc	RomAddress_RG
		lsr
Read$
		bcs	FLAG_X1
		dec	BitCount_RG
FLAG_X0
		bne	Read$
		lsr

		ibt	BitCount_RG,#8
		getb
		inc	RomAddress_RG
		lsr
Read$
		bcs	FLAG_10

;		X 個の区間をそのままにする
;-------------------------------------------------------
FLAG_00
		move	Temp_RG,Data_RG

		move	LoopAddress_RG,PrgAddress_RG

		to	Color_RG
		ldb	(PlotX_RG)
		dec	PlotX_RG
Loop$
		bmi	PLOT_BUFF1
		ldb	(PlotX_RG)
		cmp	Color_RG
		beq	Loop$
		dec	PlotX_RG

		loop
		inc	PlotX_RG

		move	Data_RG,Temp_RG

		bra	READ_COUNT
		db	0ACh		;ibt	LoopCount_RG,#0

;		ＲＡＭのデータをプロット
;---------------------------------------------------------------------
PLOT_BUFF0
		to	Temp_RG		;move
PLOT_BUFF1
		inc	PlotX_RG	;<= 0
PLOT_BUFF2
		iwt	LoopCount_RG,#128
		move	LoopAddress_RG,PrgAddress_RG

		ldb	(PlotX_RG)
		color
		loop
		plot

		inc	PlotY_RG
		from	PlotY_RG
		cmp	Ymax_RG
		bcc	NextLine$
		dec	PlotX_RG	; <= 127

		rpix
		stop
		nop
NextLine$
		move	Data_RG,Temp_RG		;ビットデータ復帰
		bra	READ_COUNT
		db	0ACh		;ibt	LoopCount_RG,#0

;		パレット１３５の色
;---------------------------------------
PALET_High
		to	Color_RG
		swap
		with	Color_RG
		and	Cmask_RG

;		色をセット
;---------------------------------------
SET_COLOR
		with	PrgAddress_RG
SET_COLOR1
		to	LoopAddress_RG

		from	Color_RG
		stb	(PlotX_RG)
		loop
		dec	PlotX_RG

		bpl	READ_COUNT
		ibt	LoopCount_RG,#0

		bra	PLOT_BUFF0
		with	Data_RG		;move

;		新区間作成
;-------------------------------------------------------
FLAG_10
		dec	BitCount_RG
		bne	Read$
		lsr

		ibt	BitCount_RG,#8
		getb
		inc	RomAddress_RG
		lsr
Read$
		bcs	PALET_1XX
		dec	BitCount_RG
PALET_0XX
		bne	Read$
		lsr

		ibt	BitCount_RG,#8
		getb
		inc	RomAddress_RG
		lsr
Read$
		bcs	PALET_01X
		dec	BitCount_RG
PALET_00X
		bne	Read$
		lsr

		ibt	BitCount_RG,#8
		getb
		inc	RomAddress_RG
		lsr
Read$
		bcs	PALET_High
		from	Palet10_RG

;		パレット０の色
;---------------------------------------
PALET_000
		to	Color_RG
		bra	SET_COLOR
		and	Cmask_RG

;		パレット読み込み
;- - - - - - - - - - - - - - - - - - - -
PALET_1XX
		bne	Read$
		lsr

		ibt	BitCount_RG,#8
		getb
		inc	RomAddress_RG
		lsr
Read$
		bcs	PALET_11X
		dec	BitCount_RG
PALET_10X
		bne	Read$
		lsr

		ibt	BitCount_RG,#8
		getb
		inc	RomAddress_RG
		lsr
Read$
		bcs	PALET_High
		from	Palet54_RG

;		パレット４の色
;---------------------------------------
PALET_100
		to	Color_RG
		bra	SET_COLOR
		and	Cmask_RG

;		パレット読み込み
;- - - - - - - - - - - - - - - - - - - -
PALET_01X
		bne	Read$
		lsr

		ibt	BitCount_RG,#8
		getb
		inc	RomAddress_RG
		lsr
Read$
		bcs	PALET_High
		from	Palet32_RG

;		パレット２の色
;---------------------------------------
PALET_010
		to	Color_RG
		bra	SET_COLOR
		and	Cmask_RG

;		パレット読み込み
;- - - - - - - - - - - - - - - - - - - -
PALET_11X
		bne	Read$
		lsr

		ibt	BitCount_RG,#8
		getb
		inc	RomAddress_RG
		lsr
Read$
		bcs	PALET_111
		from	Palet76_RG

;		パレット６の色
;---------------------------------------
PALET_110
		to	Color_RG
		bra	SET_COLOR
		and	Cmask_RG

;		新しい色
;---------------------------------------
PALET_111
		ibt	Color_RG,#0

		dec	BitCount_RG
		bne	c8$
		lsr

		ibt	BitCount_RG,#8
		getb
		inc	RomAddress_RG
		lsr
c8$
		with	Color_RG
		rol

		dec	BitCount_RG
		bne	c4$
		lsr

		ibt	BitCount_RG,#8
		getb
		inc	RomAddress_RG
		lsr
c4$
		with	Color_RG
		rol

		dec	BitCount_RG
		bne	c2$
		lsr

		ibt	BitCount_RG,#8
		getb
		inc	RomAddress_RG
		lsr
c2$
		with	Color_RG
		rol

		dec	BitCount_RG
		bne	c1$
		lsr

		ibt	BitCount_RG,#8
		getb
		inc	RomAddress_RG
		lsr
c1$
		with	Color_RG
		rol
		iwt	PrgAddress_RG,#SET_COLOR1
		with	PrgAddress_RG

;=====================================================================
		end

