Flash Loader for Z8 Encore


Application Note
Flash Loader for Z8 Encore!







ZiLOG Worldwide Headquarters








Information Integrity







Document Disclaimer














Table of Contents



















List of Figures




















List of Tables






Abstract










General Overview




























Discussion



Theory of Operation





















:
















4000h




Flash Loader




































0000h-0001h
0002h-0007h
0008h-0037h
0038h-01FFh
0200h-3FFFh
4000h-FFFFh
Results of Operation






























4000h- FFFFh











4000h







Summary









Technical Discussion
Code relocation

4000h
0-37h










/* Define  C0 Interrupt for code relocation. Interrupt  C0 may not be
required for the application, however is still defined as it is
is a workaround for ZDS-II to do code relocation*/
#pragma interrupt
void isr_C0(void)
{
}
void init_PC0(void)
{
SET_VECTOR(C0, isr_C0);
}
Intel Hex File




Record Marker


Record Length
GDWD

Address


Record Type

00 01
Data Bytes


Checksum









:10008000AF5F67F0602703E0322CFA92007
780C361
:1000900089001C6B7EA7CA9200FE10D2AA0
0477D81
:0B00A00080FA92006F3600C3A00076CB
:00000001FF


10h
0080h



Flash Loader Complete Code Breakup



































Project Settings
































Flash Loader Code
Header files
Flash_IO.h : Header file for Flash Driver
/*************************************************
* Copyright (C) 1999-2002 by ZiLOG, Inc.
* All Rights Reserved
*
* Flash_IO.h : Header file for Flash Driver
**************************************************/
#define PAGE_SIZE 512
//Prototype definitions
unsigned int UnlockFlash(void);
void LockFlash(void);
unsigned int EraseFlash(unsigned int pagenum);
void WriteByteToFlash(int location, unsigned int value);
void WriteByteToFlash1(int location, unsigned int value);
unsigned int ReadByteFromFlash(int location);
EVB.h : Evaluation board header file and Ram I/O defines
/************************************************************
* Copyright (C) 1999-2002 by ZiLOG, Inc.
* All Rights Reserved
*
* EVB.h :Evaluation board header file and RAM I/O defines
************************************************************/
typedef unsigned char volatile far *IORegInt8;
#define IO_ADDR(addr) *((IORegInt8) addr)




sio.h : Header file for Serial Drivers
/*************************************************
* Copyright (C) 1999-2002 by ZiLOG, Inc.
* All Rights Reserved
*
* SIO.h : Header file for Serial Drivers
**************************************************/
// Function Prototypes
void init_uart(void);
char putch(char c);
int kbhit(void);
char getch(void);
void init_UART1_Int(void);
void dis_UART1_Int(void);
void modem_access(void);
void DMA_modem_access(void);




loadhex.h : Header file for HREX file parser and loader
/***********************************************************
* Copyright (C) 1999-2002 by ZiLOG, Inc.
* All Rights Reserved
*
* loadhex.h : Header file for HEX file parser and loader
***********************************************************/
// prototype definitions
void InitLoadHex(void);
void LoadHexMain(void);
char HexCharToLowNibble(char c);
#define VersionString "\r\nZiLOG eZ8 Flash Loader Version 0.95"
#define PromptString "\r\neZ8>"
#define USER_START_ADDR0 x4000
#define JUMP_TO_USER_CODE "jp 4000H" // "jp USER_START_ADDR"
#define USER_START_PAGE 0x20 // (USER_START_ADDDR >> 9)
#define USER_END_PAGE 0x7F // (0xFFFF >> 9)
#define BL_STA_HIGH 0x02 // Boot Loader Start Address High Nibble
#define BL_STA_LOW 0x00 // Boot Loader Start Address Low Nibble
//----------------------------------------------------------
//General constants
#define FALSE 0
#define TRUE 1
//-----------------------------------------------------------
//ASCII char values for some special keys used in the monitor
#define BACKSPACE 8
#define CTRL_Q 17
#define CTRL_S 19
#define ESC 27
#define CTRL_C 3
#defineBELL 7
//Various record types used in eZ8 Hex files
#define DATARECORD 0x00
#define EOFRECORD 0x01
//Various return codes
#define LOADHEX_OK 0
#define LOADHEX_USERABORT 1
#define LOADHEX_ERRONEOUS 2
//---------------------------------------------------------------------------
//Various states of the hex file downloader function
#define WAITFOR_RECORDBEGIN 1
#define RECORDLENGTH 2
#define COMMENTFIELD 3
#define RECORDADDRESS 4
#define RECORDTYPE 5
#define DATAFIELD 6
#define SUMCHECK 7
#define EXTENDEDADDRESS 8
#define STARTADDRESS 9




