; TASKING TriCore C compiler v1.5r1 Build 322 SN 00090364
; options: -e -A1 -g -O0 -Id:\ctri\include -s -N8 -zTC112_COR1 -zTC112_COR4
;          -zTC112_COR10 -zTC112_COR13 -zTC112_COR14 -zTC112_COR16 -zTC112_COR17
	.name	"queens"
	.symb	TOOL, "TASKING TriCore C compiler v1.5", 1
	.symb	TYPE, 256, "bit", 'g', 0, 1
	.symb	FILE, "queens.c"
; file "queens.c":
;    1 |/************************************************************************
;    2 | *
;    3 | *      FILEMAKE:       queens.c
;    4 | *
;    5 | *      DESCRIPTION:    Demonstration of the eight-queens problem. Each
;    6 | *                      solution found is displayed as a chess diagram.
;    7 | *
;    8 | *                      File system simulation is used to display the
;    9 | *                      progress and the results. File System
;   10 | *                      Simulation emulates a vt100 terminal.
;   11 | *
;   12 | *                      It is also possible to run it on a PC, using a
;   13 | *                      standard C compiler for DOS, when ANSI.SYS is
;   14 | *                      loaded. Needs "screen.c" for ANSI escape
;   15 | *                      sequences.
;   16 | *
;   17 | *      Copyright 2000-2002 Altium BV
;   18 | *
;   19 | ************************************************************************/
;   20 |
;   21 |#include <stdio.h>
	.symb	FILE, "d:\\ctri\\include\\stdio.h"
	.symb	FILE, "d:\\ctri\\include\\stdarg.h"
	.symb	ENDF
	.symb	TYPE, 258, 'P', #2
	.symb	TYPE, 259, "_iobuf", 'S', 24,\
		 "_ptr", #258, 0,\
		 "_base", #258, 4,\
		 "_cnt", #16, 8,\
		 "_bufsiz", #16, 12,\
		 "_flag", #5, 16,\
		 "_file", #2, 18,\
		 "_wcnt", #16, 20
	.symb	TYPE, 260, "FILE", 'T', #259
	.symb	TYPE, 261, "va_list", 'T', #258
	.symb	TYPE, 262, "size_t", 'T', #18
	.symb	TYPE, 263, "fpos_t", 'T', #7
	.symb	TYPE, 264, "off_t", 'T', #6
	.symb	ENDF
;   22 |#include <stdlib.h>
	.symb	FILE, "d:\\ctri\\include\\stdlib.h"
	.symb	TYPE, 265, 'S', 8,\
		 "quot", #16, 0,\
		 "rem", #16, 4
	.symb	TYPE, 266, "div_t", 'T', #265
	.symb	TYPE, 267, 'S', 8,\
		 "quot", #6, 0,\
		 "rem", #6, 4
	.symb	TYPE, 268, "ldiv_t", 'T', #267
	.symb	TYPE, 269, "wchar_t", 'T', #16
	.symb	ENDF
;   23 |
;   24 |#define NROWS   8
;   25 |#define NCOLS   8
;   26 |
;   27 |enum boolean { NO, YES };
;   28 |enum display { SEARCH_DISP, SOLUTION_DISP };
;   29 |
;   30 |static       short display_trying = YES;
	.sdecl	"data.queens",DATA,INIT,ABS18
	.sect	"data.queens"
	.align	4
display_trying:
	.half	0x0001
	.symb	IDEN, "display_trying", display_trying, #4, 131, 0x4
;   31 |static       short chess_board[NCOLS];            /* each index is a column     */
;   32 |                                                  /* each element is a row      */
;   33 |/*
;   34 | *      static function declarations
;   35 | */
;   36 |
;   37 |static void     find_legal_row( short, short );
;   38 |           
;   39 |
;   40 |void queens (void)
;   41 |{
	.sdecl	"code.queens",CODE
	.sect	"code.queens"
	.align	4
	.symb	LINE, 41
	.symb	TYPE, 270, 'X', 8, #1, 26, 0
	.symb	GFUN, "queens", queens, #270, 0, 0, 0
	.symb	ALAB, queens, #270
queens:
	.symb	SOFF, 0
;   42 |        find_legal_row( 0, 0 );
	.symb	LINE, 42
	mov16	d4,#0				; 0
	mov16	d5,#0				; 0
	callg	find_legal_row
;   43 |}
	.symb	LINE, 43
	ret16
	.symb	EFUN
