; ; instruction_m.def: This file contains definitions of example nanoprocessor ; Copyright (C) 2004 CESNET ; Author(s): Marek Pospisil ; ; LICENSE TERMS ; ; Redistribution and use in source and binary forms, with or without ; modification, are permitted provided that the following conditions ; are met: ; 1. Redistributions of source code must retain the above copyright ; notice, this list of conditions and the following disclaimer. ; 2. Redistributions in binary form must reproduce the above copyright ; notice, this list of conditions and the following disclaimer in ; the documentation and/or other materials provided with the ; distribution. ; 3. Neither the name of the Company nor the names of its contributors ; may be used to endorse or promote products derived from this ; software without specific prior written permission. ; ; ALTERNATIVELY, provided that this notice is retained in full, this ; product may be distributed under the terms of the GNU General Public ; License (GPL), in which case the provisions of the GPL apply INSTEAD ; OF those given above. ; ; This software is provided ``as is'', and any express or implied ; warranties, including, but not limited to, the implied warranties of ; merchantability and fitness for a particular purpose are disclaimed. ; In no event shall the company or contributors be liable for any ; direct, indirect, incidental, special, exemplary, or consequential ; damages (including, but not limited to, procurement of substitute ; goods or services; loss of use, data, or profits; or business ; interruption) however caused and on any theory of liability, whether ; in contract, strict liability, or tort (including negligence or ; otherwise) arising in any way out of the use of this software, even ; if advised of the possibility of such damage. ; ; $Id: instruction.def,v 1.7 2004/04/10 11:03:48 xhofer Exp $ ; ; #name "XOR_MACHINE" #define const_length 8 #define regaddr_length 8 #define jaddr_length 11 #instrlen 20 ; definitions of instructions of sample nanoprocessor ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; #define in :1011 $x[16] {\ if (feof(inp)) {\ fprintf(outp, "\n");\ if (inp!=stdout) fclose(inp);\ if (outp != stdout) fclose(outp);\ force_finish = 1;\ } else fscanf(inp, "%d", acc);\ ip ++;\ }; reads a value from the input file #define out :1001 $x[16] {\ fprintf(outp, "%d ", *acc);\ ip ++;\ }; writes the value of the ACCUMULATOR into the output file #define jmp $a :1101 $x[5] $a[jaddr_length] {\ ip = (code1 & JMP_MASK);\ }; jumps to the address `a' #define xor $b, $a :1111 $b[regaddr_length] $a[regaddr_length] {\ breg = &(memory[((code1 >> REGADDR_SHIFT) & REGADDR_MASK)]);\ areg = &(memory[(code1 & REGADDR_MASK)]);\ *acc = (*areg ^ *breg);\ ip ++;\ }; performes logical xor (acc = areg ^ breg) #define movr $b, $a :0001 $b[regaddr_length] $a[regaddr_length] {\ breg = &(memory[((code1 >> REGADDR_SHIFT) & REGADDR_MASK)]);\ areg = &(memory[(code1 & REGADDR_MASK)]);\ *breg = *areg;\ ip ++;\ }; stores value from address `a' in the memory on address `b' #define sta $a :0111 $x[const_length] $a[const_length] {\ areg = &(memory[(code1 & CONST_MASK)]);\ *areg = *acc;\ ip ++;\ }; stores a value from accumulator in the memory at specified address `a' #define lda $n :0011 $x[const_length] $n[const_length] {\ *acc = (u_int8_t) (code1 & CONST_MASK);\ ip++;\ }; loads a value `n' into the accumulator ; macros used in semantics of instructions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; #define { #define CONST_MASK 0xff} #define { #define REGADDR_MASK 0xff} #define { #define REGADDR_SHIFT 0x8} #define { #define JMP_MASK 0x7ff} #define MEMORY_SIZE 256 #define ACCUMULATOR 256 ; directives enabling debugging of nanoprograms ; - PRINT_MEM specifies the name of the memory array, ; whose content should be printable when debugging ; ; - PRINT_TYPE specifies the type of a single ; cell in the array ; ; - PRINT_MIN & PRINT_MAX specify the range of this ; array ; ; - in the case of input or output memories replace ; the word PRINT with the word PRINTIN or PRINTOUT ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; #define { #define PRINT_MEM memory} #define { #define PRINT_TYPE u_int8_t} #define { #define PRINT_MIN 0} #define { #define PRINT_MAX MEMORY_SIZE} ; variables used in semantics of instructions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; #define{ #include} #define{ #include} #define {\ u_int8_t memory[MEMORY_SIZE + 1];\ u_int8_t *acc = &(memory[ACCUMULATOR]);\ u_int8_t *areg = NULL;\ u_int8_t *breg = NULL;\ } ; initial part of the interpreter ; - the code below may be used, i.e., to parse ; and process nanoprogram parameters ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; #define {\ int i;\ int ai;\ int ao;\ FILE *inp, *outp;\ ai = ao = 0;\ inp = NULL;\ outp = NULL;\ for (i = 0; i <= MEMORY_SIZE; i++) memory[i] = 0;\ for (i = 0; i < argc; i++) {\ if (argv[i][0] == '-') {\ if (argv[i][1] == 'o') ao = 1;\ else if (argv[i][1] == 'i') ai = 1;\ else if (argv[i][1] == 'h'){\ printf("\nXOR machine interpreter syntax:\nnsim [-n] -i [options]\n");\ printf("\t-n\tno debug (inputs are converted to outputs only)\n");\ printf("options:\n");\ printf("\t-h\tprints this help screen\n");\ printf("\t-i file\tdefines input stream (text of decimal numbers)\n");\ printf("\t-o file\tdefines output stream (text of decimal numbers)\n");\ force_finish=1;\ }\ } else {\ if (ao && outp == NULL) {\ outp = fopen(argv[i], "w");\ if (outp == NULL) {\ fprintf(stderr, "error: output file %s cannot be opened\n", argv[i]);\ }\ }\ if (ai && inp == NULL) {\ inp = fopen(argv[i], "r");\ if (inp == NULL) {\ fprintf(stderr, "error: input file %s cannot be opened\n", argv[i]);\ }\ }\ }\ }\ if (inp == NULL) inp = stdin;\ if (outp == NULL) outp = stdout;\ }