wok diff runcom/stuff/debug.S @ rev 11820

Up: thunderbird to 10.0.2.
author Christopher Rogers <slaxemulator@gmail.com>
date Sat Feb 25 14:16:39 2012 -0500 (2012-02-25)
parents
children f32d3ba00836
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/runcom/stuff/debug.S	Sat Feb 25 14:16:39 2012 -0500
     1.3 @@ -0,0 +1,493 @@
     1.4 +// Usage:
     1.5 +//
     1.6 +// f DX:CX                 load one CHS sector to 0000:7C00
     1.7 +// t                       trace one step
     1.8 +// g <address>             go to adrs
     1.9 +// d <address>             display 16 bytes, CR for next 16 bytes...
    1.10 +// e <address> <words>...  enter memory byte/word/dword
    1.11 +// m <segment>             self move
    1.12 +// + <segment>             default segment offset
    1.13 +//
    1.14 +// Example:
    1.15 +// m 0FC0                  move debugger to 0FC0:0000 0FC0:01FF
    1.16 +// f 1			   read floppy boot sector to 0000:7C00
    1.17 +// f 80 1		   read hard disk master boot sector to 0000:7C00
    1.18 +// g 7C0E		   ...
    1.19 +
    1.20 +#define REGS32		28 bytes	display FS, GS and 32 bits datas for AX..DI 
    1.21 +#define ADJESDI		16 bytes	add segment overflow support for e and d
    1.22 +#define ASCIIDUMP	20 bytes	display hexa and ascii datas
    1.23 +#define INPUTBUFFER	 3 bytes	overload init code with a 32+ bytes input buffer
    1.24 +
    1.25 +.macro pushib val
    1.26 +	.byte	0x6A, \val-_start
    1.27 +.endm
    1.28 +
    1.29 +#ifdef REGS32
    1.30 +#define SEGREGSZ	10
    1.31 +#define REGSZ	32
    1.32 +#define USER_SP		REGSZ+SEGREGSZ-28(%bp)
    1.33 +#define FIXSP		14
    1.34 +#else
    1.35 +#define SEGREGSZ	6
    1.36 +#define REGSZ	16
    1.37 +#define USER_SP		REGSZ+SEGREGSZ-14(%bp)
    1.38 +#define FIXSP		10
    1.39 +#endif
    1.40 +#define USER_FLAGS	REGSZ+SEGREGSZ+4(%bp)
    1.41 +#define USER_FLAGS_HIGH	REGSZ+SEGREGSZ+5(%bp)
    1.42 +#define USER_IP		REGSZ+SEGREGSZ(%bp)
    1.43 +#define USER_CS		REGSZ+SEGREGSZ+2(%bp)
    1.44 +#define USER_CSIP	REGSZ+SEGREGSZ(%bp)
    1.45 +
    1.46 +#ifdef INPUTBUFFER
    1.47 +//#define ABS(x)		(x-(setvectors-_start))
    1.48 +#define ABS(x)		(x-32)
    1.49 +#else
    1.50 +#define ABS(x)		(x)
    1.51 +#endif
    1.52 +.macro initcode
    1.53 +	movw	$0x0FC0, %di	# move (and jump) to 0FC0:0000
    1.54 +	subw	$_startz-_start, USER_IP
    1.55 +	movw	USER_IP, %ax
    1.56 +	shrw	$4, %ax		# _start MUST be aligned on paragraph
    1.57 +	addw	USER_CS, %ax	# normalize %cs to have _start=0
    1.58 +	movw	%ax, %ds
    1.59 +.endm
    1.60 +
    1.61 +	.text
    1.62 +	.code16
    1.63 +	.org	0
    1.64 +
    1.65 +	.globl	_start
    1.66 +_start:
    1.67 +	pushf
    1.68 +	pushw	%cs
    1.69 +	stc
    1.70 +	call	init		# the start code will be overwritten by the input buffer
    1.71 +_startz:
    1.72 +
    1.73 +#ifdef INPUTBUFFER
    1.74 +isinit:
    1.75 +	initcode
    1.76 +	addw	$FIXSP, USER_SP	# adjust SP with [FLAGS CS IP DS ES [FS GS]] size
    1.77 +	pushib	setvectors
    1.78 +	jmp	moveself
    1.79 +#endif
    1.80 +
    1.81 +setvectors:
    1.82 +	xorw	%si, %si	# set interrupt vectors in 0 segment
    1.83 +	movw	%si, %ds
    1.84 +	movb	$0x7D, %cl	# skip nmi
    1.85 +hooklp:				# interrupts: 0=div0 1=step 2=nmi 3=brk 4=ov 5=bound 6=invalid
    1.86 +	pushw	%cs
    1.87 +	pushib	ABS(dbgstart)	# set %cs:dbgstart
    1.88 +	popl	(%si)		# to interrupt vector
    1.89 +skiphook:
    1.90 +	lodsl			# %si += 4
    1.91 +	shrb	$1,%cl
    1.92 +	jnc	skiphook
    1.93 +	jnz	hooklp		# note %cx will be cleared: SP will be untouched
    1.94 +	decw	(3-7)*4(%si)	# update int3 vector
    1.95 +	jmp	dbgstartz	# registers are already pushed by startup code
    1.96 +
    1.97 +regs:
    1.98 +	.ascii	"ss"
    1.99 +	.ascii	"di"  
   1.100 +	.ascii	"si"  
   1.101 +	.ascii	"bp"  
   1.102 +	.ascii	"sp"  
   1.103 +	.ascii	"bx"  
   1.104 +	.ascii	"dx"  
   1.105 +	.ascii	"cx"  
   1.106 +	.ascii	"ax"  
   1.107 +#ifdef REGS32
   1.108 +	.ascii	"gs"
   1.109 +	.ascii	"fs"
   1.110 +#endif
   1.111 +	.ascii	"es"
   1.112 +	.ascii	"ds"  
   1.113 +	.ascii	"ip"  
   1.114 +	.ascii	"cs"  
   1.115 +# Bit   Label    Desciption
   1.116 +# ---------------------------
   1.117 +# 0      CF      Carry flag
   1.118 +# 2      PF      Parity flag
   1.119 +# 4      AF      Auxiliary carry flag
   1.120 +# 6      ZF      Zero flag
   1.121 +# 7      SF      Sign flag
   1.122 +# 8      TF      Trap flag
   1.123 +# 9      IF      Interrupt enable flag
   1.124 +# 10     DF      Direction flag
   1.125 +# 11     OF      Overflow flag
   1.126 +#ifdef REGS32
   1.127 +	.ascii	"odi|sz|a|p|c"	# flags bits
   1.128 +#else
   1.129 +	.ascii	"oditsz?a?p c="	# flags bits
   1.130 +#endif
   1.131 +# 12-13  IOPL    I/O Priviledge level
   1.132 +# 14     NT      Nested task flag
   1.133 +# 16     RF      Resume flag
   1.134 +# 17     VM      Virtual 8086 mode flag
   1.135 +# 18     AC      Alignment check flag (486+)
   1.136 +# 19     VIF     Virutal interrupt flag
   1.137 +# 20     VIP     Virtual interrupt pending flag
   1.138 +# 21     ID      ID flag
   1.139 +
   1.140 +#ifdef INPUTBUFFER
   1.141 +ismove:
   1.142 +	pushw	%ax
   1.143 +moveself:
   1.144 +	popw	%si
   1.145 +#else
   1.146 +isinit:
   1.147 +	initcode
   1.148 +ismove:
   1.149 +	xorw	%si, %si
   1.150 +#endif
   1.151 +	movw	%di, %es	# move code to %di:0
   1.152 +	pushw	%di
   1.153 +#ifdef INPUTBUFFER
   1.154 +	xorw	%di, %di	# and jmp into (%di:setvectors) with retf
   1.155 +#else
   1.156 +	movw	$setvectors, %di	# and jmp into (%di:setvectors) with retf
   1.157 +	movw	%di, %si
   1.158 +#endif
   1.159 +	movw	$_end-setvectors, %cx
   1.160 +	pushw	%di
   1.161 +	rep	movsb
   1.162 +	retf
   1.163 +
   1.164 +int3:
   1.165 +	.byte	0x68		# push $0x086A	OV UP DI NT PL ZR - NA - PO - NC
   1.166 +# interrupt entry point: the registers [FLAGS CS IP] are already pushed
   1.167 +dbgstart:
   1.168 +	.byte	0x6A, 0x08	# push $0x08	NV UP DI NT PL NZ - NA - PO - NC
   1.169 +	popf
   1.170 +init:
   1.171 +	pushw	%ds
   1.172 +	pushw	%es
   1.173 +#ifdef REGS32
   1.174 +	pushw	%fs
   1.175 +	pushw	%gs
   1.176 +	pushal		#    [FLAGS CS IP DS ES FS GS] EAX ECX EDX EBX ESP EBP ESI EDI [SS]
   1.177 +#else
   1.178 +	pushaw		#    [FLAGS CS IP DS ES] AX CX DX BX SP BP SI DI [SS]
   1.179 +#endif
   1.180 +	pushw	%ss
   1.181 +	movw	%sp, %bp
   1.182 +#ifndef INPUTBUFFER
   1.183 +	pushf
   1.184 +	addw	$FIXSP, USER_SP	# adjust SP with [FLAGS CS IP DS ES [FS GS]] size
   1.185 +	popf
   1.186 +#endif
   1.187 +	jc	isinit
   1.188 +	jnz	notint3
   1.189 +	decw	USER_IP
   1.190 +	lesw	USER_CSIP, %di
   1.191 +#define OPCODE_BRK     0xCC
   1.192 +	.byte	0xB0		# movb $IM, %al
   1.193 +break:
   1.194 +	.byte	0xCC
   1.195 +	stosb
   1.196 +notint3:
   1.197 +#ifdef INPUTBUFFER
   1.198 +	addw	$FIXSP, USER_SP	# adjust SP with [FLAGS CS IP DS ES [FS GS]] size
   1.199 +#endif
   1.200 +dbgstartz:
   1.201 +dbgregslp:
   1.202 +	pushw	%cs
   1.203 +	popw	%ds
   1.204 +	movw	$ABS(regs), %si
   1.205 +#ifdef REGS32
   1.206 +	subw	%si, %bp
   1.207 +	movw	$15, %cx
   1.208 +#else
   1.209 +	movw	$13, %cx
   1.210 +#endif
   1.211 +regslp:
   1.212 +	call	putreg		# display register name and value
   1.213 +	loop	regslp
   1.214 +#ifdef REGS32
   1.215 +	movw	(%bp,%si), %dx	# get flags
   1.216 +#else
   1.217 +	movw	USER_FLAGS, %dx
   1.218 +	pushw	%si
   1.219 +	stc			# add trailing =
   1.220 +#endif
   1.221 +	movb	$13, %cl
   1.222 +	rcrw	%cl, %dx
   1.223 +nextbit:
   1.224 +	lodsb
   1.225 +	shlw	$1, %dx
   1.226 +#ifdef REGS32
   1.227 +	jnc	skipflag
   1.228 +	cmpb	$'|', %al	# remove system flags
   1.229 +	je	skipflag
   1.230 +	call	dbgputc
   1.231 +skipflag:
   1.232 +#else
   1.233 +	call	dbgputcbit	# display active flags bits
   1.234 +#endif
   1.235 +	loop	nextbit
   1.236 +#ifdef REGS32
   1.237 +	movw	%sp, %bp
   1.238 +#else
   1.239 +	popw	%si
   1.240 +	movb	$8, %cl
   1.241 +stacklp:
   1.242 +	lodsw			# si += 2
   1.243 +	call	putr16		# display flags and the beginning of the stack
   1.244 +	loop	stacklp
   1.245 +#endif
   1.246 +	call	getline
   1.247 +	lodsb
   1.248 +	xchgw	%ax, %di
   1.249 +	call	getval
   1.250 +	.byte	0x81, 0xC3	# addw	$0, %bx
   1.251 +offset_value:
   1.252 +	.word	0
   1.253 +	movw	%bx, %es
   1.254 +	xchgw	%ax, %di
   1.255 +	subb	$'m', %al
   1.256 +	je	ismove
   1.257 +	subb	$'+'-'m', %al
   1.258 +	jne	not_offset
   1.259 +	movw	%di, ABS(offset_value)
   1.260 +not_offset:
   1.261 +	orb	$1, USER_FLAGS_HIGH	# set TF
   1.262 +	subb	$'t'-'+', %al
   1.263 +	je	done
   1.264 +	subb	$'d'-'t', %al
   1.265 +	xchgw	%ax, %cx
   1.266 +	jcxz	dump		# 'd' ?
   1.267 +	loop	noenter		# 'e' ?
   1.268 +nextval:
   1.269 +	call	getval
   1.270 +	jcxz	dbgregslp
   1.271 +	xchgb	%dl, %dh
   1.272 +mextmsb:
   1.273 +	stosb
   1.274 +	xchgw	%ax, %dx
   1.275 +	xchgb	%al, %dh
   1.276 +#ifdef ADJESDI
   1.277 +	call	adjustESDI
   1.278 +#endif
   1.279 +	decw	%cx
   1.280 +	loopne	mextmsb
   1.281 +	jmp	nextval
   1.282 +noenter:
   1.283 +	loop	not_floppy_load	# f DX:CX ?
   1.284 +	movw	%es, %dx
   1.285 +	movw	%cx, %es
   1.286 +	movw	%di, %cx
   1.287 +	movw	$0x0201, %ax
   1.288 +	movw	$0x7C00, %bx
   1.289 +	pushw	%bx
   1.290 +	int	$0x13
   1.291 +	popw	%di
   1.292 +godbgregslpifc:	
   1.293 +	jc	dbgregslp
   1.294 +dump:
   1.295 +	movw	%es, %ax
   1.296 +	call	putax
   1.297 +	movw	%di, %ax
   1.298 +	call	putax
   1.299 +	movw	$16, %cx
   1.300 +dhex:
   1.301 +	movb	%es:(%di), %ah
   1.302 +#ifdef ASCIIDUMP
   1.303 +	movb	%ah, (%si)
   1.304 +	incw	%si
   1.305 +#endif
   1.306 +#ifdef ADJESDI
   1.307 +	call	incESDI
   1.308 +#else
   1.309 +	incw	%di
   1.310 +#endif
   1.311 +#ifdef REGS32
   1.312 +	movb	$0x30, %dh	# the data has 2 digits
   1.313 +#else
   1.314 +	movb	$0x01, %dh	# the data has 2 digits
   1.315 +#endif
   1.316 +	call	putx
   1.317 +	loop	dhex
   1.318 +#ifdef ASCIIDUMP
   1.319 +	movb	$16, %cl
   1.320 +	subw	%cx, %si
   1.321 +dascii:
   1.322 +	lodsb
   1.323 +	cmpb	$0x7F, %al
   1.324 +	jnc	skipascii
   1.325 +	cmpb	$0x20, %al
   1.326 +	cmc
   1.327 +skipascii:
   1.328 +	call	dbgputcbit
   1.329 +	loop	dascii
   1.330 +#endif
   1.331 +	call	dbgputcr
   1.332 +	int	$0x16
   1.333 +	cmpb	$13, %al
   1.334 +	je	dump
   1.335 +notdump:
   1.336 +not_floppy_load:
   1.337 +	stc
   1.338 +	loop	godbgregslpifc	# g ?
   1.339 +isgo:
   1.340 +	andb	$0xfe, USER_FLAGS_HIGH	# clear TF
   1.341 +	xchgw	%ax, %cx
   1.342 +	jcxz	done
   1.343 +setbreak:
   1.344 +	movb	$OPCODE_BRK, %al
   1.345 +	xchgb	%al, %es:(%di)
   1.346 +	movb	%al, ABS(break)
   1.347 +done:
   1.348 +	popw	%ax  	# %ss
   1.349 +#ifdef REGS32
   1.350 +	popal
   1.351 +	popw	%gs
   1.352 +	popw	%fs
   1.353 +#else
   1.354 +	popaw
   1.355 +#endif
   1.356 +	popw	%es
   1.357 +	popw	%ds
   1.358 +	iret
   1.359 +
   1.360 +#ifdef ADJESDI
   1.361 +adjustESDI:
   1.362 +	decw	%di
   1.363 +incESDI:
   1.364 +	incw	%di
   1.365 +	jnz	esok
   1.366 +	pushw	%es
   1.367 +	addb	$0x10,-3(%bp)
   1.368 +	popw	%es
   1.369 +esok:
   1.370 +	ret
   1.371 +#endif
   1.372 +
   1.373 +putreg:
   1.374 +	call	dbgput2c
   1.375 +	movb	$'=', %al
   1.376 +	call	dbgputc
   1.377 +putr16:
   1.378 +#ifdef REGS32
   1.379 +	movl	-2(%bp,%si), %eax
   1.380 +	movw	$0x3FC0, %dx	# check bits 7..14
   1.381 +	shrw	%cl, %dx
   1.382 +putax:
   1.383 +	movb	$0xF0, %dh	# the data has 4 digits
   1.384 +	jnc	putx		# 16 bits register ?
   1.385 +	incw	%bp		# a 32 bits register, not 16 bits
   1.386 +	incw	%bp
   1.387 +	movb	$0xFF, %dh	# the data has 8 digits
   1.388 +	jmp	putx
   1.389 +putxlp:  
   1.390 +#else
   1.391 +#	movw	_start-ABS(regs)-2(%bp,%si), %ax
   1.392 +	.byte	0x8b, 0x42, _start-ABS(regs)-2
   1.393 +putax:
   1.394 +	movb	$0x07, %dh	# the data has 4 digits
   1.395 +putx:
   1.396 +putxlp:  
   1.397 +	rolw	$4, %ax
   1.398 +#endif
   1.399 +	pushw	%ax
   1.400 +	andb	$0xf, %al
   1.401 +	addb	$0x90, %al
   1.402 +	daa
   1.403 +	adcb	$0x40, %al
   1.404 +	daa
   1.405 +	call	dbgputc
   1.406 +	popw	%ax
   1.407 +#ifdef REGS32
   1.408 +putx:
   1.409 +	roll	$4, %eax
   1.410 +#endif
   1.411 +	shrb	$1, %dh
   1.412 +	jc	putxlp
   1.413 +#ifdef REGS32
   1.414 +	jnz	putx
   1.415 +#endif
   1.416 +dbgputcbit:
   1.417 +	jc	dbgputc  
   1.418 +	mov	$0x20, %al
   1.419 +dbgputc:
   1.420 +	movw	$7, %bx
   1.421 +	mov	$0xE, %ah
   1.422 +	int	$0x10
   1.423 +	xchgw	%ax, %bx
   1.424 +#	clc				# for putax
   1.425 +	ret
   1.426 +
   1.427 +# get value in DX:AX, BX is segment CX is digits count.
   1.428 +getval:
   1.429 +	xorw	%ax, %ax
   1.430 +	xorw	%bx, %bx
   1.431 +	xorw	%cx, %cx
   1.432 +getvalz:
   1.433 +	xchgw	%ax, %bx
   1.434 +	cwd
   1.435 +	decw	%cx
   1.436 +isx:
   1.437 +	shll	$4, %edx
   1.438 +	orb	%al, %dl
   1.439 +	incw	%cx
   1.440 +gotspc:
   1.441 +getvallp:
   1.442 +	lodsb
   1.443 +	cmpb	$0x20, %al		# heat heading spaces
   1.444 +	jne	notspc
   1.445 +	jcxz	gotspc
   1.446 +notspc:
   1.447 +	subb	$'0', %al
   1.448 +	cmpb	$10, %al		# in 0..9 ?
   1.449 +	jb	isx
   1.450 +	subb	$'a'-'0'-10, %al
   1.451 +	cmpb	$16, %al		# in a..f ?
   1.452 +	jb	isx
   1.453 +	cmpb	$':'-'a'+10, %al
   1.454 +	pushl	%edx
   1.455 +	popw	%ax
   1.456 +	popw	%dx
   1.457 +	je	getvalz			# store segment in %bx
   1.458 +	pushw	%dx
   1.459 +	shlw	$12, %dx
   1.460 +	orw	%dx, %bx
   1.461 +	popw	%dx
   1.462 +	ret
   1.463 +
   1.464 +getline:
   1.465 +	call	dbgputcr
   1.466 +getlinebs:
   1.467 +	cmpw	$ABS(buffer), %si
   1.468 +	je	getc
   1.469 +	decw	%si
   1.470 +getlinelp:
   1.471 +	call	dbgputc
   1.472 +getc:
   1.473 +	int	$0x16
   1.474 +	cmpb	$8, %al
   1.475 +	je	getlinebs
   1.476 +	orb	$0x20, %al
   1.477 +	movb	%al, (%si)
   1.478 +	inc	%si
   1.479 +	cmpb	$0x2D, %al
   1.480 +	jne	getlinelp
   1.481 +dbgputcr:
   1.482 +	movw	$ABS(crlf), %si
   1.483 +dbgput2c:
   1.484 +	call	dbgput1c
   1.485 +dbgput1c:
   1.486 +	lodsb
   1.487 +	jmp	dbgputc
   1.488 +
   1.489 +crlf:
   1.490 +	.byte	13,10
   1.491 +_end:
   1.492 +buffer:
   1.493 +
   1.494 +	.org	510
   1.495 +	.byte	0x55, 0xAA
   1.496 +