UserIF.h : Header file for Flash Loader User Interface
/***********************************************************
* Copyright (C) 1999-2002 by ZiLOG, Inc.
* All Rights Reserved
*
* UserIF.h : Header file for Flash Loader User Interface
***********************************************************/
// Function Prototypes
void LoadHex(void);
void Write_Flash1(void);
void Read_Flash1 (void);
void inputIOport(void);
void outputIOport(void);
void prompt(void);
void display_help(void);
void execute_user_code(void);
void process_char(int c);




Utility.h : Header file for Flash Loader utilities
/********************************************************
* Copyright (C) 1999-2002 by ZiLOG, Inc.
* All Rights Reserved
*
* utlity.h :Header file for Flash Loader Utilties
*******************************************************/
// Prototype Definitions
void clear_screen (void);
unsigned int ScanHex (unsigned char num);
int ascii_to_hex(int c);




C-files
flashloader.c : contains main program for Flash Boot Loader
/*************************************************
* Copyright (C) 1999-2002 by ZiLOG, Inc.
* All Rights Reserved
*
* flashloader.c: contains main program
* for Flash Boot Loader
*************************************************/
#include
#include
#include
#include "SIO.h"
#include "utility.h"
#include "loadhex.h"
#include "UserIF.h"
/*************************************************
* Main Program
**************************************************/
main ()
{
int i;
unsigned char ch;
init_uart();
// check for special sequence key - space tab
for(i=0; i< 0x2000; i++)
if(U0STAT0 & 0x80) //Wait for receive data available
ch = U0D;
// if Special sequence key is not present - transfer PC
// to user application code area
if( ch !=   )
execute_user_code();
// if Special sequence key is present -- clear screen, put Version String
// and prompt for User input
clear_screen();
puts(VersionString);
puts("\r\n\n Type H for help\n");
prompt();
while(1)
{
do
{
ch = getch ();




putch (ch);
} while(ch < 0);
process_char(ch);
}
} // end of main
/* Define  C0 Interrupt for code relocation. Interrupt  C0 is not
required for the boot loader, however is still defined as it is
a workaround for ZDS-II to perform code relocation*/
#pragma interrupt
void isr_C0(void)
{
}
void init_PC0(void)
{
SET_VECTOR(C0, isr_C0);
}




