MỤC LỤC
Mở đầu
Chương I Phân tích yêu cầu
1.1 Phân tích yêu cầu trang 5
1.2 Phương hướng giải quyết vấn đề trang 5
Chương II Thiết kế phần cứng
2.1. Phân tích các chức năng của cổng ghép nối với máy in trang 8
2.2. Kiến trúc Z80 CPU. trang 10
2.3. Thiết kế chi tiết. trang 48
2.4. Nguyên lý hoạt động. trang 49
Chương III. Giới thiệu phần soạn thảo văn bản
3.1. Các thành phần chính của main menu trang 55
3.2. Hướng dẫn sử dụng trong màn hình soạn thảo trang 55
Chương IV. Giải thuật trang 57
Chương V. Chương trình nguồn
5.1. Chương trình đưa data ra RAM chung, Debug, Dump, Run trang 71
5.2. Chương trình con viết bằng ngôn ngữ con Assembler Z80 dùng đọc trạng thái các thanh ghi trang 109
5.3. Chương trình con viết bằng ngôn ngữ con Assembler Z80 dùng dump memory trang 111
5.4. Chương trình tạo tiện ích soạn thảo văn bản vàhỗ trợ chế độ chạy debug Z80 trang 111
5.5. Các chương trình ví dụ trang 133
Phụ lục A: Các thông báo lỗi. trang 142
Phụ lục B: Vi mạch 8255. trang 143
Tài liệu tham khảo. trang 145
147 trang |
Chia sẻ: banmai | Lượt xem: 2006 | Lượt tải: 0
Bạn đang xem trước 20 trang tài liệu Thiết kế, thi công và viết chương trình điều khiển mạch thực hành cho Z80 CPU. Giao tiếp với máy tính qua ngõ máy in, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
ình ôû mode debug==*/
void msg_toolbar(void)
{
unsigned pos;
clear_screen(20,24,60,1,MENU_ATTR);
print_string(20,24,toolbar_debug,TEXT_ATTR);//Toolbar_debug.
set_attr_screen(0,24,80,1,MENU_ATTR);
print_string(67,24,"DEBUG_MODE",0x74);
clear_screen(14,24,4,1,MENU_ATTR); //Chæ soá haøng.
clear_screen(19,24,2,1,MENU_ATTR); //Chæ soá coät.
clear_screen(18,24,1,1,MENU_ATTR); //":"
}
/*==Chöông trình con in thoâng baùo keát thuùc run & debug==*/
void msg_finish(void)
{
clear_screen(11,4,58,15,MSG_ATTR); //1E
set_cursor_style(0x20,0x20);
set_attr_screen(21,9,38,4,ROSE_ATTR);
gotoxy(22,10); puts(" é");
gotoxy(22,11); puts("½ Finish, KIT going. ½ ");
gotoxy(22,12); puts("½ ESC-Return editor F3-Dump ½ ");
gotoxy(22,13); puts("ë ");
}
/*==Chöông trình con tìm chu kyø M1, traû veà ñ/c ñeå caáp cho Dump==*/
addr SearchM1(void)
{
byte FlagDump=0,i,k;
byte ErrorYes,BusyYes;
addr AddrDump;
/* 1. Tìm chu kyø M1 */
do
{
i=0;
do
{
ErrorYes=RDError();
if ( ErrorYes ) break;
delay(Set);
i++;
if ( ( i==10 ) && ( !RDError() ) ) { i=0; SetWAIT(); };
} while ( ErrorYes == 0 );
for (k=1;k<=3;k++)
{
BusyYes=RDBusy();
if ((BusyYes)&&(k>=2)) break; //Ñoïc 2 laàn ñeå choáng nhieãu
delay(Set);
};
if (BusyYes)
{
if (FlagDump) break;
}
else FlagDump=1;
SetWAIT();
} while (1);
AddrDump.lo=RD_LoAddr();
AddrDump.hi=RD_HiAddr();
return(AddrDump);
}
/*==Chöông trình con dump boä nhôù==*/
/*ñ/c nhaûy veà ñ/c nhaûy tôùi (baét ñaàu c/t dump)*/
byte Dump(byte LowAddr,byte HighAddr,byte RAddrLo,byte RAddrHi,
byte DStartLoAddr,byte DStartHiAddr,byte DEndLoAddr,byte DEndHiAddr)
/*ñ/c ñaàu boä nhôù ñ/c cuoái boä nhôù*/
{
int countTx=0;
int i,k;
byte dataDump;
byte ErrorYes,BusyYes;
byte FlagError=1,readFlag=0;
byte DisplayFlag=0;
int Hi,value;
byte valueLo,valueHi;
byte DataMemory;
unsigned Key;
int STT=0;
Hi=RAddrHi*256;
value=Hi+RAddrLo;
value +=0x0F; // 000F + RAddrHiRAddrLo: loop.
valueLo=value%256; // byte thaáp cuûa value.
valueHi=value/256; // byte cao cuûa value.
/* Phan 1: Xöû lyù 18 byte ñaàu tieân */
do // while ( countTx < countDump ) ( countDump=18 ).
{
countTx++;
i=0;
do
{
ErrorYes=RDError();
if ( ErrorYes ) break;
delay(Set);
i++;
if ( ( i==10 ) && ( !RDError() ) ) { i=0; SetWAIT(); }; //???
} while ( ErrorYes == 0 );
/* Neáu tröôùc ñoù coù ñöa data ra thì phaûi caám 244. */
if ( ( countTx == 2 ) || ( countTx == 3 ) || ( countTx == 4 ) ||
( countTx == 14 ) || ( countTx == 15 ) || ( countTx == 18 ) )
{
outportb(CTRLREG,InitCTRL);
delay(Set);
/* Cho pheùp RAM chung sau khi ñöa data ra */
if (( countTx == 4 )||( countTx == 15 )) EnableRAM();
};
for (k=1;k<=3;k++)
{
BusyYes=RDBusy();
if ((BusyYes)&&(k>=2)) break; //Ñoïc 2 laàn ñeå choáng nhieãu.
delay(Set);
};
/* Xöû lyù chu kyø laáy maõ leänh */
if ( BusyYes )
{
switch ( countTx )
{
case 1: SetWAITDebug(JUMP); break;
/*Caùc tröôøng hôïp ñoïc boä nhôù nhöng bò nhieãu (coù tín hieäu M1) */
case 2,3,13,14,17,18:
{
//Nhaûy veà ñaàu chöông trình
ErrorReg(LowAddr,HighAddr);
return(0);
}
default: SetWAIT();
}
/* Xöû lyù chu kyø ñoïc/ghi boä nhôù */
}
else // if ( BusyYes )
{
switch ( countTx )
{
case 2: SetWAITDebug(RAddrLo); break;
case 3: SetWAITDebug(RAddrHi); break;
case 13: SetWAITDebug(DEndLoAddr); break;
case 14: SetWAITDebug(DEndHiAddr); break;
case 17: SetWAITDebug(DStartLoAddr); break;
case 18: SetWAITDebug(DStartHiAddr); break;
/* byte DStartLoAddr,byte DStartHiAddr,byte DEndLoAddr,byte DEndHiAddr) */
/*ñ/c ñaàu boä nhôù ñ/c cuoái boä nhôù*/
default: SetWAIT(); // Error: Vì khoâng coù tröôøng hôïp khaùc.
}; //end switch
}; //end else
} while ( countTx < countDump ); // countDump=18.
/*Phaàn 2: xöû lyù voøng laëp (Start: 000F + RAddrHiRAddrLo)
Ñoïc maõ leänh C2, thay ñòa chæ 000F baèng ñòa chæ töông öùng (value).
Neáu coù tín hieäu error -> ñoïc data */
outportb(CTRLREG,InitCTRL);
delay(Set);
EnableRAM();
do // while (dataDump != C3)
{
do
{
ErrorYes=RDError();
if ( ErrorYes ) break;
delay(Set);
i++;
if ( ( i==10 ) && ( !RDError() ) ) { i=0; SetWAIT(); };
} while ( ErrorYes == 0 );
if ( ( readFlag==5 ) || ( readFlag==6 ) )
{
outportb(CTRLREG,InitCTRL);
delay(Set);
if (readFlag==6)
{
EnableRAM();
readFlag=0;
};
}
for (k=1;k<=3;k++)
{
BusyYes=RDBusy();
if ((BusyYes)&&(k>=2)) break; //Ñoïc 2 laàn ñeå choáng nhieãu.
delay(Set);
};
if ( BusyYes )
{
dataDump=CallRet_Ins(); // Ñoïc data töø bus.
if ( dataDump != 0xC3 ) // Maõ leänh JP.
{
if ( dataDump == 0x32 ) readFlag=1;
else if ( dataDump == 0xC2 ) readFlag=4;
SetWAIT();
}
else
{
SetWAIT();
break;
}
}
/* Xöû lyù chu kyø ñoïc boä nhôù */
else
{
switch (readFlag)
{
case 1: readFlag=2; SetWAIT(); break;
case 2: readFlag=3; SetWAIT(); break;
case 3:
{
if (!DisplayFlag)
{
readFlag=0;
DataMemory=ReadMem();
STT++;
if (STT>160)
{
STT=1;
Key=get_key();
while ( ( Key!=K_ENTER ) && ( Key != K_ESC ) )
{
Key=get_key();
};
if ( Key == K_ESC ) { DisplayFlag=1; msg_finish(); };
}
if (!DisplayFlag)
{
PrintMemory(DataMemory,STT,DStartLoAddr,DStartHiAddr);
if ( STT==1 ) STT = ( DStartLoAddr % 0x10 ) + 1;
if (DStartLoAddr == 0xFF)
{
DStartHiAddr++;
DStartLoAddr = 0x00;
}
else DStartLoAddr++;
}
SetWAIT(); break;
}
else { SetWAIT(); break; };
}
case 4: SetWAITDebug(valueLo); readFlag=5; break;
case 5: SetWAITDebug(valueHi); readFlag=6; break;
default: readFlag=0; SetWAIT();
};
} // end else
} while (1);
/* Phaàn 3: ñöa ñòa chæ trôû veà ra data bus */
do
{
ErrorYes=RDError();
if ( ErrorYes ) break;
delay(Set);
i++;
if ( ( i==10 ) && ( !RDError() ) ) { i=0; SetWAIT(); };
} while ( ErrorYes == 0 );
for (k=1;k<=3;k++)
{
BusyYes=RDBusy();
if ((BusyYes)&&(k>=2)) break; // Ñoïc 2 laàn ñeå choáng nhieãu.
delay(Set);
};
if (BusyYes)
{
ErrorReg(LowAddr,HighAddr);
return(0);
}
else SetWAITDebug(LowAddr);
do
{
ErrorYes=RDError();
if ( ErrorYes ) break;
delay(Set);
i++;
if ( ( i==10 ) && ( !RDError() ) ) { i=0; SetWAIT(); };
} while ( ErrorYes == 0 );
outportb(CTRLREG,InitCTRL);
delay(Set);
for (k=1;k<=3;k++)
{
BusyYes=RDBusy();
if ((BusyYes)&&(k>=2)) break; // Ñoïc 2 laàn ñeå choáng nhieãu.
delay(Set);
};
if (BusyYes)
{
ErrorReg(LowAddr,HighAddr);
return(0);
}
else SetWAITDebug(HighAddr);
outportb(CTRLREG,InitCTRL);
delay(Set);
EnableRAM();
return(FlagError);
}
/*==Chöông trình con ñoïc memory==*/
byte ReadMem(void)
{
byte value,lo,hi;
/*Ñoïc 4 bit thaáp*/
outportb(DataREG,RDREG); // Ñaët trôû laïi giaù trò ban ñaàu cho 374.
Latch374();
lo=ReadLow();
lo=lo & 0xF0; // 1111 0000: Xoaù 4 bit thaáp cuûa thanh ghi traïng thaùi.
lo=lo / 0x10; // Dòch phaûi 4 bit.
/*Ñoïc 4 bit cao*/
hi=ReadHigh();
hi=hi & 0xF0; // 1111 0000: Xoaù 4 bit thaáp cuûa thanh ghi traïng thaùi.
value=hi+lo;
delay(Set);
return(value);
}
/*==Chöông trình con in noäi dung memory==*/
void PrintMemory(byte DataDump,int STT,byte AddrLow,byte AddrHi)
{
set_cursor_style(0x20,0x20);
if ( STT==1 )
{
clear_screen(11,4,58,15,MSG_ATTR);
set_attr_screen(10,4,60,15,ROSE_ATTR);
Menu_Mem();
gotoxy(12,7);
if (AddrLow%16)
{
gotoxy(12,8);
printf("½ %02X",AddrHi);
printf("%02X ½ ",AddrLow-(AddrLow%16));
gotoxy(21+(3*(AddrLow%16)),8);
}
}
if ( ( AddrLow % 16 ) ) printf("%02X ",DataDump);
else
{
printf("\n ³ %02X",AddrHi);
printf("%02X ½ ",AddrLow);
printf("%02X ",DataDump);
}
}
/*==Chöông trình con in form==*/
void Menu_Mem(void)
{
gotoxy(11,5); puts(" é ");
gotoxy(11,6); puts("½ ADDR ½ The contents of memory ½ ");
gotoxy(11,7); puts(ӎ ");
gotoxy(11,8); puts(" ½ ½ ½ ");
gotoxy(11,9); puts(" ê ½ ½ ");
gotoxy(11,10); puts("½ ½ ½ ");
gotoxy(11,11); puts("½ ½ ½ ");
gotoxy(11,12); puts("½ ½ ½ ");
gotoxy(11,13); puts("½ ½ ½ ");
gotoxy(11,14); puts("½ ½ ½");
gotoxy(11,15); puts("½ ½ ½ ");
gotoxy(11,16); puts("½ ½ ½ ");
gotoxy(11,17); puts("½ ½ ½ ");
gotoxy(11,18); puts("½ ½ Enter - More F3 - Dump ESC - QUIT ½ ");
gotoxy(11,19); puts(“ë");
}
/*==Chöông trình con in form nhaäp ñ.chæ (first)==*/
addr input_addr_dump_start(void)
{
addr StartAddr;
clear_screen(10,4,60,15,MSG_ATTR);
set_attr_screen(26,10,28,3,ROSE_ATTR);
set_attr_screen(45,11,4,1,HIGH_ATTR);
gotoxy(28,11); puts(“ë Dumpû”);
gotoxy(28,12); puts("½ First address: ½");
gotoxy(28,13); puts (“ë");
gotoxy(46,12);
set_cursor_style(6,7);
scanf("%4X",&StartAddr);
return (StartAddr);
}
/*==Chöông trình con in form nhaäp ñ.chæ (last)==*/
addr input_addr_dump_end(void)
{
addr EndAddr;
set_attr_screen(26,10,28,3,ROSE_ATTR);
set_attr_screen(45,11,4,1,HIGH_ATTR);
gotoxy(28,11); puts("é Dumpù”);
gotoxy(28,12); puts("½ Last address: ½");
gotoxy(28,13); puts(“ë");
gotoxy(46,12);
set_cursor_style(6,7);
scanf("%4X",&EndAddr);
return (EndAddr);
}
/*==Chöông trình con baùo loãi ñ.chæ ñöa vaøo khoâng hôïp leä==*/
void msgError1(void)
{
clear_screen(10,4,60,15,MSG_ATTR);
set_attr_screen(26,10,28,3,RED_ATTR);
gotoxy(28,11); puts("é Error ù");
gotoxy(28,12); puts("½ Address invalid. ½");
gotoxy(28,13); puts(“ë");
set_cursor_style(0x20,0x20);
if (getch()==0) getch();
}
/*==Chöông trình con in thoâng baùo chôø trong khi xöû lyù dump memory==*/
void msgWait(void)
{
clear_screen(10,4,60,15,MSG_ATTR);
set_attr_screen(26,10,28,3,ROSE_ATTR);
gotoxy(28,11); puts("éMessagerù");
gotoxy(28,12); puts("½ Please wait ! ½");
gotoxy(28,13); puts(“ë");
set_cursor_style(0x20,0x20);
}
void ServiceCall(void)
{
byte ErrorYes,BusyYes,CycleM1;
byte j,k,InvalidM1=1,Flag=0;
do
{
j=0; // Chôø ñoïc tín hieäu ERROR.
do
{
ErrorYes=RDError(); // Ñoïc tín hieäu ERROR.
if ( ErrorYes ) break; // Neáu coù -> thoaùt.
delay(Set); // Neáu khoâng -> chôø 3 micro second.
j++; //Neáu chöa coù ERROR thì CPU coù theå ñang trong 2 case:
//1. Chu kyø I/O 2. Ñang bò treo
if ( ( j==10 ) && ( !RDError() ) ) { j=0; SetWAIT(); };
} while ( ErrorYes == 0 );
for (k=1;k<=3;k++)
{
BusyYes=RDBusy();
if ((BusyYes)&&(k>=2)) break;//Ñoïc 2 laàn ñeå choáng nhieãu
delay(Set);
};
if (BusyYes)
{
if ( InvalidM1 )
{
if (Flag) break;
CycleM1=FirstM1();
/*Ñaùnh daáu chu kyø M1 tieáp sau seõ khoâng hôïp leä*/
if ( CycleM1==1 ) InvalidM1=0;
else if (CycleM1==RET) Flag=1;
SetWAIT();
}
else if ( InvalidM1 == 0 )
{
InvalidM1=1;
SetWAIT();
};
}
else SetWAIT();
} while (1);
}
/*++++++++++++++++++++++++++++EOF++++++++++++++++++++++++++++*/
5.2. Chöông trình con vieát baèng ngoân ngöõ assembler Z80 duøng ñoïc traïng thaùi caùc thanh ghi.
;StartAddr (XX XX): ñ/c giaû (thuoäc RAM chung) ñeå xuaát
Mnemonics Opcodes
======== ======
push AF ;save AF F5
push BC ;save BC C5
ld (StartAddr),A ;out A 32 XX XX
ld A,B ;out B 78
ld (StartAddr),A 32 XX XX
ld A,C ;out C 79
ld (StartAddr),A 32 XX XX
ld A,D ;out D 7A
ld (StartAddr),A 32 XX XX
ld A,E ;out E 7B
ld (StartAddr),A 32 XX XX
ld A,H ;out H 7C
ld (StartAddr),A 32 XX XX
ld A,L ;out L 7D
ld (StartAddr),A 32 XX XX
ld A,I ;out I ED 57
ld (StartAddr),A 32 XX XX
ld A,R ;out R ED 5F
ld (StartAddr),A 32 XX XX
push AF ;out F F5
pop BC C1
ld A,C 79
ld (StartAddr),A 32 XX XX
push IX ;out X DD E5
pop BC C1
ld A,C 79
ld (StartAddr),A 32 XX XX
ld A,B 78
ld (StartAddr),A 32 XX XX
push IY ;out Y FD E5
pop BC C1
ld A,C 79
ld (StartAddr),A 32 XX XX
ld A,B 78
ld (StartAddr),A 32 XX XX
;
ex AF,AF' ;(AF)(AF') 08
exx ;(BC)(BC') D9
;(DE)(DE')
;(HL)(HL')
push AF ;save AF' F5
push BC ;save BC' C5
ld (StartAddr),A ;out A' 32 XX XX
ld A,B ;out B' 78
ld (StartAddr),A 32 XX XX
ld A,C ;out C' 79
ld (StartAddr),A 32 XX XX
ld A,D ;out D' 7A
ld (StartAddr),A 32 XX XX
ld A,E ;out E' 7B
ld (StartAddr),A 32 XX XX
ld A,H ;out H' 7C
ld (StartAddr),A 32 XX XX
ld A,L ;out L' 7D
ld (StartAddr),A 32 XX XX
push AF ;out F' F5
pop BC C1
ld A,C 79
ld (StartAddr),A 32 XX XX
pop BC ;restore BC' C1
pop AF ;restore AF' F1
ex AF,AF' ;restore AF' 08
exx ;restore BC',DE',HL' D9
ld (StartAddr),SP
pop BC ;restore BC C1
pop AF ;restore AF F1
;
jmp Addr ;nhay ve d/c xuat phat C3 YY YY
5.3. Chöông trình con vieát baèng ngoân ngöõ assembler Z80 duøng dump memory.
;Cuù phaùp: D XXXX YYYY, vôùi XXXX=1111, YYYY=FFFF
push af ;Save noäi dung caùc thanh ghi l.quan
push ix
push iy
push bc
push de
ld ix,0ffffh ;ñ/c cuoái
ld iy,1111h ;ñ/c ñaàu
loop:
ld a,(iy+00)
ld (0000h),a ;ñoïc veà PC taïi ñaây
inc iy
push iy
push ix
pop de
pop bc
ld a,c
cp e
jp nz,loop
ld a,b
cp d
jp nz,loop
pop de
pop bc
pop iy
pop ix
pop af
jp 0000h
5.4. Chöông trình taïo tieän ích soaïn thaûo vaên baûn & hoã trôï cheá ñoä chaïy Debug Z80.
#include
#include
#include
#include
#include
#include
#include
#include "keyboard.h"
#include "library.c"
#include "lvtn.h"
#define MAX_X 80
#define MAX_LINE 500
#define TEXT_ATTR 0x1E
#define MENU_ATTR 0x70
#define HIGH_ATTR 0x20
#define TOGGLE_ATTR 0x5F
#define NUM_ITEM 4
//#define NUM_FUNC 4
#define DEFAULT_FILENAME "NONAME.TXT"
#define SAVE 1
#define SAVE_AS 0
char menu_bar[NUM_ITEM][10]={" File ", " Run ", " Debug ", " Help "};
char tool_bar[57]={" F1 Help F7 Trace into F8 Step over F10 Menu "};
char text[MAX_LINE][MAX_X+1];
unsigned x, y, num_line, win_index, saved;
unsigned selection1=0, selection2=0,selection3=0,selection4=0,selection5=0;
int arr_bp=0,sub_bp=0;
char file_name[64];
char string_y[5],string_x[3];
/*
File Run Debug Help
½ New ½ Run Ctrl+F9 ½ Toggle breakpoint Ctrl+F8 ½ Introduction for use
½ Open... F3 ½ Trace into F7 ½ Delete breakpoint all ½ Instruction description ½
½ Save F2 ½ Step over F8
½ Save as... ½ ½ About... ½
½ Exit Alt+X ½
½ File name: noname.txt ½
/*Tao MENU */
void init_menu_file(void)
{
gotoxy(3,2); puts(“é ");
gotoxy(3,3); puts("½ New ½ ");
gotoxy(3,4); puts("½ Open... F3 ½ ");
gotoxy(3,5); puts("½ Save F2 ½ ");
gotoxy(3,6); puts("½ Save as... ½");
gotoxy(3,7); puts("½");
gotoxy(3,8); puts("½ Exit Alt+X ½ ");
gotoxy(3,9); puts("ë ");
set_attr_screen(2,1,17,8,MENU_ATTR); //Draw toolbox File
} //2,1,14,7,
/////////////////////////////////////////////////////////////////////
void init_menu_run(void)
{
gotoxy(11,2); puts (“é");
gotoxy(11,3); puts("½ Run Ctrl+F9 ½ ");
gotoxy(11,4); puts("½ Trace into F7 ½ ");
gotoxy(11,5); puts("½ Step over F8 ½");
gotoxy(11,6); puts("ë ");
set_attr_screen(10,1,20,5,MENU_ATTR); //Draw toolbox File
}
/////////////////////////////////////////////////////////////////////
void init_menu_debug(void)
{
gotoxy(18,2); puts(" é");
gotoxy(18,3); puts("½ Toggle breakpoint Ctrl+F8 ½ ");
gotoxy(18,4); puts("½ Delete breakpoint all ½ ");
gotoxy(18,5); puts(" ë");
set_attr_screen(17,1,34,4,MENU_ATTR); //Draw toolbox File
}
/////////////////////////////////////////////////////////////////////
void init_menu_help(void)
{
gotoxy(27,2); puts(" é ");
gotoxy(27,3); puts("½ Introduction for use ½ ");
gotoxy(27,4); puts("½ Instruction description ½ ");
gotoxy(27,6); puts("½ About... ½ ");
gotoxy(27,7); puts(" ë”);
set_attr_screen(26,1,29,6,MENU_ATTR); //Draw toolbox File
}
/////////////////////////////////////////////////////////////////////
void new_file(void)
{
for(register i=0; i<MAX_LINE; i++) text[i][0]=0;
x=0; y=0; num_line=1; win_index=0; saved=0;
strcpy(file_name,DEFAULT_FILENAME);
} /* DEFAULT_FILENAME "NONAME.TXT" */
/////////////////////////////////////////////////////////////////////
void init_program(void)
{ /* x,y,width,hight,attr*/
unsigned po1;
clear_screen(0,0,80,1,MENU_ATTR); //Xoaù thanh ngang phía treân
clear_screen(0,24,80,1,MENU_ATTR); //Xoaù thanh ngang phía döôùi
new_file(); //=3
for( register i=0,x=1; i<NUM_ITEM; i++)
{
gotoxy(x+1,1); //Ñöa File Run Debug leân thanh ngang treân
puts(menu_bar[i]);
x += strlen(menu_bar[i]);
}
x=0;
print_string(22,24,tool_bar,TEXT_ATTR); //Tool_bar
set_attr_screen(0,24,80,1,MENU_ATTR);
clear_screen(14,24,4,1,MENU_ATTR); //Chæ soá haøng
itoa(win_index+y+1, string_y, 10);
pos=strlen(string_y);
print_string(18-pos,24,string_y,0x74);
clear_screen(19,24,2,1,MENU_ATTR); //Chi so cot
itoa(x+1, string_x, 10);
print_string(19,24,string_x,0x74);
clear_screen(18,24,1,1,MENU_ATTR); //":"
print_string(18,24,":",0x74);
clear_screen(0,24,13,1,MENU_ATTR);
print_string(1,24,file_name,0x74); //In filename cuoái maøn hình.
}
/////////////////////////////////////////////////////////////////////
/*===========================Doc ten file=========================*/
unsigned read_string(unsigned x, unsigned y, unsigned win_width,
unsigned max_len, unsigned attr, char s[])
{
unsigned key,len=strlen(s), cursor_pos, win_pos=0, i;
if (len+1>win_width) cursor_pos=0;
else cursor_pos=len;
set_attr_screen(x,y,win_width,1,attr);
do
{ //top, bottom
set_cursor_style(0x20,0x20);
len=strlen(s);
for( i=0; (i<win_width-1)&&(i<len); i++)
*(char far *)MK_FP(0xB800, y*160+x+x+i+i) = s[win_pos+i];
for( ; i<win_width; i++)
*(char far *)MK_FP(0xB800, y*160+x+x+i+i) = ' ';
gotoxy(x+cursor_pos+1, y+1);
set_cursor_style(6,7);
key = get_key();
switch (key)
{
case K_ENTER:
case K_ESC:
return key;
case K_LEFT:
if (cursor_pos>0) cursor_pos--;
else if (win_pos>0) win_pos--;
break ;
case K_RIGHT:
if ((win_pos+cursor_pos)!=len)
{
if (cursor_pos<win_width-1) cursor_pos++ ;
else if (win_pos+win_width-1<max_len) win_pos++ ;
}
break ;
case K_END:
if ((len>win_width-1)&&(len-win_pos>=win_width-1))
{
cursor_pos = win_width-1;
win_pos = len-win_width+1;
}
else cursor_pos = len-win_pos;
break ;
case K_HOME:
cursor_pos=0;
win_pos=0;
break;
case K_DEL:
for(i=cursor_pos+win_pos; i<len; i++) s[i]=s[i+1];
break ;
case K_BACK_SPACE:
if (cursor_pos+win_pos>0)
{
for(i=cursor_pos+win_pos-1; i<len; i++) s[i]=s[i+1];
if (cursor_pos==0) win_pos--;
else cursor_pos--;
}
break ;
default:
if ((key<0xFF)&&(len<max_len)&&(key!=K_ESC))
{
for(i=len+1; i>cursor_pos+win_pos; i--) s[i]=s[i-1];
s[i]=key;
if (cursor_pos==win_width-1) win_pos++;
else cursor_pos++;
}
break;
} //end switch
} while (1) ; //end do
}
/////////////////////////////////////////////////////////////////////
void K_UP_func(void)
{
unsigned pos;
if (y>0) y--;
else if (win_index>0)
{
win_index--;
scroll_up(0,1,80,23,TEXT_ATTR);
print_string(0,1,text[win_index],TEXT_ATTR);
}
clear_screen(14,24,4,1,MENU_ATTR);
itoa(win_index+y+1, string_y, 10);
pos=strlen(string_y);
print_string(18-pos,24,string_y,0x74);
}
/////////////////////////////////////////////////////////////////////
void K_DOWN_func(void)
{
unsigned pos;
if (win_index+y==num_line-1) return;
if (y<22) y++;
else if (win_index+23<num_line)
{
win_index++;
scroll_down(0,1,80,23,TEXT_ATTR);
if (win_index+22<num_line)
print_string(0,23,text[win_index+22],TEXT_ATTR);
}
clear_screen(14,24,4,1,MENU_ATTR);
itoa(win_index+y+1, string_y, 10);
pos=strlen(string_y);
print_string(18-pos,24,string_y,0x74);
}
/////////////////////////////////////////////////////////////////////
void K_LEFT_func(void)
{
clear_screen(19,24,2,1,MENU_ATTR);
itoa(x+1, string_x, 10);
print_string(19,24,string_x,0x74);
}
/////////////////////////////////////////////////////////////////////
void K_RIGHT_func(void)
{
clear_screen(19,24,2,1,MENU_ATTR);
itoa(x+1, string_x, 10);
print_string(19,24,string_x,0x74);
}
/////////////////////////////////////////////////////////////////////
void K_HOME_func(void)
{
clear_screen(19,24,2,1,MENU_ATTR);
itoa(x+1, string_x, 10);
print_string(19,24,string_x,0x74);
}
/////////////////////////////////////////////////////////////////////
void K_END_func(void)
{
clear_screen(19,24,2,1,MENU_ATTR);
itoa(x+1, string_x, 10);
print_string(19,24,string_x,0x74);
}
/////////////////////////////////////////////////////////////////////
unsigned K_DEL_func(void) {
if (x<strlen(text[win_index+y])) {
register len = strlen(text[win_index+y])-1;
for( register i=x; i<len; i++)
text[win_index+y][i]=text[win_index+y][i+1];
text[win_index+y][i]=0;
*(char far *)MK_FP(0xB800, y*160+i+i+160) = ' ';
print_string(0,y+1,text[win_index+y],TEXT_ATTR);
}
else if (win_index+y<num_line-1) {
if (x+strlen(text[win_index+y+1])>MAX_X) return 1;
else {
if (text[win_index+y+1][0]!=0) {
for(register i=strlen(text[win_index+y]); i<x; i++)
text[win_index+y][i]=' ';
memmove(text[win_index+y]+i,
text[win_index+y+1], strlen(text[win_index+y+1])+1);
}
memmove(text[win_index+y+1], text[win_index+y+2],
(num_line-1-(win_index+y+1))*(MAX_X+1));
num_line--;
text[num_line][0]=0;
print_string(x,y+1,text[win_index+y]+x,TEXT_ATTR);
scroll_down(0,y+2,80,22-y,TEXT_ATTR);
if (win_index+22<=num_line-1)
print_string(0,23,text[win_index+22],TEXT_ATTR);
}
}
saved=0;
return 0;
}
/////////////////////////////////////////////////////////////////////
void update_screen(void)
{
unsigned i,pos;
char string[5];
clear_screen(0,1,80,23,TEXT_ATTR);
for(i=0; i<23; i++)
print_string(0,i+1,text[win_index+i],TEXT_ATTR);
//Tam thoi set dong dau tien
if (sub_bp) set_attr_screen(0, sub_bp-win_index, 80, 1, TOGGLE_ATTR);
clear_screen(22,24,60,1,MENU_ATTR);
print_string(22,24,tool_bar,TEXT_ATTR); //Tool_bar
set_attr_screen(0,24,80,1,MENU_ATTR);
clear_screen(14,24,4,1,MENU_ATTR); //Chæ soá haøng
itoa(win_index+y+1, string_y, 10);
pos=strlen(string_y);
print_string(18-pos,24,string_y,0x74);
clear_screen(19,24,2,1,MENU_ATTR); //Chæ soá coät
itoa(x+1, string_x, 10);
print_string(19,24,string_x,0x74);
clear_screen(18,24,1,1,MENU_ATTR); //":"
print_string(18,24,":",0x74);
clear_screen(0,24,13,1,MENU_ATTR);
print_string(1,24,file_name,0x74); //In filename cuoái maøn hình.
}
/////////////////////////////////////////////////////////////////////
/*Laøm vieäc vôùi maøn hình soaïn thaûo*/
unsigned edit_text(void)
{
unsigned key, i;
update_screen();
set_cursor_style(6,7);
do
{
gotoxy(x+1,y+2); // 1, 2
key = get_key(); //Read keyboard
switch (key)
{ //Xöû lyù caùc phím ñieàu khieån
case K_ALT_X:
case K_F10:
return key;
case K_F1:
return key;
case K_F2:
return key;
case K_F3:
return key;
case K_F7:
return key;
case K_F8:
return key;
case K_CTRL_F9:
return key;
case K_CTRL_F8:
return key;
case K_ESC:
break;
case K_LEFT:
if (x>0) x--; K_LEFT_func(); break;
case K_RIGHT:
if (x<MAX_X-1) x++; K_RIGHT_func(); break;
case K_UP:
K_UP_func(); break;
case K_DOWN:
K_DOWN_func(); break;
case K_HOME:
x = 0; K_HOME_func(); break;
case K_END:
x = strlen(text[win_index+y]);
if (x==MAX_X) x--; K_END_func(); break;
case K_DEL:
K_DEL_func(); break;
case K_BACK_SPACE:
if (x>strlen(text[win_index+y])) x--;
else if (x>0)
{
x--;
K_DEL_func();
}
else if (win_index+y!=0)
{
K_UP_func();
x = strlen(text[win_index+y]);
if (K_DEL_func()!=0)
{
x = 0;
K_DOWN_func();
}
}
break;
case K_PAGEUP:
if (win_index!=0)
{
if (win_index<22) win_index=y=0;
else win_index-=22;
update_screen();
}
else y=0;
break;
case K_PAGEDOWN:
if (win_index+22>num_line-1) win_index=num_line-1;
else (win_index+=22);
if (win_index+y>num_line-1) y=num_line-1-win_index;
update_screen();
break;
case K_ENTER:
if (win_index+y<MAX_LINE-1)
{
saved=0;
if (num_line-1>win_index+y)
memmove(text[win_index+y+2], text[win_index+y+1], (MAX_X+1)*(num_line-win_index-y-1));
scroll_up(0,y+2,80,22-y,TEXT_ATTR);
if (strlen(text[win_index+y])<=x)
text[win_index+y+1][0]=0;
else
{
strcpy(text[win_index+y+1],text[win_index+y]+x);
text[win_index+y][x]=0;
for(i=x; i<MAX_X; i++)
*(char far *)MK_FP(0xB800, y*160+i+i+160) = ' ';
if (y<22) print_string(0,y+2,text[win_index+y+1],TEXT_ATTR);
}
num_line++;
x = 0;
K_DOWN_func();
}
break;
default: //Xöû lyù caùc phím kyù töï
if ((key&0xFF)==0) break;
if ((key&0xFF)=='\t') key=' ';
register len=strlen(text[win_index+y]);
if (len<x)
{
saved=0;
*(char far *)MK_FP(0xB800, y*160+x+x+160) = key;
memset(text[win_index+y]+len,' ',x-len);
text[win_index+y][x]=key;
text[win_index+y][x+1]=0;
if (x<MAX_X-1) x++;
}
else if (len<MAX_X)
{
saved=0;
text[win_index+y][strlen(text[win_index+y])+1]=0;
for(i=strlen(text[win_index+y]); i>x; i--)
{
text[win_index+y][i]=text[win_index+y][i-1];
*(char far *)MK_FP(0xB800, y*160+i+i+160) = text[win_index+y][i];
}
text[win_index+y][x]=key;
*(char far *)MK_FP(0xB800, y*160+x+x+160) = key;
if (x<MAX_X-1) x++;
}
break;
} //end switch
} while (1); //end do
}
/////////////////////////////////////////////////////////////////////
unsigned input_file_name(void) {
char str[64];
strcpy(str,file_name);
set_attr_screen(24,11,32,3,0x5F);
gotoxy(25,12); puts("éOpen a Fileù");
gotoxy(25,13); puts("½ File name: ½");
if (read_string(40,12,14,60,0x5F,str)==K_ESC) return 1;
strcpy(file_name,str);
return 0;
}
/////////////////////////////////////////////////////////////////////
unsigned save_file_name(void) {
char str[64];
strcpy(str,file_name);
set_attr_screen(24,11,32,3,0x5F);
gotoxy(25,12); puts (“Save File As ");
gotoxy(25,13); puts("½ File name: ½");
if (read_string(40,12,14,60,0x5F,str)==K_ESC) return 1;
strcpy(file_name,str);
return 0;
}
/////////////////////////////////////////////////////////////////////
void open_error(void) {
set_cursor_style(0x20,0x20);
set_attr_screen(23,11,33,3,0x4F);
gotoxy(24,12); puts(“ éError ù");
gotoxy(24,13); puts("½ Can not open file ! ½ ");
if (getch()==0) getch();
}
/////////////////////////////////////////////////////////////////////
unsigned overwrite_file(void) {
unsigned key;
set_cursor_style(0x20,0x20);
set_attr_screen(14,11,50,3,0x4F);
gotoxy(15,12); puts("éMessageù”);
gotoxy(15,13); puts("½ File is already exist. Overwrite (Y/N) ½ ");
do key=get_key();
while ((key!='Y')&&(key!='y')&&(key!='N')&&(key!='n'));
if ((key=='Y')||(key=='y')) return 1;
else return 0;
}
/////////////////////////////////////////////////////////////////////
void error_filename(void)
{
set_cursor_style(0x20,0x20);
set_attr_screen(23,11,38,4,0x4F);
gotoxy(24,13); puts("½ ERROR: ½");
gotoxy(24,14); puts("½ Filename may be: filename.asm ½ ");
if (getch()==0) getch();
}
/////////////////////////////////////////////////////////////////////
void open(void)
{
FILE *fptr;
char str[64];
strcpy(tr,file_name);
if (input_file_name()!=0) return;
strupr(file_name); //Ñoåi teân file thaønh chöõ hoa
fptr = fopen(file_name,"rt");
if (fptr!=NULL)
{ //MAX_LINE=500, MAX_X=80
for(register i=0; i<MAX_LINE; i++) text[i][0]=0;
i=0;
fgets(text[i],MAX_X,fptr);
while (!feof(fptr))
{
text[i][strlen(text[i])-1]=0;
i++;
fgets(text[i],MAX_X,fptr);
}
num_line=i+1;
for(i=0; i<num_line; i++)
for(register j=0; j<MAX_X; j++)
if (text[i][j]=='\t') text[i][j]=' ';
fclose(fptr);
x=y=win_index=0;
saved=1;
}
else
{
open_error();
strcpy(file_name,str);
}
}
/////////////////////////////////////////////////////////////////////
void save(unsigned option) {
FILE *fptr;
char str[64];
strcpy(str,file_name);
if ((option==SAVE_AS)||(strcmp(file_name,DEFAULT_FILENAME)==0)) {
if (save_file_name()!=0) return;
if (access(file_name,0)==0) if (overwrite_file()==0) {
strcpy(file_name,str);
return;
}
}
strupr(file_name); //Ñoåi teân file thaønh chöõ hoa
fptr = fopen(file_name,"wt");
if (fptr!=NULL) {
fputs(text[0], fptr);
for(register i=1; i<num_line; i++) {
fputc('\n',fptr);
fputs(text[i], fptr);
}
fclose(fptr);
saved=1;
}
else {
open_error();
strcpy(file_name,str);
}
}
/////////////////////////////////////////////////////////////////////
void spawnle_example(void)
{
int result;
result = spawnle(P_WAIT, "dz80.exe", NULL, NULL);
if (result == -1)
{
perror("Error from spawnle");
exit(1);
}
}
/////////////////////////////////////////////////////////////////////
/*==Dich, dua chuong trinh ra RAM chung & Run==*/
void run(void)
{
int n;
char tenfile[64];
//Goi dz80
spawnle_example();
update_screen();
clear_screen(0,0,80,1,MENU_ATTR); //Xoaù thanh ngang phía treân
for( register i=0,x=1; i<NUM_ITEM; i++)
{
gotoxy(x+1,1); //Ñöa File Run Debug leân thanh ngang treân
puts(menu_bar[i]);
x += strlen(menu_bar[i]);
}
//Tim chuoi ".asm" trong filename, AddPoint -> '.' (neáu tìm thaáy)
if (strstr(file_name,".ASM")==NULL) error_filename();
else
{
//Thay file.asm (file_name) = file.bin
n=strlen(file_name);
memcpy(tenfile,file_name,n-3);
tenfile[n-3]=0X00;
strcat(tenfile,"bin");
main1(tenfile,2,0);
//Sau khi thöïc hieän xong main 1 phaûi traû laïi maøn hình cuõ
}
}
/////////////////////////////////////////////////////////////////////
/*==Dòch, ñöa chöông trình ra RAM chung & Debug==*/
void debug(int key)
{
int n;
char tenfile[64];
//Goi dz80
spawnle_example();
update_screen();
clear_screen(0,0,80,1,MENU_ATTR); //Xoaù thanh ngang phía treân
for( register i=0,x=1; i<NUM_ITEM; i++)
{
gotoxy(x+1,1); //Ñöa File Run Debug leân thanh ngang treân
puts(menu_bar[i]);
x += strlen(menu_bar[i]);
}
//Tim chuoi ".asm" trong filename, AddPoint -> '.' (neáu tìm thaáy)
if (strstr(file_name,".ASM")==NULL) error_filename();
else
{
//Thay file.asm (file_name) = file.bin
n=strlen(file_name);
memcpy(tenfile,file_name,n-3);
tenfile[n-3]=0X00;
strcat(tenfile,"bin");
main1(tenfile,1,key);
}
}
/////////////////////////////////////////////////////////////////////
unsigned main_menu(void)
{
unsigned key;
set_cursor_style(0x20,0x20);
do
{
for(register i=0,x=1; i<selection1; i++) x+=strlen(menu_bar[i]);
set_attr_screen(x+1, 0, strlen(menu_bar[selection1])-2, 1, HIGH_ATTR);
key = get_key();
if ((key==K_DOWN)&&(selection1==0)) key=K_ENTER;
if ((key==K_ENTER)||(key==K_ESC)||(key==K_ALT_X)||
(key==K_F1)||(key==K_F2)||(key==K_F3) ||(key==K_CTRL_F9)||
(key==K_F7)||(key==K_F8)||(key==K_CTRL_F8) ) return key;
set_attr_screen(x+1, 0, strlen(menu_bar[selection1])-2, 1, MENU_ATTR);
if (key==K_LEFT)
if (selection1==0) selection1=NUM_ITEM-1;
else selection1--;
else if (key==K_RIGHT)
if (selection1==NUM_ITEM-1) selection1=0 ;
else selection1++;
} while (1);
}
/////////////////////////////////////////////////////////////////////
/*==Xöû lyù caùc phím: Enter, Esc, Alt_x, muõi teân leân, muõi teân xuoáng==*/
unsigned menu_file(void)
{
unsigned key;
init_menu_file();
do
{
set_attr_screen(4,selection2+2,13,1,HIGH_ATTR);
key = get_key();
if ((key==K_ENTER)||(key==K_ESC)||(key==K_ALT_X)||(key==K_LEFT)||
(key==K_F7)||(key==K_F8) ||(key==K_CTRL_F9)||(key==K_RIGHT)||
(key==K_F1)||(key==K_F2)||(key==K_F3)||(key==K_CTRL_F8) ) break;
set_attr_screen(4,selection2+2,13,1,MENU_ATTR);
if (key==K_UP)
if (selection2==0) selection2=5;
else if (selection2==5) selection2=3;
else selection2--;
else if (key==K_DOWN)
if (selection2==5) selection2=0 ;
else if (selection2==3) selection2=5;
else selection2++;
} while (1);
update_screen();
return key;
}
/////////////////////////////////////////////////////////////////////
unsigned menu_run(void)
{
unsigned key;
init_menu_run();
do
{
set_attr_screen(12,selection3+2,16,1,HIGH_ATTR);
key = get_key();
if ((key==K_ENTER)||(key==K_ESC)||(key==K_ALT_X)||(key==K_LEFT)||
(key==K_F1)||(key==K_F2)||(key==K_F3) ||(key==K_CTRL_F9)||
(key==K_RIGHT)||(key==K_F7)||(key==K_F8)||(key==K_CTRL_F8) ) break;
set_attr_screen(12,selection3+2,16,1,MENU_ATTR);
if (key==K_UP)
if (selection3==0) selection3=2;
else selection3--;
else if (key==K_DOWN)
if (selection3==2) selection3=0 ;
else selection3++;
} while (1);
update_screen();
return key;
}
/////////////////////////////////////////////////////////////////////
unsigned menu_debug(void)
{
unsigned key;
init_menu_debug();
do
{
set_attr_screen(19,selection4+2,30,1,HIGH_ATTR);
key = get_key();
if ((key==K_ENTER)||(key==K_ESC)||(key==K_ALT_X)||(key==K_LEFT)||
(key==K_F7)||(key==K_F8) ||(key==K_CTRL_F9)||(key==K_RIGHT)||
(key==K_F1)||(key==K_F2)||(key==K_F3)||(key==K_CTRL_F8) ) break;
set_attr_screen(19,selection4+2,30,1,MENU_ATTR);
if (key==K_UP)
if (selection4==0) selection4=1;
else selection4--;
else if (key==K_DOWN)
if (selection4==1) selection4=0 ;
else selection4++;
} while (1);
update_screen();
return key;
}
/////////////////////////////////////////////////////////////////////
unsigned menu_help(void)
{
unsigned key;
init_menu_help();
do
{
set_attr_screen(28,selection5+2,25,1,HIGH_ATTR);
key = get_key();
if ((key==K_ENTER)||(key==K_ESC)||(key==K_ALT_X)||(key==K_LEFT)||
(key==K_F7)||(key==K_F8) ||(key==K_CTRL_F9)||(key==K_RIGHT)||
(key==K_F1)||(key==K_F2)||(key==K_F3)||(key==K_CTRL_F8) ) break;
set_attr_screen(28,selection5+2,25,1,MENU_ATTR);
if (key==K_UP)
if (selection5==0) selection5=3;
else if (selection5==3) selection5=1;
else selection5--;
else if (key==K_DOWN)
if (selection5==3) selection5=0 ;
else if (selection5==1) selection5=3;
else selection5++;
} while (1);
update_screen();
return key;
}
/////////////////////////////////////////////////////////////////////
void conv_attr(unsigned order_menu)
{
for(register i=0,x=1; i<selection1; i++) x+=strlen(menu_bar[i]);
set_attr_screen(x+1, 0, strlen(menu_bar[selection1])-2, 1, HIGH_ATTR);
for(i=0,x=1; i<order_menu; i++) x+=strlen(menu_bar[i]);
set_attr_screen(x+1, 0, strlen(menu_bar[order_menu])-2, 1, MENU_ATTR);
}
/////////////////////////////////////////////////////////////////////
void toggle(void)
{
int sub_arr_bp;
if (win_index+y+1 == sub_bp)
{
set_attr_screen(0, y+1, 80, 1, TEXT_ATTR);
arr_bp=0;
sub_bp=0;
}
else
{
set_attr_screen(0, y+1, 80, 1, TOGGLE_ATTR);
arr_bp=win_index+y+1; //traû veà soá doøng treân file *.asm
sub_bp=win_index+y+1;
sub_arr_bp=(arr_bp-1)/60*2;
arr_bp=arr_bp+sub_arr_bp; //Doøng t.öùng trong file *.lst
//*.asm -> *.lst: line=line+(line-1)/60*2
}
}
/////////////////////////////////////////////////////////////////////
void About(void)
{
set_cursor_style(0x20,0x20);
set_attr_screen(21,6,38,14,0x5F);
set_attr_screen(36,18,8,1,MENU_ATTR);
gotoxy(22,7); puts (“ éAbout ù");
gotoxy(22,8); puts("½ Truong Dai Hoc Ky Thuat. ½ ");
gotoxy(22,9); puts("½ Khoa cong nghe thong tin. ½ ");
gotoxy(22,10); puts("½ ½ ");
gotoxy(22,11); puts("½ LUAN VAN TOT NGHIEP ½ ");
gotoxy(22,12); puts("½ ½ ");
gotoxy(22,13); puts("½ Giao vien huong dan: ½ ");
gotoxy(22,14); puts("½ Thay Nguyen Xuan Minh. ½ ");
gotoxy(22,15); puts("½ Sinh vien thuc hien: ½ ");
gotoxy(22,16); puts("½ Luu Dinh Dung. ½ ");
gotoxy(22,17); puts("½ Hoang Le Binh. ½ ");
gotoxy(22,18); puts("½ Lop: KSII-K6-T. ½ ");
gotoxy(22,19); puts("½ OK ½ ");
if (getch()==0) getch();
}
/////////////////////////////////////////////////////////////////////
void Instruction(void)
{
unsigned count_line=0,phim;
char help_text[81];//Nho hon 80
FILE *fp;
fp = fopen("help.hlp","rt");
if (fp!=NULL)
{
set_cursor_style(0x20,0x20);
clear_screen(1,1,78,22,0x5F);
gotoxy(4,2); puts(" Instruction Z80 ");
gotoxy(4,24); puts(" Enter - More ESC - Exit ");
set_attr_screen(1,1,78,1,0x4F);
set_attr_screen(1,23,78,1,0x4F);
while ( !feof(fp) )
{
if (count_line==20)
{
phim=get_key();
while ( ( phim != K_ENTER ) && ( phim != K_ESC ) ) phim=get_key();
if (phim==K_ESC) break;
else count_line=0;
clear_screen(1,2,76,20,0x5F);
}
fgets(help_text,80,fp);
print_string(2,count_line+2,help_text,0x5F);
count_line++;
}
fclose(fp);
}
else
{
set_cursor_style(0x20,0x20);
set_attr_screen(23,11,33,3,0x4F);
gotoxy(24,12); puts("éErrorù”);
gotoxy(24,13); puts("½ Can not open file help.hlp! ½ ");
if (getch()==0) getch();
}
// phim=get_key();
while ( phim != K_ESC ) phim=get_key();
update_screen();
}
/////////////////////////////////////////////////////////////////////
void Intro(void)
{
set_cursor_style(0x20,0x20);
set_attr_screen(21,6,38,14,0x5F);
set_attr_screen(36,18,8,1,MENU_ATTR);
gotoxy(22,7); puts("éTrouble shoottingù");
gotoxy(22,8); puts("½ 1. Ram 's fail: Replace. ½ ");
gotoxy(22,9); puts("½ 2. Power off: Power on. ½ ");
gotoxy(22,10); puts("½ 3. Jack 25 pin: ½ ");
gotoxy(22,11); puts("½ - Check jack 25 or connect to PC. ½ ");
gotoxy(22,12); puts("½ 4. File 's too long: ½ ");
gotoxy(22,13); puts("½ - File (*.bin) may be < 32Kb. ½ ");
gotoxy(22,14); puts("½ 5. Hard wave error: ½ ");
gotoxy(22,15); puts("½ - Check 74LS: 157,244,374,8255... ½ ");
gotoxy(22,16); puts("½ 6. Invalid breakpoint: ½ ");
gotoxy(22,17); puts("½ - Set breakpoint again. ½ ");
gotoxy(22,18); puts("½ - Possible is incorrect. ½ ");
gotoxy(22,19); puts("½ OK ½ ");
if (getch()==0) getch();
}
/////////////////////////////////////////////////////////////////////
void delToggle(void)
{
set_attr_screen(0, 1, 80, 23, TEXT_ATTR);
arr_bp=0;
sub_bp=0;
}
/////////////////////////////////////////////////////////////////////
unsigned func_func(unsigned key)
{
switch (key)
{
case K_F1: About(); return (1);
case K_F2: save(SAVE); return (1);
case K_F3: open(); return (1);
case K_F7: debug(K_F7); return (1);
case K_F8: debug(K_F8); return (1);
case K_CTRL_F8: toggle(); return (1);
case K_CTRL_F9: run(); return (1);
case K_ESC: return (1);
default: return 0;
}
}
////////////////////////////////////////////////////////////////////
void main(void)
{
unsigned key,pos;
int number;
init_program();
do
{
set_attr_screen(0,0,80,1,MENU_ATTR);
//Xu ly trong man hinh soan thao
key = edit_text(); //return F10
if (key==K_ALT_X) break; //Alt + X = Exit
if ( !(func_func(key) ) )
{
key = main_menu();
if (key==K_ALT_X) break;
if ( !(func_func(key) ) )
if (key==K_ENTER) //(1)
{
do
{
if (selection1==0) //Select File menu
{
key = menu_file();
if (key==K_ALT_X) break;
else if (key==K_RIGHT) selection1=1;
else if (key==K_LEFT) selection1=3;
conv_attr(0);
}
else if (selection1==1)
{
key = menu_run();
if (key==K_ALT_X) break;
else if (key==K_LEFT) selection1=0;
else if (key==K_RIGHT) selection1=2;
conv_attr(1);
}
else if (selection1==2)
{
key = menu_debug();
if (key==K_ALT_X) break;
else if (key==K_LEFT) selection1=1;
else if (key==K_RIGHT) selection1=3;
conv_attr(2);
}
else if (selection1==3)
{
key = menu_help();
if (key==K_ALT_X) break;
else if (key==K_LEFT) selection1=2;
else if (key==K_RIGHT) selection1=0;
conv_attr(3);
}
} while ( (!(key == K_ENTER)) && ( !(func_func(key)) ) );
if (key==K_ENTER) //(2)
{
if (selection1==0)
{
if (selection2==5) break;
else if (selection2==0) new_file();
else if (selection2==1) open();
else if (selection2==2) save(SAVE);
else save(SAVE_AS);
}
else if (selection1==1)
{
if (selection3==0) run();
else if (selection3==1) debug(K_F7);
else if (selection3==2) debug(K_F8);
}
else if (selection1==2)
{
if (selection4==0) toggle();
else if (selection4==1) delToggle();
}
else if (selection1==3)
{
if (selection5==0) Intro();
else if (selection5==1) Instruction();
else if (selection5==3) About();
}
} //end if (key==K_ENTER) (2)
} //end if (key==K_ENTER) (1)
} //end if (!(func_func()))
} while (1);
clear_screen(0,0,80,25,0x07);
gotoxy(1,1);
set_cursor_style(6,7);
}
/*========================END OF FILE==========================*/
5.5. Caùc chöông trình ví duï.
5.5.1. Chöông trình ví duï No.1.
; Ñoïc traïng thaùi SW.
; Xuaát traïng thaùi töông öùng ra LED.
; Ngöôøi vieát: Löu Ñình Duõng.
; Ngaøy vieát: 11/03/1999.
; Caäp nhaät laàn cuoái:09/07/1999.
org 0000h
ld sp,0fe00h
start:
in a,(04h)
out (00h),a
jp start
;================= End of file ====================
5.5.2. Chöông trình ví duï No.2.
; Load data vao RAM töøØ baûng.
; Xuaát data töø RAM ra LED.
; Xuaát data töø baûng ra LED.
; Ngöôøi vieát: Löu Ñình Duõng.
; Ngaøy vieát: 11/03/1999.
; Caäp nhaät laàn cuoái: 09/07/1999.
org 0000h
start:
;Set giaù trò ban ñaàu cho Stack
ld sp,0fe00h
;Naïp caùc baûng vaøo RAM
ld ix,8000h
ld b,01h
loop:
ld a,b
;
cp 01h
jp z,sw001 ;Neáu a=01h, nhaûy ñeán sw001
cp 02h
jp z,sw002
cp 03h
jp z,sw003
cp 04h
jp z,sw004
cp 05h
jp z,sw005
cp 06h
jp z,sw006
cp 07h
jp z,sw007
cp 08h
jp z,sw008
jp start1
;
sw001:
inc b
ld hl,table1
jp loop02
sw002:
inc b
ld hl,table2
jp loop02
sw003:
inc b
ld hl,table3
jp loop03
sw004:
inc b
ld hl,table4
jp loop03
sw005:
inc b
ld hl,table5
jp loop02
sw006:
inc b
ld hl,table6
jp loop02
sw007:
inc b
ld hl,table7
jp loop02
sw008:
inc b
ld hl,table8
jp loop04
;
loop02:
ld d,08h
jp loop01
loop03:
ld d,0ah
jp loop01
loop04:
ld d,10h
; jp loop01
loop01:
ld a,(hl)
ld (ix+00h),a
inc ix
inc hl
dec d
jp nz,loop01
jp loop
;
start1:
ld iy,8000h
start2:
ld a,(iy+00)
out (00h),a
call delay
;
loop00:
in a,(04h)
;
cp 0feh ;1111 1110
jp z,sw1
;
cp 0fdh ;1111 1101
jp z,sw2
;
cp 0fbh ;1111 1011
jp z,sw3
;
cp 0f7h ;1111 0111
jp z,sw4
;
cp 0efh ;1110 1111
jp z,sw5
;
cp 0dfh ;1101 1111
jp z,sw6
;
cp 0bfh ;1011 1111
jp z,sw7
;
cp 7fh ;0111 1111
jp z,sw8
;
; jp start1
;
inc iy
push iy
push ix
pop de ; ld de,ix
pop bc ; ld bc,iy
ld a,c ; so saùnh ix & iy (cho iy chæ tôùi ix max)
cp e ; (giaù trò lôùn nhaát naïp vaøo RAM)
jp nz,start2
ld a,b
cp d
jp nz,start2
jp start1
;
sw1:
ld hl,table1
jp sw00
;
sw2:
ld hl,table2
jp sw00
;
sw3:
ld hl,table3
jp sw02
;
sw4:
ld hl,table4
jp sw02
;
sw5:
ld hl,table5
jp sw00
;
sw6:
ld hl,table6
jp sw00
;
sw7:
ld hl,table7
jp sw00
;
sw8:
ld hl,table8
jp sw03
;
sw00:
ld b,8
jp sw01
;
sw02:
ld b,0ah
jp sw01
;
sw03:
ld b,10h
; jp sw01
;
sw01:
ld a,(hl)
out (00h),a
call delay
inc hl
dec b
jp nz,sw01
;
jp loop00
;
table1:
db 0feh ;1111 1110
db 0fdh ;1111 1101
db 0fbh ;1111 1011
db 0f7h ;1111 0111
db 0efh ;1110 1111
db 0dfh ;1101 1111
db 0bfh ;1011 1111
db 7fh ;0111 1111
;
table2:
db 7fh ;0111 1111
db 0bfh ;1011 1111
db 0dfh ;1101 1111
db 0efh ;1110 1111
db 0f7h ;1111 0111
db 0fbh ;1111 1011
db 0fdh ;1111 1101
db 0feh ;1111 1110
;
table3:
db 0ffh ;1111 1111
db 0e7h ;1110 0111
db 0c3h ;1100 0011
db 81h ;1000 0001
db 00h ;0000 0000
db 0ffh ;1111 1111
db 0e7h ;1110 0111
db 0c3h ;1100 0011
db 81h ;1000 0001
db 00h ;0000 0000
;
table4:
db 00h ;0000 0000
db 81h ;1000 0001
db 0c3h ;1100 0011
db 0e7h ;1110 0111
db 0ffh ;1111 1111
db 00h ;0000 0000
db 81h ;1000 0001
db 0c3h ;1100 0011
db 0e7h ;1110 0111
db 0ffh ;1111 1111
;
table5:
db 0aah ;1010 1010
db 55h ;0101 0101
db 0aah ;1010 1010
db 55h ;0101 0101
db 0aah ;1010 1010
db 55h ;0101 0101
db 0aah ;1010 1010
db 55h ;0101 0101
;
table6:
db 7fh ;0111 1111
db 3fh ;0011 1111
db 1fh ;0001 1111
db 0fh ;0000 1111
db 07h ;0000 0111
db 03h ;0000 0011
db 01h ;0000 0001
db 00h ;0000 0000
;
table7:
db 0feh ;1111 1110
db 0fch ;1111 1100
db 0f8h ;1111 1000
db 0f0h ;1111 0000
db 0e0h ;1110 0000
db 0c0h ;1100 0000
db 80h ;1000 0000
db 00h ;0000 0000
;
table8:
db 0ffh ;1111 1111
db 7fh ;0111 1111
db 3fh ;0011 1111
db 0bfh ;1011 1111
db 1fh ;0001 1111
db 0dfh ;1101 1111
db 0fh ;0000 1111
db 0efh ;1110 1111
db 07h ;0000 0111
db 0f7h ;1111 0111
db 03h ;0000 0011
db 0fbh ;1111 1011
db 01h ;0000 0001
db 0fdh ;1111 1101
db 00h ;0000 0000
db 0feh ;1111 1110
;
delay:
push hl ;3M, 11T
ld d,0fh ;2M, 7T
delay01:
ld e,0fh ;2M, 7T
delay001:
ld h,0fh ;2M, 7T
delay002:
ld l,0fh ;2M, 7T
;
delay003:
dec l ;1M, 4T
jr nz,delay003 ;2M, 7T
;
dec h ;1M, 4T
jr nz,delay002 ;2M,7T
;
dec e ;1M, 4T
jr nz,delay001 ;2M, 7T
;
dec d ;1M, 4T
jr nz,delay01 ;2M, 7T
;
pop hl ;3M, 10T
ret ;3M, 10T
;
; T1 = 11*15 = 165
; T2 = ( 18+T1 ) * 15 = ( 18+165 ) * 15 = 183 * 15 = 2745
; T3 = ( 18+T2 ) * 15 = ( 18+2745 ) * 15 = 2763 * 15 = 41445
; T4 = ( 18+T3 ) * 15 = ( 18+41445 ) * 15 = 41463 * 15 = 621945
; T5 = T4+59 = 621945+59 = 622004
; T (Thôøi gian delay) = T5/2.4576 (uS) = 253094(uS) = 0.253 (S)
; T = 1/4 (S)
;
;================= End of file ====================
&
Phuï luïc A
Caùc thoâng baùo loãi.
Caùc loaïi loãi coù theå coù khi chaïy chöông trình ôû mode Debug vaø Run:
1. File coù phaàn môû roäng .asm chöa ñöôïc môû.
2. Nhaäp khoâng ñuùng teân cuûa file ñang ñöôïc môû khi coù yeâu caàu nhaäp teân file töø chöông trình dòch (Chuù yù: khoâng nhaäp phaàn môû roäng).
3. Thieáu file coù phaàn môû roäng .lst khi chaïy chöông trình ôû cheá ñoä Debug (Chuù yù: nhaäp ‘Y’ khi coù thoâng baùo taïo file listing khi dòch chöông trình).
4. Chöa caáp nguoàn +5V cho KIT.
5. Phaàn cöùng coù vaán ñeà:
a. Coù byte trong vuøng RAM chung hö.
b. Chöa caém Jack 25 pin.
c. Caùc IC 8255, 74LS157, 74LS244, 74LS374, 74LS74, 74LS04 coù söï coá.
6. Nguoàn +5V bò maát ñoät ngoät trong khi chöông trình ñang chaïy.
7. File *.bin quaù lôùn (>32 Kb).
Caùc loaïi loãi treân seõ ñöôïc thoâng baùo leân maøn hình (neáu coù).
Chuù yù: Trong khi thöïc hieän phaûi chaéc raèng KIT Z80 ñang ñöôïc caáp nguoàn +5V.
& Phuï luïc B
Vi maïch 8255
Vi maïch 8255 thöôøng hay ñöôïc söû duïng trong vieäc xuaát vaø nhaäp döõ lieäu soá, trong vieäc ñieàu khieån quaù trình bieán ñoåi A/D. 8255 coù 24 ñöôøng xuaát nhaäp ñöôïc xeáp thaønh 3 coång song song (coång A, B vaøC).
Tính linh hoaït cuûa vi maïch naøy theå hieän ôû khaû naêng laäp trình. Qua moät thanh ghi ñieàu khieån, ngöôøi söû duïng xaùc ñònh loaïi hoaït ñoäng vaø coång naøo caàn ñöôïc söû duïng nhö laø loái ra hoaëc loái vaøo. Ñöôøng data 2 chieàu D0 - D7 roäng 8 bits. Taát caû caùc döõ lieäu khi truy xuaát ghi hoaëc ñoïc ñöôïïc daãn qua caùc ñöôøng daãn naøy. Traïng thaùi logic ghi/ñoïc ñöôïc nhaän bieát qua caùc tín hieäu ñieàu khieån /CS, /RD, /WR. Khi /CS = '0', /RD = '0' caùc tín hieäu cuûa coång ñöôïc löïïa choïn ñöôïc daãn ñeán data bus. Khi /CS = '0', /WR = 0 moïi vieäc xaûy ra ngöôïc laïi. Caùc bit ñòa chæ A0 vaø A1 cuøng vôùi caùc tín hieäu ghi vaø ñoïc baùo hieäu cho bieát seõ truy nhaäp leân coång naøo.
Baûng söï thaät döôùi ñaây toùm taêt caùc keát luaän vöøa môùi trình baøy ôû treân:
A1 A0 /RD /WR /CS Chöùc naêng
0 0 0 1 0 Port A => Data bus
0 1 0 1 0 Port B => Data bus
1 0 0 1 0 Port C => Data bus
0 0 1 0 0 Data bus => Port A
0 1 1 0 0 Data bus => Port B
1 0 1 0 0 Data bus => Port C
1 1 1 0 0 Ghi töø ñieàu khieån
X X X X 1 Data bus ôû traïng thaùi HiZ
X X 1 1 0 Data bus ôû traïng thaùi HiZ
Trong moät chu trình ghi leân thanh ghi ñieàu khieån, ngöôøi söû duïng khaúng ñònh coång vaøo/ ra cuõng nhö kieåu hoaït ñoäng trong moät töø ñieàu khieån. Moät möùc logic '1' ôû chaân RESET ñaët trôû laïi thanh ghi ñieàu khieån vaø ñònh nghiaõ toaøn boä 24 ñRôøng daãn nhö laø caùc loái vaøo. Söï saép xeáp cuûa töø ñieàu khieån ñöôïc moâ taû treân hình sau:
D7 D6 D5 D4 D3 D2 D1 D0
Nhoùm B
Coång C
1 = in
0 = out
Coång B
1 = in
0 = out
Mode
1 = Mode 1
0 = Mode 0
Nhoùm A
Coång C
1 = in
0 = out
Coång A
1 = in
0 = out
Mode
00 = Mode 0
01 = Mode 1
0X = Mode 2
Mode - Flag
1 = Active
Söï saép xeáp cuûa töø ñieàu khieån ôû vi maïch 8255.
Ta thaáy 24 ñöôøng daãn cuûa caùc coång A, B, C ñöôïc phaân chia thaønh 2 nhoùm. Trong ñoù 1 nöûa cuûa coång C (PC4...PC7) thuoäc vaøo nhoùm A, coøn nöûa kia thuoäc nhoùm B. Toång coäng ngöôøi söû duïng coù 3 kieåu hoaït ñoäng khaùc nhau. Mode 0, mode 1, mode 2.
Mode 0: Xuaát vaø nhaäp döõ lieäu theo caùch ñôn giaûn qua PA, PB, PC.
Ta seõ khoâng ñeà caäp tôùi mode 1 vaø mode 2.
&
Taøi lieäu tham khaûo.
[1] Ngoâ Dieân Taäp, Ño löôøng vaø ñieàu khieån baèng maùy tính.
Nhaø xuaát baûn khoa hoïc vaø kyõ thuaät - 1996.
[2] Leâ Vaên Vieät, Caáu truùc maùy tính.
Nhaø xuaát baûn Ñoàng Nai - 1995.
[3] Lance A. Leventhal, Z80 Assembly language programming.
McGraw-Hill, Inc - 1986.
[4] Atkinson and Atkinson, Using Borland C++.
QUE - 1991.
[5] Gerald Leblanc, Turbo C.
Nhaø xuaát baûn khoa hoïc vaø kyõ thuaät - 1996.
[6] Motorola, DATA BOOK DIGITAL IC - 1992.
[7] Z80 Microprocessor Family.
User’s Manual.
-------------------------------------------------------------
&
Các file đính kèm theo tài liệu này:
- THIET_KE__THI_CONG__VIET_CH.DOC