;#define _8MHZ_					; defined if it is an 8MHZ board
#define _4MHZ_					; defined if it is a 4MHZ board
;#define _4800_					; defined if 4800 baud is desired rather than 9600(default)
#define _CHECKSUM_				; defined if you want to use checksums when loading
;#define _TESTING_ 				; if this is defined, the board is being tested
						; and the Vector information is already on the
						; board so it does not need to be loaded
#define _STAR_ 					; if this is defined, the board will print out *s when loading
						; a program
; 
; 6812 monitor 2.21
;
; Feb 10, 2004  (v2.25) Jate Sujjavanich made some useful changes to help, MD, and splash
;		changed MD to accept single argument
;		- When generating the EPROM file, objcopy may be used.  This keeps the
;		  RAM pseudovectors available though, so they must be deleted when generating
;		  the bin file
; May 28, 2003	(v2.2) Eric Schwartz updated serial file and main program
;		-replaced zmb's with ds.b's
;		-replaced dw's with ds.w's
;		
; May 28, 2003	(v2.1) Robert Hill fixed some serial communication problems
;
; Sept 06, 2002 (v2.0) Robert Oaks takes over at this point. (RWO)
;               
; 		-added backspace capability that works and won't go past the buffer
;		-took out the annoying extra address with the "md" command.
;		-improved the "go"command by clearing registers
;		-made it so a command can be repeated by pressing enter
;			as long as nothing else was pressed. useful with "md"
;		-added a stepping routine with hardware breakpoints
;		-fixed bug that didn't let the program work with MiniIDE's
;			terminal connection
;		-commented everything thoroughly
;		-added checksums to the loading process and the ability
;			to print out stars after each record
; Mar 04, 2002 (v1.9.2) released the version 1.9.1 experimental changes
;              onto the world.
; Feb 20, 2002 (v1.9.1) EXPERIMENTAL VERSION
;              Changed the download subroutine to handle S0 (header)
;              records.
; Feb 13, 2002 (v1.9) Turned off clock stretching by changing the MISC
;              register. It should be noted that the "old" commented
;              way of turning it off was the same. We had a failure of
;              communication. Also cleaned up the code a little. (The
;              old method used the MODE register. It doesn't work. The
;              MODE register ESTR bit can't be changed to turn off
;              clock stretching. You must use MISC to set zero 
;              stretches. 
;              I also made it so that the PEAR is read and OR'd 
;              rather than simply set.
; Jan 28, 2002 (v1.8) Need to change the pseudo-interrupt vector table
;              to be like whats in the 6812B32 manual. (Correct, that is.)
; Dec 24, 2001 Fixed the bug in the resume function. Stack ptr
;              could keep decrementing if RES is called (via jsr)
;              and the user's SWIs keep happening (which doesn't
;              reset the monitor's SP.)
;              I used pre-processor commands to conditionally
;              compile all the debugging stuff. The bad thing 
;              is that ASM12 doesn't have a -D command-line
;              option! D'oh!
; Dec 26, 2001 I think I fixed all of the interrupt vectors and fake
;              user vectors for the 68HC12B32. It works, anyhow.
;
;
; Dec 23, 2001 (v1.7) All the breakpoint stuff is back! Works, too.
;              Ummm ... not much more than that. Yes, I skipped 
;              v1.6 because there have been so many changes since 
;              v1.5. Remember that breakpoints only work with
;              code in RAM. See the notes below about doing code
;              in flash.
;              Still some test code at the end that can be removed
;              if necessary.
; Dec 22, 2001 (v1.5) Yes, I threatened to release the code with
;              a disassembler as v1.5 so here it is. The 
;              disassembler code is in disasm.asm which is included
;              herein using a directive at the bottom. I'm not quite
;              sure which opcodes it'll disassemble but I don't care 
;              to find out just yet. v1.5 is now the release version.
;              Now to add the breakpoint stuff back in ...
; Dec 22, 2001 (v1.6.1) Putting breakpoint stuff back. EXPERIMENTAL.
; Dec 18, 2001 (no version change) The command-line is working now.
;              change the version if any new options are added. ok?
;              Removed the breakpoint code because of continuing
;              debate over its usefulness. We don't know if using
;              SWIs is a good idea.
; Dec 18, 2001 (v1.4.1) This is an EXPERIMENTAL version for trying
;              to write a damn disassembler. If successful, it'll
;              become v1.5 (or higher.) v1.4 is still the "release"
;              version!
; Dec 16, 2001 (v1.4) I'm changing the way the user interface works 
;              since I'm out of menu numbers (see v1.3 source.) It'll
;              now work mostly like the old 6811 Buffalo did. Must 
;              give them proper names :)
; Dec 10, 2001 (v1.3) added breakpoint stuff while in a motel in 
;              Tallahassee after visiting historic Doak Campbell 
;              Stadium at FSU. I'm not impressed. Their campus is 
;              so dirty. The inside of all their buildings looks like 
;              the basements of ours. Doak is big, tho. Seems bigger
;              than Ben Hill at UF but maybe I was imagining things.
; Mar 21, 2001 updated (works fully) (v1.2?)
; Feb 12, 2001 (v1.0?? v1.1??);
;
;
; Author   : Patrick O'Malley (POM) (pomalley@mil.ufl.edu)
;	   : Robert Oaks (RWO) (x@solidstasis.com)
; Place    : University of Florida/Machine Intelligence Lab
;            (code was written entirely on the author's laptop, however)
; Copyright: This code is freely distributed to the public under
;            the GNU Public License (GPL). It may be modified and
;            distributed (freely) as long as the original author's
;            (and anyone else's who modifies the code) name 
;            remains attached. And anyone may distribute a 
;            binary of this code as long as due credit is given
;            the author(s).
;            This author (POM) places another stipulation that
;            the notes section _must_ be distributed with the
;            source -- no removing it. Period. 
;
; This was written for the EEL4744 course which is/was
; changed over to the 6812 from the 6811. Yeah, a dumb
; change but whatever -- it wasn't my decision. (POM)
;
;
; Before anyone asks (and they will) the S19 download routine
; will NOT dump code into EEPROM or FLASH! Got that? Firstly, 
; putting it into flash would be dumb and necessitates a 12v
; external "programming voltage". Secondly, for debugging, one
; should dump their code to RAM, run it and debug it there. Thats
; fast and easy. Anything else would be rather dumb. Oh, and I don't
; know how much EEPROM the thing has so I'm not going to write tons
; of code just to find out that it has 256 bytes that you can't run
; code out of anyhow! (POM)
;
; You know whats funny? I wasn't aware of some ops when I wrote this!
; Like "sts" -- had no idea! I had been using "tsx" then storing X!
; Thats like a RISC programmer to do something like that :) (POM)
;
; 
;
; -------- MISC EQUATES --------

PEAR			equ	$000A	; Port-E Assignment Register
MODE			equ	$000B
MISC			equ	$0013	
test_start_point	equ	$4000	; where the program starts if in testing mode


#include "i_vects.asm"

; ----------- MONITOR VARIABLES -------------
tempw			dc.w		$0000		; temporary variable
tempw2			dc.w		$0000		; temporary variable

tempb			ds.b		1		; temporary variable

							; see "m_go.asm"
hw_step_byte		ds.b		1		; byte used in steptracing
go_jmp			dc.b		$6		; instruction for a jump command
go_addr			ds.w		1		; word used in the go command as address

							; see "m_load.asm"
star_count_register	ds.b		1		; how many records are loaded before printing a star


bkpt_table		ds.b		10		; the addresses of the breakpoints
bkpt_tbl_code		ds.b		5		; the code that used to be where the SWIs are now
SP_save			dc.w		user_stack_top	; a place to save the user's SP when SWI occurs
SP_save_mon		dc.w		$0000		; a place to save the monitor's SP (why??)
cmd_buffer		ds.b		cmd_buffer_size	; where the user's command is put!
cmd_buffer_size		equ		20		; I didn't know where to put this!

bkpt_state		dc.b		$00		; state of the bkpt code (see SWI handler)
bkpt_addr		dc.w		$0000		; where to put the bkpt back
bkpt_addr2		dc.w		$0000		; where the "second" bkpt is (for break - )
bkpt_code		dc.b		$00		; code to put back where the second SWI is




#ifdef	__DEBUG__
ram_code_test		zmb		10

end_of_ram		equ		*
#endif

; the monitor's stack is 32 bytes (should be enough)
; and is located here, at the top of the internal RAM.
; user stack can be just below it. user_stack_top
; is here just for reference! its never used specifically!

			org	$0bff
mon_stack_top		equ	*

			org	$0bdf
user_stack_top		equ	*

	
; -----=-=-=-= MONITOR RESET ENTRY =-=-=-=-----
; RESET ENTERS HERE!

#ifndef _TESTING_				; If this is being programmed to an EPROM
						; it should begin at $E000
	ORG		start_point		; start_point is found in "i_vects.asm"
						; because it needs to be put into the vector table
						; in addition to having the code start here
#endif


#ifdef _TESTING_				; Otherwise, it should begin in testing space.
						; My testing space begins at $4000
	ORG		test_start_point	; test_start_point is defined with an equ at the top
						; of this document
#endif

						; initialization of monitor things

	lds		#mon_stack_top		; setup the stack pointer
	jsr		cop_off			; turn off the COP reset gen
	jsr		init_serial_tx_rx	; start the serial system, duh.

						; since SP_save is in RAM and we don't control that, lets
						; make sure its OK by putting the address of user_stack_top 
						; into SP_save
	


	ldx		#user_stack_top
	stx		SP_save

	ldaa		#6
	staa		go_jmp

	ldaa		#star_count
	staa		star_count_register


						; clear the bkpt state (just in case)
	clra
	staa		bkpt_state	

						; clear the bkpt table
	ldaa		#10			; loads counter with number of bytes in table
	ldx		#bkpt_table		; load address of table in x
	clrb					; clears b
b_loop2:
	stab		0,x			; stores value in b( $00 ) to the address in x
	inx					; increments x
	deca					; decrements counter
	bne		b_loop2			; loops again until counter is 0
		


; the following code turns off the clock stretching the right way
; v1.9 code change
	ldaa		MISC
	anda		#%11110011
	staa		MISC

	; the next two lines enable RAM R/W
	; or it won't work!
	ldaa		PEAR
	oraa		#%00000100		; enable R/W
	staa		PEAR

#ifdef 	__DEBUG__				; this is pretty weird. this isn't used
	; need to move test code into RAM to test bkpt stuff
	ldab		#(testcode - tc_end)
	ldx		#testcode
	ldy		#ram_code_test
b_loop1:
	ldaa		0,x
	staa		0,y
	inx
	iny
	decb
	bne		b_loop1	
#endif

; print the splash screen and the menu
begin:
	jsr		print_splash		; print the splash once and only once!

begin_1:
	ldy		#newline		; loads the address of the newline string
	jsr		print_string		; prints the string to the console

						; print the prompt
	ldaa		#'>'			; loads '>'
	jsr		put_char		; and prints it to the console
	ldaa		#' '			; loads a space
	jsr		put_char		; and prints it to the console

						; use get_line to get a line from the user
	ldx		#cmd_buffer		; first loads the address of the command buffer
	ldab		#20			; B holds the max # of chars a user can enter
	jsr		get_line		; calls get line, which gets a whole line of user input
	jsr		parse_command_line	; parse the command line. returns addr of sub in X, returns addr of 
						; end of cmd string in cmd buffer in Y
	iny					; actually we need to inc Y to get past the end of the cmd buffer
	cpx		#cmd_buffer		; if there was an error, X will be #cmd_buffer	
	beq		begin_1			; if error, just go back to prompt

	jsr		0,x			; otherwise jump to the sub
	bra		begin_1			; and return to beginning


#include "messages.asm"
#include "prompt.asm"

; -----------------=-=-=-= SUBROUTINES =-=-=-=----------------------

#include "parse.asm"		; stuff to parse a menu command
#include "swi.asm"		; stuff for handling software interupts
#include "m_dis.asm"		; menu dissassembler command
#include "m_view.asm"		; menu memory view command
#include "m_go.asm"		; menu go command
#include "m_res.asm"		; menu resume command
#include "m_editm.asm"		; menu modify memory command
#include "m_bf.asm"		; menu block fill command
#include "m_breakp.asm"		; menu add/remove breakpoint command
#include "m_rm.asm"		; menu register modify command
#include "m_rv.asm"		; menu register view command
#include "m_load.asm"		; menu load program command

#include "m_help.asm"		; menu help command
#include "m_bptbl.asm"		; menu print breakpoint table command
#include "m_debug.asm"		; menu debug command
#include "util.asm"		; utility functions defined
#include "m_step.asm"		; utility to steptrace

#ifdef	__DEBUG__
; ----- TEST CODE! -----
; tests SWI and disassembler
; remove in production
testcode:
	ldab		#20
tc_loop1:
	swi
	ldy		#error_msg
	jsr		print_string
	decb		
	bne		tc_loop1
tc_end	equ		*
#endif

#include "disasm.asm"
#include "serial.asm"
#include "cop.asm"

#include "sample.asm"		; sample command used for demonstrating how to add a command to the prompt