;   44 |
;   45 |/*
;   46 | * This algorithm assumes there must be a single queen
;   47 | * in each column. It tries to find a legal row position
;   48 | * by checking it against the positions found in
;   49 | * previously processed columns.
;   50 | * When one has been found, it recursively calls itself
;   51 | * to find positions for subsequent columns. If the whole
;   52 | * chess_board is filled, the solution is printed.
;   53 | */
;   54 |
;   55 |static void
;   56 |find_legal_row (short row, short col)
;   57 |{
	.symb	LINE, 57
	.symb	TYPE, 271, 'X', 8, #1, 26, 2, #4, #4
	.symb	LFUN, "find_legal_row", find_legal_row, #271, 0, 32, 0
find_legal_row:
	.symb	SOFF, 32
	.symb	LTIM, "row", 4, 0, 0x8cd
	.symb	LTIM, "col", 5, 0, 0x8cd
	.symb	IDEN, "row", 4, #4, 0, 0x80d
	.symb	IDEN, "col", 5, #4, 0, 0x80d
	.symb	IDEN, "chk_row", -24, #4, 131, 0x5
	.symb	IDEN, "chk_col", -20, #4, 131, 0x5
	.symb	IDEN, "accepted", -16, #4, 131, 0x5
	.symb	LTIM, "col", 5, 0, 0x84d
	.symb	LTIM, "col", -32, 131, 0x8c5
	.symb	LTIM, "row", 4, 0, 0x84d
	.symb	LTIM, "row", -28, 131, 0x8c5
	nop
	sub16.a	sp,#32
;   58 |        short   chk_row,
;   59 |                chk_col,
;   60 |                accepted;
	.symb	LINE, 60
	st16.h	[sp],d5				; col
	st.h	[sp]4,d4			; row
;   61 |   
;   62 |        while ( row < NROWS )
	.symb	LINE, 62
	j	_26
_17:
;   63 |        {
;   64 |                accepted = YES;             
	.symb	LINE, 64
	mov16	d15,#1				; accepted
	st16.h	[sp]16,d15			; accepted
;   65 |                /* check this col-row combination
;   66 |                 * against previously filled columns
;   67 |                 */
;   68 |                for ( chk_col = 0; chk_col < col; chk_col++ )
	.symb	LINE, 68
	mov16	d15,#0				; chk_col
	st16.h	[sp]12,d15			; chk_col
	j	_21
_18:
	.symb	LTIM, "row", -28, 131, 0x845
	.symb	LTIM, "row", 7, 0, 0x8cd
	.symb	LTIM, "col", -32, 131, 0x845
	.symb	LTIM, "col", 15, 0, 0x8cd
	.symb	LTIM, "row", 7, 0, 0x84d
	.symb	LTIM, "row", 4, 0, 0x8cd
	.symb	LTIM, "col", 15, 0, 0x84d
	.symb	LTIM, "row", 4, 0, 0x84d
;   69 |                {
;   70 |                        chk_row = chess_board[chk_col];
	.symb	LINE, 70
	movh.a	a15,#@his(chess_board)
	lea	a15,[a15]@los(chess_board)
	ld16.h	d15,[sp]12			; chk_col
	addsc.a	a15,a15,d15,#1
	ld16.h	d15,[a15]
	st16.h	[sp]8,d15			; chk_row
;   71 |                        /*
;   72 |                         * same row or same diagonal?
;   73 |                         */
;   74 |                        if ( chk_row == row                        ||
;   75 |                             col - chk_col == abs (row - chk_row)
;   76 |                           )
	.symb	LINE, 76
	ld16.h	d15,[sp]8			; chk_row
	ld.h	d7,[sp]4			; row
	jeq	d15,d7,_19
	ld16.h	d15,[sp]			; col
	ld.h	d7,[sp]12			; chk_col
	sub	d14,d15,d7
	ld.h	d4,[sp]4			; row
	ld16.h	d15,[sp]8			; chk_row
	sub16	d4,d15
	callg	abs
	jne	d14,d2,_20
_19:
;   77 |                        {
;   78 |                                accepted = NO;  /* not allowed!   */
	.symb	LINE, 78
	mov16	d15,#0				; accepted
	st16.h	[sp]16,d15			; accepted
;   79 |                                break;      /* stop further testing */
	.symb	LINE, 79
	j	_22
;   80 |                        }
	.symb	LINE, 80
_20:
	.symb	LINE, 68
	nop
	ld16.h	d15,[sp]12			; chk_col
	add16	d15,#1				; chk_col
	st16.h	[sp]12,d15			; chk_col
_21:
	.symb	LTIM, "col", 7, 0, 0x8cd
	nop
	ld16.h	d15,[sp]12			; chk_col
	ld16.h	d7,[sp]				; col
	jlt	d15,d7,_18
;   81 |                }
	.symb	LINE, 81
_22:
	.symb	LTIM, "row", 15, 0, 0x8cd
	.symb	LTIM, "col", 7, 0, 0x84d
	.symb	LTIM, "col", 7, 0, 0x8cd
	.symb	LTIM, "row", 15, 0, 0x84d
	.symb	LTIM, "col", 7, 0, 0x84d
	.symb	LTIM, "col", 15, 0, 0x8cd
;   82 |                if ( accepted )
	.symb	LINE, 82
	nop
	ld16.h	d15,[sp]16			; accepted
	jeq	d15,#0,_25			; accepted
;   83 |                {
;   84 |                        chess_board[col] = row;
	.symb	LINE, 84
	ld16.h	d15,[sp]4			; row
	movh.a	a15,#@his(chess_board)
	lea	a15,[a15]@los(chess_board)
	ld16.h	d7,[sp]				; col
	addsc.a	a15,a15,d7,#1
	st16.h	[a15],d15
;   85 |                        if ( col == NCOLS-1 )
	.symb	LINE, 85
	ld16.h	d15,[sp]			; col
	jne	d15,#7,_23
;   86 |							;
	.symb	LINE, 86
	j	_24
_23:
	.symb	LTIM, "col", 15, 0, 0x84d
	.symb	LTIM, "col", 5, 0, 0x8cd
	.symb	LTIM, "col", 5, 0, 0x84d
;   87 |//                                display_board( SOLUTION_DISP );
;   88 |                        else
;   89 |                                find_legal_row( 0, col+1 );
	.symb	LINE, 89
	mov16	d4,#0				; 0
	ld16.h	d5,[sp]				; col
	add16	d5,#1
	extr	d5,d5,#0,#16
	callg	find_legal_row
_24:
;   90 |                }
	.symb	LINE, 90
_25:
	.symb	LTIM, "row", 15, 0, 0x8cd
	.symb	LTIM, "row", 15, 0, 0x84d
	.symb	LTIM, "row", -28, 131, 0x8c5
;   91 |//                display_field( empty, row, col, SEARCH_DISP );
;   92 |                row++;
	.symb	LINE, 92
	nop
	ld16.h	d15,[sp]4			; row
	add16	d15,#1				; row
	st16.h	[sp]4,d15			; row
	.symb	LINE, 62
_26:
	.symb	LTIM, "row", -28, 131, 0x845
	.symb	LTIM, "row", 15, 0, 0x8cd
	.symb	LTIM, "row", 15, 0, 0x84d
	nop
	ld16.h	d15,[sp]4			; row
	lt	d15,d15,#8
	jne	d15,#0,_17
;   93 |        }
;   94 |}
	.symb	LINE, 94
	ret16
	.symb	EFUN
;   95 |
	.symb	ENDF
	.symb	TYPE, 272, 'X', 8, #16, 26, 1, #16
	.symb	ALAB, abs, #272
	.symb	TYPE, 273, 'Z', #4, 7
	.symb	IDEN, "chess_board", chess_board, #273, 129, 0x4
	.sdecl	"farbss.queens",DATA,CLEAR
	.sect	"farbss.queens"
	.align	4
chess_board:
	.space	16
	.sect	"data.queens"
	.align	4
	.global	queens
	.extern	(CODE) abs
	.calls	"queens.find_legal_row", "abs"
	.calls	"queens.find_legal_row", "queens.find_legal_row"
	.calls	"queens", "queens.find_legal_row"