flash_IO.c : Flash I/O Driver Routines
/*************************************************
* Copyright (C) 1999-2002 by ZiLOG, Inc.
* All Rights Reserved
*
* flash_IO.c:Flash I/O Driver routines
*************************************************/
#include
#include "flash_IO.h"
char far Flash_Page[PAGE_SIZE];// Far is used so this variable is placed in
//Edata
char near RamByte;
/* This function unlocks Flash for Flash Programming */
unsigned int UnlockFlash(void)
{
if( (FSTAT & 0x3F) == 0x00 )// Flash Controller Locked state
{
FCTL = 0x73;// First unlock command
if ( (FSTAT & 0x3F) == 0x01 )// First unlock Command recieved
{
FCTL = 0x8C;// Second unlock command
if ( (FSTAT & 0x3F) == 0x02 )// Unlocked programming state
return 0x00;
else
return 0xFF;
}
else
return 0xFF;
}
else
return 0xFF;
}
/* Locks the Flash controller */
void LockFlash(void)
{
while( (FSTAT & 0x3F) != 0x00 )
{
FCTL = 0xFF;// write some value other than 0x73, 0x8c, 0x95, and 0x63
// to Flash control Register
}
}
/* This function Erases the page "pagenum" in Flash */
unsigned int EraseFlash(unsigned int pagenum)
{
unsigned int status;




status = UnlockFlash();
if (status == 0x00)
{ // check if Flash is unlocked
FPS = pagenum; // Set the page to be erased
FCTL = 0x95;// Page erase
}
// No need to unlock as Flash controller locks immediatetly after page erase
}
/* This function writes a single byte value to the Flash location. It assumes the Page
that corresponds to the location needs to be erased. */
void WriteByteToFlash(int location, unsigned int value)
{
unsigned int pagenum;
int i, TempLoc;
pagenum = (location>>9);// compute the page number of byte address
TempLoc = (location & 0xFE00);
for(i=0; i< PAGE_SIZE; i++)// save the Flash page in RAM
Flash_Page[i] = ReadByteFromFlash(TempLoc+i);
// Modify the byte location in RAM that corresponds to Flash saved page
Flash_Page[location - TempLoc] = value;
EraseFlash(pagenum);// erase Flash Page
// Restore the page from RAM back to Flash
for(i=0; i< PAGE_SIZE; i++)
WriteByteToFlash1(TempLoc+i, Flash_Page[i]);
LockFlash();
}
/* This function writes a single byte value in the Flash location. It assumes the Byte
value of the Flash location is 0xFF and need NOT be erased before programming */
void WriteByteToFlash1(int location, unsigned int value)
{
UnlockFlash();
RamByte=(location>>8);
asm("LD R8, _RamByte");
RamByte = (location & 0x00FF);
asm("LD R9, _RamByte");
RamByte = value;
asm("LD R10, _RamByte");
asm("LDC @RR8, R10");// Load Byte into Flash location
}




/* This function returns a single byte value read from the Flash location */
unsigned int ReadByteFromFlash(int location)
{
unsigned int value;
RamByte=(location>>8);
asm("LD R8, _RamByte");
RamByte = (location & 0x00FF);
asm("LD R9, _RamByte");
asm("LDC R10, @RR8");
asm("LD _RamByte, R10");
value = RamByte;
return (value&0xFF);
}




loadhex.c : Functions to parse a hex file and download into the Flash
/*************************************************
* Copyright (C) 1999-2002 by ZiLOG, Inc.
* All Rights Reserved
*
* loadhex.c : Functions to parse a hex file and
* download into the Flash
**************************************************/
#include
#include
#include
#include "loadhex.h"
#include "flash_IO.h"
#include "SIO.h"
#include "utility.h"
extern char far Flash_Page[];
far char InterruptTable[0x38];
//------------------------------------------------------------------------------
char Ch, PrevCh;
unsigned int DownLoadState, HexCharCount, SumCheckValue, DownLoadErrors;
unsigned int Record_Address, RecType, CheckSumByte, DataCount, Count, index;
unsigned int LinesInFile, TotalBytes, NotWritable;
char done, SumByte, ChUpper, DataByte;
//------------------------------------------------------------------------------
char GetNextChar(void)
{
PrevCh = Ch;
Ch = getch();
return(Ch);
}
//------------------------------------------------------------------------------
void InitLoadHex(void)
{
unsigned int ch;
for(ch=0; ch < 0x38 ; ch++)
InterruptTable[ch] = 0xFF;
// Erase from location USER_START_ADDRESS(0x20) - 0xFFFF
for(ch = USER_START_PAGE; ch <= USER_END_PAGE; ch++)
EraseFlash(ch);
done = FALSE; PrevCh =  \0 ;
DownLoadState = WAITFOR_RECORDBEGIN; //wait for  :
DownLoadErrors = 0;
TotalBytes = 0;
NotWritable = 0;
LinesInFile = 0;
}




