📄 jbistub.c
字号:
// Module: jbistub.c//// Copyright (C) Altera Corporation 1997-2000// some parts (porting to linux kernel) (C) by Daniel Haensse under GPL// Description: Main source file for stand-alone JAM Byte Code Player#ifndef NO_ALTERA_STDIO#define NO_ALTERA_STDIO#endif#include <asm/MC68VZ328.h>#include <linux/config.h>#include "jbiport.h"#if defined ( CONFIG_FPGA1K30 ) #include "jbc30.h"#elif defined ( CONFIG_FPGA1K50 ) #include "jbc50.h"#else #error "jbc.h file needed"#endiftypedef int BOOL;typedef unsigned short WORD;typedef unsigned long DWORD;#define TRUE 1#define FALSE 0#include <linux/module.h>#include <linux/sched.h>#include <linux/tty.h>#include <linux/tty_flip.h>#include <linux/kernel.h>#include <linux/string.h>#include <linux/errno.h>#include <linux/kd.h>#include <linux/major.h>#include <linux/mm.h>#include <linux/console.h>#include <linux/init.h>#include <linux/devfs_fs_kernel.h>#include <linux/vt_kern.h>#include <linux/selection.h>#include <linux/console_struct.h>#include <linux/kbd_kern.h>#include <linux/consolemap.h>#include <linux/timer.h>#include <linux/interrupt.h>#include <linux/config.h>#include <linux/version.h>#include <linux/tqueue.h>#include <linux/bootmem.h>#include <linux/pm.h>#include <asm/io.h>#include <asm/system.h>#include <asm/uaccess.h>#include <asm/bitops.h>#include <asm/MC68VZ328.h>/*#include <stdio.h>#include <stdlib.h>#include <string.h>#include <io.h>#include <fcntl.h>#include <malloc.h>#include <time.h>#include <conio.h>#include <ctype.h>#include <sys/types.h>#include <sys/stat.h>*/#include "jbiexprt.h"/************************************************************************** Global variables*//* serial port interface available on all platforms *//* we need a direct interface to the serial port, because the serial port driver is not working that early at boottime*/extern void putC (char ch);extern void putS (char * str);extern void printhex (unsigned long n);/************************************************************************** Customized interface functions for JAM Byte Code Interpreter I/O:** jbi_jtag_io()* jbi_message()* jbi_delay()*/void initialize_jtag_hardware(){ // Inititialize ports B,D and M *(volatile unsigned char *) PBSEL_ADDR |= 0x20; *(volatile unsigned char *) PDSEL_ADDR |= 0x03; *(volatile unsigned char *) PMSEL_ADDR |= 0x20; *(volatile unsigned char *) PBPUEN_ADDR &= ~0x20; *(volatile unsigned char *) PDPUEN_ADDR &= ~0x03; *(volatile unsigned char *) PMPUEN_ADDR &= ~0x20; *(volatile unsigned char *) PBDIR_ADDR |= 0x20; *(volatile unsigned char *) PDDIR_ADDR |= 0x03; *(volatile unsigned char *) PMDIR_ADDR &= ~0x20; *(volatile unsigned char *) PBDATA_ADDR |= 0x20; *(volatile unsigned char *) PDDATA_ADDR |= 0x03; *(volatile unsigned char *) PMDATA_ADDR |= 0x20; //printk("Initilized\n");}void close_jtag_hardware(){ *(volatile unsigned char *) PBDIR_ADDR &= ~0x20; *(volatile unsigned char *) PDDIR_ADDR &= ~0x03; *(volatile unsigned char *) PMDIR_ADDR &= ~0x20; //printk("Closed\n");}/*inline int reading_jtag_hardware(){ int x; x= ((*(volatile unsigned char *) PMDATA_ADDR)&0x20) == 0x20; //printk("%x\n",x); return(x);}inline void writing_jtag_hardware(unsigned char x){ *(volatile unsigned char *) PBDATA_ADDR = ((*(volatile unsigned char *) PBDATA_ADDR)&(~0x20)) | (x&0x20); *(volatile unsigned char *) PDDATA_ADDR = ((*(volatile unsigned char *) PDDATA_ADDR)&(~0x03)) | (x&0x03); //printk("Write\n");} */__inline__ int jbi_jtag_io(int tms, int tdi, int read_tdo){ int tdo = 0; PBDATA = tms ? (PBDATA|0x20) : (PBDATA&0xdf); /* TMS */ PDDATA = (PDDATA&0xfc) | (tdi ? 0x01 : 00 ); /* TCK, TDI */ if (read_tdo) { tdo=(PMDATA&0x20) == 0x20; } PDDATA|=0x02; PDDATA&=0xfd; return (tdo);}void jbi_message(char *message_text){ printk(message_text); putS(message_text);}void jbi_export_integer(char *key, long value){ printk("Export: key = \"%s\", value = %ld\n", key, value);}#define HEX_LINE_CHARS 72#define HEX_LINE_BITS (HEX_LINE_CHARS * 4)char conv_to_hex(int value){ char c; if (value > 9) { c = (char) (value + ('A' - 10)); } else { c = (char) (value + '0'); } return (c);}void jbi_export_boolean_array(char *key, unsigned char *data, long count){ unsigned int size, line, lines, linebits, value, j, k; char string[HEX_LINE_CHARS + 1]; long i, offset; if (count > HEX_LINE_BITS) { printk( "Export: key = \"%s\", %ld bits, value = HEX\n", key, count); lines = (unsigned int) ((count + (HEX_LINE_BITS - 1)) / HEX_LINE_BITS); for (line = 0; line < lines; ++line) { if (line < (lines - 1)) { linebits = HEX_LINE_BITS; size = HEX_LINE_CHARS; offset = count - ((line + 1) * HEX_LINE_BITS); } else { linebits = (unsigned int) (count - ((lines - 1) * HEX_LINE_BITS)); size = (linebits + 3) / 4; offset = 0L; } string[size] = '\0'; j = size - 1; value = 0; for (k = 0; k < linebits; ++k) { i = k + offset; if (data[i >> 3] & (1 << (i & 7))) value |= (1 << (i & 3)); if ((i & 3) == 3) { string[j] = conv_to_hex(value); value = 0; --j; } } if ((k & 3) > 0) string[j] = conv_to_hex(value); printk( "%s\n", string); } } else { size = (unsigned int) ((count + 3) / 4); string[size] = '\0'; j = size - 1; value = 0; for (i = 0; i < count; ++i) { if (data[i >> 3] & (1 << (i & 7))) value |= (1 << (i & 3)); if ((i & 3) == 3) { string[j] = conv_to_hex(value); value = 0; --j; } } if ((i & 3) > 0) string[j] = conv_to_hex(value); printk( "Export: key = \"%s\", %ld bits, value = HEX %s\n", key, count, string); }}static long memcount;extern int _ramstart;// FIXME: We assume that we have enough memory and the stack won't overlap the memory we use herevoid *jbi_malloc(unsigned int size){ void *p; p=(void*)(_ramstart + memcount); if((size%4)!=0) size=size+(4-(size%4)); //printk("alloc %lx %lx\n",p,memcount); //printhex(p); //putC('\n'); //printhex(size); //putC('\n'); memcount+=size; //putC('\n'); //putC('\r'); return (p);}void jbi_free(void *ptr){}char *error_text[] ={/* JBIC_SUCCESS 0 */ "success",/* JBIC_OUT_OF_MEMORY 1 */ "out of memory",/* JBIC_IO_ERROR 2 */ "file access error",/* JAMC_SYNTAX_ERROR 3 */ "syntax error",/* JBIC_UNEXPECTED_END 4 */ "unexpected end of file",/* JBIC_UNDEFINED_SYMBOL 5 */ "undefined symbol",/* JAMC_REDEFINED_SYMBOL 6 */ "redefined symbol",/* JBIC_INTEGER_OVERFLOW 7 */ "integer overflow",/* JBIC_DIVIDE_BY_ZERO 8 */ "divide by zero",/* JBIC_CRC_ERROR 9 */ "CRC mismatch",/* JBIC_INTERNAL_ERROR 10 */ "internal error",/* JBIC_BOUNDS_ERROR 11 */ "bounds error",/* JAMC_TYPE_MISMATCH 12 */ "type mismatch",/* JAMC_ASSIGN_TO_CONST 13 */ "assignment to constant",/* JAMC_NEXT_UNEXPECTED 14 */ "NEXT unexpected",/* JAMC_POP_UNEXPECTED 15 */ "POP unexpected",/* JAMC_RETURN_UNEXPECTED 16 */ "RETURN unexpected",/* JAMC_ILLEGAL_SYMBOL 17 */ "illegal symbol name",/* JBIC_VECTOR_MAP_FAILED 18 */ "vector signal name not found",/* JBIC_USER_ABORT 19 */ "execution cancelled",/* JBIC_STACK_OVERFLOW 20 */ "stack overflow",/* JBIC_ILLEGAL_OPCODE 21 */ "illegal instruction code",/* JAMC_PHASE_ERROR 22 */ "phase error",/* JAMC_SCOPE_ERROR 23 */ "scope error",/* JBIC_ACTION_NOT_FOUND 24 */ "action not found",};#define MAX_ERROR_CODE (int)((sizeof(error_text)/sizeof(error_text[0]))+1)/************************************************************************/int jbi_main(){ /* file buffer for JAM Byte Code input file */ unsigned char *file_buffer = NULL; long file_length = 0L; long offset = 0L; long error_address = 0L; JBI_RETURN_TYPE crc_result = JBIC_SUCCESS; JBI_RETURN_TYPE exec_result = JBIC_SUCCESS; unsigned short expected_crc = 0; unsigned short actual_crc = 0; char key[33] = {0}; char value[257] = {0}; int exit_status = 0; int exit_code = 0; int format_version = 0; char *workspace = NULL; char *action = "CONFIGURE"; char *init_list[10]; //int init_count = 0; long workspace_size = 0; char *exit_string = NULL; int reset_jtag = 0; int execute_program = 1; int action_count = 0; int procedure_count = 0; int index = 0; char *action_name = NULL; char *description = NULL; JBI_PROCINFO *procedure_list = NULL; JBI_PROCINFO *procptr = NULL; memcount=0; init_list[0] = NULL; /* print out the version string and copyright message */ printk( "Jam STAPL Byte-Code Player Version 2.1\nCopyright (C) 1998-2000 Altera Corporation\nPorted to Linux by Daniel Haensse <daniel.haensse@alumni.ethz.ch>\n"); initialize_jtag_hardware(); if ((workspace_size > 0) && ((workspace = (unsigned char*) jbi_malloc( (size_t) workspace_size )) == NULL)) { printk( "Error: can't allocate memory (%d Kbytes)\n", (int) (workspace_size / 1024L)); exit_status = 1; } /* Set file_buffer to download code */ file_buffer=jbc_code; // points to the download code file_length=jbc_code_size; /* Check CRC */ crc_result = jbi_check_crc(file_buffer, file_length,&expected_crc, &actual_crc); if(crc_result == JBIC_CRC_ERROR) { switch (crc_result) { case JBIC_SUCCESS: printk( "CRC matched: CRC value = %04X\n", actual_crc); break; case JBIC_CRC_ERROR: printk( "CRC mismatch: expected %04X, actual %04X\n",expected_crc, actual_crc); return(13); break; case JBIC_UNEXPECTED_END: printk( "Expected CRC not found, actual CRC value = %04X\n",actual_crc); return(13); break; case JBIC_IO_ERROR: printk( "Error: File format is not recognized.\n"); return(13); break; default: printk( "CRC function returned error code %d\n", crc_result); return(13); break; } } /* * Display file format version */ jbi_get_file_info(file_buffer, file_length, &format_version, &action_count, &procedure_count); printk( "File format is %s Byte-code format\n",(format_version == 2) ? "Jam STAPL" : "Jam 1.1"); /* * Dump out NOTE fields */ while (jbi_get_note(file_buffer, file_length, &offset, key, value, 256) == 0) { printk( "NOTE \"%s\" = \"%s\"\n", key, value); } /* * Dump the action table */ if ((format_version == 2) && (action_count > 0)) { printk( "\nActions available in this file:\n"); for (index = 0; index < action_count; ++index) { jbi_get_action_info(file_buffer, file_length, index, &action_name, &description, &procedure_list); if (description == NULL) { printk( "%s\n", action_name); } else { printk( "%s \"%s\"\n", action_name, description); } procptr = procedure_list; while (procptr != NULL) { if (procptr->attributes != 0) { printk( " %s (%s)\n", procptr->name, (procptr->attributes == 1) ? "optional" : "recommended"); } procedure_list = procptr->next; jbi_free(procptr); procptr = procedure_list; } } /* add a blank line before execution messages */ if (execute_program) printk( "\n"); } if (execute_program) { /* * Execute the JAM Byte Code program */ exec_result = jbi_execute(file_buffer, file_length, workspace, workspace_size, action, init_list, reset_jtag, &error_address, &exit_code, &format_version); if (exec_result == JBIC_SUCCESS) { if (format_version == 2) { switch (exit_code) { case 0: exit_string = "Success"; break; case 1: exit_string = "Checking chain failure"; break; case 2: exit_string = "Reading IDCODE failure"; break; case 3: exit_string = "Reading USERCODE failure"; break; case 4: exit_string = "Reading UESCODE failure"; break; case 5: exit_string = "Entering ISP failure"; break; case 6: exit_string = "Unrecognized device"; break; case 7: exit_string = "Device revision is not supported"; break; case 8: exit_string = "Erase failure"; break; case 9: exit_string = "Device is not blank"; break; case 10: exit_string = "Device programming failure"; break; case 11: exit_string = "Device verify failure"; break; case 12: exit_string = "Read failure"; break; case 13: exit_string = "Calculating checksum failure"; break; case 14: exit_string = "Setting security bit failure"; break; case 15: exit_string = "Querying security bit failure"; break; case 16: exit_string = "Exiting ISP failure"; break; case 17: exit_string = "Performing system test failure"; break; default: exit_string = "Unknown exit code"; break; } } else { switch (exit_code) { case 0: exit_string = "Success"; break; case 1: exit_string = "Illegal initialization values"; break; case 2: exit_string = "Unrecognized device"; break; case 3: exit_string = "Device revision is not supported"; break; case 4: exit_string = "Device programming failure"; break; case 5: exit_string = "Device is not blank"; break; case 6: exit_string = "Device verify failure"; break; case 7: exit_string = "SRAM configuration failure"; break; default: exit_string = "Unknown exit code"; break; } } printk( "Exit code = %d... %s\n", exit_code, exit_string); } else if ((format_version == 2) && (exec_result == JBIC_ACTION_NOT_FOUND)) { if ((action == NULL) || (*action == '\0')) { printk( "Error: no action specified for Jam file.\nProgram terminated.\n"); } else { printk( "Error: action \"%s\" is not supported for this Jam file.\nProgram terminated.\n", action); } } else if (exec_result < MAX_ERROR_CODE) { printk( "Error at address %ld: %s.\nProgram terminated.\n", error_address, error_text[exec_result]); } else { printk( "Unknown error code %d\n", exec_result); } } putS("\n\r"); close_jtag_hardware(); if (workspace != NULL) jbi_free(workspace); return (exit_status);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -