#include <stdio.h>
#include "defs.h"
#include "externs.h"

/* printline prints the contents of prlnbuf */

println()
{
	if (!xlist || !clist || (expand_macro && !mlist))
		return;
	fprintf(lptr, "%s\n", prlnbuf);
}

/* clearline clear prlnbuf */

clearln()
{
	int i;

	for (i = 0; i < SFIELD; i++)
		prlnbuf[i] = ' ';
	prlnbuf[i] = 0;
}

/* error printing routine */

error(char *stptr)
{
	int i, temp;

        if (switch_throwback == 1) {
            switch_throwback++;
            DDEUtils_ThrowbackStart();
        }

	/*  Put the source line number into prlnbuf. */

	i = 4;
	temp = slnum;
	while (temp != 0) {
		prlnbuf[i--] = temp % 10 + '0';
		temp /= 10;
	}

	/*  Update the name of the current file. */

	if (infile_error != infile_num) {
	        if (switch_throwback) DDEUtils_ThrowbackSend_Processing(input_file[infile_num].name);
		fprintf(stdout, "#[%i]   %s\n", infile_num, input_file[infile_num].name);
		infile_error = infile_num;
	}

	/*  Output the line and the error message. */

	loadlc(loccnt, 0);
	fprintf(stdout, "%s\n", prlnbuf);
	fprintf(stdout, "       %s\n", stptr);
	if (switch_throwback) DDEUtils_ThrowbackSend_Error(input_file[infile_num].name, slnum, 1, stptr);
	errcnt++;
}

/* load 16 bit value in printable form into prlnbuf */

loadlc(int offset, int f)
{
	int	i;

	i = 7 + 9*f;

	if (!f) {
		hexcon(2, bank);
		prlnbuf[i++] = hex[1];
		prlnbuf[i++] = hex[2];
		prlnbuf[i++] = ':';
		offset |= page << 13;
	}
	hexcon(4, offset);
	prlnbuf[i++] = hex[1];
	prlnbuf[i++] = hex[2];
	prlnbuf[i++] = hex[3];
	prlnbuf[i]   = hex[4];
}

/* load value in hex into prlnbuf[contents[i]] */
/* and output hex characters to obuf if LAST_PASS & oflag == 1 */

loadv(int offset, int val, int f)
{
	hexcon(2, val);
	prlnbuf[16 + 3*f] = hex[1];
	prlnbuf[17 + 3*f] = hex[2];

	if (pass == LAST_PASS) {
		if (offset < 0x2000) {
			rom[bank][offset] = val;
			if (bank > max_bank)
				max_bank = bank;
		}
	}
}

/* convert number supplied as argument to hexadecimal in hex[digit] (lsd)
		through hex[1] (msd)		*/

hexcon(int digit, int num)
{
	for (; digit > 0; digit--) {
		hex[digit] = (num & 0x0f) + '0';
		if (hex[digit] > '9')
			hex[digit] += 'A' -'9' - 1;
		num >>= 4;
	}
}