void LoadHexMain(void )
{
unsigned int ch;
Ch = GetNextChar();
if(Ch == ESC)
{
done = TRUE;
DownLoadState=LOADHEX_USERABORT;//User terminated load by pressing ESC key
}
else if(Ch ==  : && (DownLoadState != WAITFOR_RECORDBEGIN))
{
if (PrevCh ==  \n )
LinesInFile++;
else
DownLoadErrors++;
DownLoadState=WAITFOR_RECORDBEGIN;
}
//Process te char depending on the state of download
switch(DownLoadState)
{
case WAITFOR_RECORDBEGIN:
switch(Ch)
{
case   : //Ignore white space before record begins
case  \t :
case  \r : //keep track of No.of Lines in hex file
LinesInFile++;
break;
case  \n :
if(PrevCh ==  \r ) break;
else LinesInFile++;
break;
case  : :
SumCheckValue = 0; //Init the byte checksum to 0
DownLoadState = RECORDLENGTH;
HexCharCount = 0;
Count = 0;
break;
case  * : //Allow for * delimited lines
DownLoadState = COMMENTFIELD;
break;
default: //We are recving out of place characters/ damaged record stream
DownLoadState = COMMENTFIELD;
DownLoadErrors++;
break;
}
break;
//---------------------------------------------------------
case COMMENTFIELD: //Wait till resynchronization opportunity
if(Ch ==  \n || Ch ==  \r )
DownLoadState = WAITFOR_RECORDBEGIN;




break;
//----------------------------------------------------------
case RECORDLENGTH:
if(!isxdigit(Ch))
{
DownLoadState = COMMENTFIELD;
break;
}
Count = ((Count << 4) | ascii_to_hex(Ch)) & 0xFF;
HexCharCount++;
if((HexCharCount & 0x01) == 0) SumCheckValue += Count & 0xFF;
if(HexCharCount == 2)
{
DownLoadState = RECORDADDRESS;
Record_Address = 0;
HexCharCount=0;
}
break;
//-----------------------------------------------------------
case RECORDADDRESS: //A 4 character offset address field
if(!isxdigit(Ch))
{
DownLoadState = COMMENTFIELD;
break;
}
Record_Address = ((Record_Address << 4) | ascii_to_hex(Ch)) & 0xFFFF;
HexCharCount++;
if((HexCharCount & 0x01) == 0) SumCheckValue += Record_Address & 0xFF;
if(HexCharCount > 3)
{
DownLoadState = RECORDTYPE;
HexCharCount = 0;
RecType=0;
}
break;
//----------------------------------------------------------
case RECORDTYPE:
if(!isxdigit(Ch))
{
DownLoadState = COMMENTFIELD;
break;
}
RecType = ((RecType << 4) | ascii_to_hex(Ch)) & 0xFF;
HexCharCount++;
if(HexCharCount > 1)
{
HexCharCount = 0;
SumCheckValue += RecType;
index =0;
switch(RecType)
{
//----------------------------
case DATARECORD:




HexCharCount = 0;
if(Count>0)
DownLoadState = DATAFIELD;
else
DownLoadState = SUMCHECK;
SumByte = 0;
break;
//---------------------------
case EOFRECORD:
DownLoadState = SUMCHECK;
HexCharCount = 0;
SumByte = 0; //This used to recv incoming chksum
// Program the Option bits and Interrupt Vector Table
EraseFlash(0x00);
// Save User Reset Address
Record_Address =
(InterruptTable[03] + (InterruptTable[02]<<8) );
// Write BootLoader Reset Address
InterruptTable[02] = BL_STA_HIGH;
InterruptTable[03] = BL_STA_LOW;
for(ch=0; ch < 0x38 ; ch++)
{ WriteByteToFlash1((int)ch, (
unsigned int)InterruptTable[ch]);
LockFlash();
done = TRUE;
break;
//--------------------------
default:
DownLoadState = COMMENTFIELD;
DownLoadErrors++;
break;
//--------------------------
}//switch(RecType)
}//if
break;
//---------------------------------------------------------------
case DATAFIELD:
if(!isxdigit(Ch))
{
DownLoadErrors++;
DownLoadState = COMMENTFIELD;
break;
}
//Shift in the newest nibble and mask off leakage into upper nibbles
DataByte = ((DataByte << 4) | ascii_to_hex(Ch)) & 0xFF; //FF mask
HexCharCount++;
if((HexCharCount & 0x01) == 0)
{




SumCheckValue += DataByte;
Flash_Page[index] = DataByte;
if( Flash_Page[index] != DataByte)
{
NotWritable++;
DownLoadErrors++;
}
TotalBytes++;
index++;
}
//Same as "Hexchars >= Count*2" bytes
if(HexCharCount >= (Count << 1))
{
DownLoadState = SUMCHECK;
SumByte = 0;
HexCharCount = 0;
if (Record_Address > 0x3FFF)
{
// Program flash
for(ch=0; ch < Count ; ch++)
WriteByteToFlash1(Record_Address+ch,
(unsigned int)Flash_Page[ch]);
LockFlash();
}
else if (Record_Address < 0x0038)
{
for(ch=0; ch < Count; ch++)
InterruptTable[ch + (Record_Address& 0xFF)] =
Flash_Page[ch];
}
else
{
NotWritable += Count;
printf("\nHex file conflict with boot loader at
Record Address: %4x\n", Record_Address);
}
}
break;
//--------------------------------------------------------------
case SUMCHECK:
if(!isxdigit(Ch))
{
DownLoadState = COMMENTFIELD;
break;
}
SumByte = ((SumByte << 4) | ascii_to_hex(Ch)) & 0xFF;
HexCharCount++;
if(HexCharCount > 1)
{
putch( . );
HexCharCount = 0;




if(((SumByte + SumCheckValue)&0xFF) != 0)
DownLoadErrors++;
if(RecType == EOFRECORD)
done = TRUE;
else
{
DownLoadState = WAITFOR_RECORDBEGIN;
HexCharCount = 0;
}
}
break;
//-------------------------------------------------------------
default:
DownLoadErrors++;
DownLoadState = COMMENTFIELD;
break;
//-------------------------------------------------------------
}// end of switch
}// end of loadhex
//---------------------------------------------------------------------------




Sio.c : Serial I/O Driver Routines
/*************************************************
* Copyright (C) 1999-2002 by ZiLOG, Inc.
* All Rights Reserved
*
* Sio.c :Serial I/O Driver Routines
*************************************************/
#include
#include
// BRG = freq/( baud * 16)
#define FREQ 18432000 // 18.432MHz
#define BAUD 9600 // 9.6K baud
#define BRG FREQ/(BAUD*16)
#define UART0
/*****************************************************************************/
void init_uart(void)
{
PAAF |= 0x30; // Enable TxD0/RxD0 pins
U0BRH = (char)(BRG >> 8);
U0BRL = (char)(BRG & 0xff);
U0CTL0 = 0xc0; // Transmit enable, Receive Enable, No Parity, 1 Stop
}
/****************************************************************/
char putch(char c)
{
if (c ==  \n )
{
do
{
#if defined(UART0)
U0CTL0 |= 0x20; // Clear to send enable
} while (!(U0STAT0 & 0x04)); // Transmit Data register enabled
U0D =  \r ; // Send data
#elif defined(UART1)
U1CTL0 |= 0x20; // Clear to send enable
} while (!(U1STAT1 & 0x04)); // Transmit Data register enabled
U1D =  \r ; // Send data
#endif
}
do
{
#if defined(UART0)
U0CTL0 |= 0x20; // Clear to send enable
} while (!(U0STAT0 & 0x04)); // Transmit Data register enabled
U0D = c; // Send data
#elif defined(UART1)
U1CTL0 |= 0x20; // Clear to send enable
} while (!(U1STAT1 & 0x04));// Transmit Data register enabled
U1D = c; // Send data
#endif




}
/****************************************************************/
int kbhit(void)
{
#if defined(UART0)
if (U0STAT0 & 0x80) // check for receive data available
#elif defined(UART1)
if (U1STAT1 & 0x80) // check for receive data available
#endif
return(1);
return(0);
}
/****************************************************************/
char getch(void)
{
char ch;
#if defined(UART0)
while (!(U0STAT0 & 0x80)); // Wait for receive data available
ch = U0D;
#elif defined(UART1)
while (!(U1STAT1 & 0x80)); // Wait for receive data available
ch = U1D;
#endif
if (ch ==  \r || ch == ( \r |0x80))
return  \n ;
return(ch);
}




userif.c : User Interface for the Flash Loader
/*************************************************
* Copyright (C) 1999-2002 by ZiLOG, Inc.
* All Rights Reserved
*
* userif.c : User Interface for the Flash Loader
**************************************************/
#include
#include
#include
#include
#include "UserIF.h"
#include "utility.h"
#include "Flash_IO.h"
#include "EVB.h"
#include "SIO.h"
#include "loadhex.h"
extern unsigned int DownLoadState, DownLoadErrors;
extern unsigned int Record_Address, LinesInFile, TotalBytes, NotWritable;
extern char done;
//---------------------------------------------------------------------------
void LoadHex(void)
{
printf ("\r\n\tLoad Hex file.. ");
InitLoadHex();
while(!done)
{
LoadHexMain();
}
if(DownLoadState == LOADHEX_USERABORT)
printf("\r\nTerminated by ESC\r\n");
else
{
printf("\r\nDownload Completed\n\n");
printf("\r\n\tDownload Summary of User Application");
printf("\r\n\t====================================\n");
printf("\r\nStart/Reset Address \t\t: %X", Record_Address);
printf("\r\nNo. of Bytes \t\t\t: %d", TotalBytes);
printf("\r\nNo. of Bytes Not Writable \t: %d", NotWritable);
printf("\r\nNo. of Lines \t\t\t: %d", LinesInFile);
printf("\r\nNo. of Download Errors \t\t: %d", DownLoadErrors);
}
}
/****************************************************************/
// This funcction writes to FLASH
/****************************************************************/
void Write_Flash1(void)




{
unsigned char data;
unsigned int addr;
printf ("\n\r\nEnter Memory Location = ");
addr = ScanHex (4);
data = ReadByteFromFlash(addr);
printf ("\n\rCurrent FLASH Value [%4X] = %2X", addr, data);
printf ("\n\r\nEnter New data = ");
data = ScanHex (2);
WriteByteToFlash(addr, data);
}
/****************************************************************/
// This funcction reads from FLASH
/****************************************************************/
void Read_Flash1(void)
{
unsigned char data;
unsigned int addr;
printf ("\n\r\nEnter Memory Location = ");
addr = ScanHex (4);
data = ReadByteFromFlash(addr);
printf("\n\r\nFLASH [%4X] = %2X", addr, data);
}
/****************************************************************/
// This function reads IO port
/****************************************************************/
void inputIOport(void)
{
unsigned char ch;
int address;
printf ("\r\n\tEnter I/O Address [HEX]: ");
address = ScanHex (3);
ch = IO_ADDR (address);
printf ("\n\r\tCurrent I/O Data = %xH [%xH]",ch,address);
}
/****************************************************************/
// This function writes IO port
/****************************************************************/
void outputIOport(void)
{
unsigned char ch;
int address;
printf ("\r\n\tEnter I/O Addr [HEX]: ");
address = ScanHex (3);




printf ("\r\n\tEnter I/O data (Current: %2xH [%xH]): ",IO_ADDR (address), address);
ch = ScanHex (2);
IO_ADDR (address) = ch;
}
/****************************************************************/
// This displays the system prompt
/****************************************************************/
void prompt(void)
{
printf(PromptString);
}
/****************************************************************/
// This displays the list of valid commands.
// This is the "help" screen.
/****************************************************************/
void display_help(void)
{
puts("\r\n");
puts("\r H\tDisplay this help");
puts("\r V\tVersion");
puts("\r I\tInput RAM or I/O Port");
puts("\r O\tOutput RAM or I/O Port");
puts("\r R\tRead FLASH");
puts("\r W\tWrite FLASH");
puts("\r L\tLoad Hex File");
printf(" G\tExecute User Application code from %X H \n", USER_START_ADDR);
}
// Function to Execute User Application code from location JUMP_TO_USER_CODE
void execute_user_code(void)
{
asm(JUMP_TO_USER_CODE);
}
/****************************************************************/
// This parses the commands received from main, sorts
// through to determine what type of command it is, then calls
// the appropriate routine to handle it.
/****************************************************************/
void process_char(int c)
{
switch( c )
{
case  ? :
case  h :
case  H :
display_help();




break;
case  w :
case  W :
Write_Flash1();
break;
case  r :
case  R :
Read_Flash1();
break;
case  i :
case  I :
inputIOport ();
break;
case  o :
case  O :
outputIOport ();
break;
case  l :
case  L :
LoadHex();
break;
case  g :
case  G :
printf("\r\n Executing User Application code from %X H \n",
USER_START_ADDR);
execute_user_code();
break;
case  v :
case  V :
puts(VersionString);
break;
default:
break;
}
prompt();
}




utility.c : User Interface Utilities
/*************************************************
* Copyright (C) 1999-2002 by ZiLOG, Inc.
* All Rights Reserved
*
* utility.c : User Interface Utilities
**************************************************/
#include
#include
#include
#include "SIO.h"
#include
/****************************************************************/
// Clear Screen
/****************************************************************/
void clear_screen (void)
{
unsigned int i;
i=32;
while (i > 0)
{
printf ("\n");
i=i-1;
}
}
/****************************************************************/
// This converts an ascii hex character to it s binary value
// return -1 on error
/****************************************************************/
int ascii_to_hex(int c)
{
if((c >=  0 ) && (c <=  9 ))
return c -  0 ;
else if((c >=  A ) && (c <=  F ))
return c -  A + 10;
else if((c >=  a ) && (c <=  f ))
return c -  a + 10;
else
return -1;
}
/****************************************************************/
unsigned int ScanHex (unsigned char num)
{
unsigned char ch;
unsigned int addr;
int temp1, temp2, temp3, temp4;
do {
ch=getch ();
temp1 = ascii_to_hex (ch);
} while (temp1 < 0);
printf ("%X",temp1);
if (num == 1) {




ch = temp1 & 0xf;
return (ch);
}
do {
ch=getch ();
temp2 = ascii_to_hex (ch);
} while (temp2 < 0);
printf ("%X",temp2);
if (num == 2) {
ch = (temp1 << 4) & 0xF0;
ch = (ch | temp2) & 0xFF;
return (ch);
}
do {
ch=getch ();
temp3 = ascii_to_hex (ch);
} while (temp3 < 0);
printf ("%X",temp3);
if (num == 3) {
addr = 0x0000;
addr = (temp1 << 4) & 0x00F0;
addr = (addr | temp2);
addr = (addr << 4) & 0x0FF0;
addr = (addr | temp3) & 0x0FFF;
return (addr);
}
do {
ch=getch ();
temp4 = ascii_to_hex (ch);
} while (temp4 < 0);
printf ("%X",temp4);
addr = 0x0000;
addr = (temp1 << 4) & 0x00F0;
addr = (addr | temp2);
addr = (addr << 4) & 0x0FF0;
addr = (addr | temp3);
addr = (addr << 4) & 0xFFF0;
addr = (addr | temp4) & 0xFFFF;
return (addr);
}







Wyszukiwarka

Podobne podstrony:
Programowanie pamięci Flash mikrokontrolerów STM32 – Flash Loader
ferris mc flash for?rris mc
Brandy Corvin Howling for the Vampire
2007 01 Web Building the Aptana Free Developer Environment for Ajax
CSharp Introduction to C# Programming for the Microsoft NET Platform (Prerelease)
English for Medical S&D Practical English sentences key
plan for next iteration?CDF5AB
Burn Rate Models for Gun Propellants
Palmer relation between moral reasoning and agression, and implications for practice
62 FOR ostrzega Wprowadzenie klauzuli przeciwko unikaniu opodatkowania może być niezgodne z Konstytu
FOREX Systems Research Practical Fibonacci Methods For Forex Trading 2005
Cooking Homemade Recipes For Many Things

więcej podobnych podstron