1414#if defined(__TURBOC__ ) || (__WATCOMC__ )
1515#include <io.h> /* write (what else?) */
1616#include <stdlib.h> /* _psp, NULL, malloc, free, atol */
17+ #include <stdio.h> /* printf */
1718#define NON_RES_TEXT
1819#define NON_RES_DATA
1920#define NON_RES_RODATA
@@ -22,6 +23,7 @@ typedef unsigned char uint8_t;
2223typedef unsigned short uint16_t ;
2324
2425#if (__WATCOMC__ )
26+ #pragma pack (1) /* structures are packed */
2527//void far * getvect(unsigned char intno);
2628//void setvect(unsigned char intno, void far * vector);
2729#define getvect (x ) _dos_getvect(x)
@@ -182,11 +184,15 @@ static char progname[9] NON_RES_BSS;
182184#pragma aux old_handler2f "*"
183185#pragma aux handler2f "*"
184186
185- /* same as __cdecl except don't preceed with _ underscore */
187+ /* gcc-ia16 version of __cdecl, similar to OW except don't preceed with _ underscore & doesn't modify __es
188+ __caller [] -- all arguments are passed on the stack, caller pops off on return
189+ __value ... -- return result in __ax
190+ __modify [__ax __bx __cx __dx] -- may be modified, all other registers unchanged
191+ */
186192#pragma aux __gcc16 "*" \
187193__parm __caller [] \
188194__value __struct __float __struct __routine [__ax] \
189- __modify [__ax __bx __cx __dx __es ]
195+ __modify [__ax __bx __cx __dx]
190196#pragma aux (__gcc16) inner_handler
191197
192198#endif
@@ -200,9 +206,16 @@ extern uint16_t lock_table_size_bytes; /* amount bytes */
200206extern uint16_t lock_table_free ;
201207extern uint16_t lock_table_offset ;
202208#else
203- static unsigned int file_table_size_bytes NON_RES_DATA = 2048 ;
204209uint16_t file_table_size = 0 ; /* # of file_t we can have */
205210uint16_t lock_table_size = 20 ; /* # of lock_t we can have */
211+
212+ uint16_t file_table_size_bytes ; /* amount bytes */
213+ uint16_t file_table_free ;
214+ uint16_t file_table_offset ;
215+ uint16_t lock_table_size_bytes ; /* amount bytes */
216+ uint16_t lock_table_free ;
217+ uint16_t lock_table_offset ;
218+
206219#endif
207220static file_t * file_table = NULL ;
208221static lock_t * lock_table = NULL ;
@@ -733,50 +746,37 @@ static int is_file_open(char far *filename)
733746unsigned short init_tables (void ) {
734747 unsigned short paras ;
735748 char far * fptr ;
736- #if defined(__TURBOC__ )
737- char * onebyte ;
749+ char * p ;
738750
739751 file_table_size = file_table_size_bytes / sizeof (file_t );
740- if (( file_table = malloc ( file_table_size_bytes )) == NULL )
741- return 0 ;
742- memset ( file_table , 0 , file_table_size_bytes ) ;
752+ lock_table_size_bytes = lock_table_size * sizeof ( lock_t );
753+ file_table_free = file_table_size ;
754+ lock_table_free = lock_table_size ;
743755
744- if ((lock_table = malloc (lock_table_size * sizeof (lock_t ))) == NULL ) {
745- free (file_table );
746- file_table = NULL ;
756+ if ((p = malloc (file_table_size_bytes + lock_table_size_bytes )) == NULL )
747757 return 0 ;
748- }
749- memset (lock_table , 0 , lock_table_size * sizeof (lock_t ));
758+ memset (p , 0 , file_table_size_bytes + lock_table_size_bytes );
759+
760+ file_table = (void * )p ;
761+ lock_table = (void * )(p + file_table_size_bytes );
762+
763+ #if defined(__TURBOC__ )
764+ {
765+ char * onebyte ;
750766
751767 /* Allocate a single byte. This tells us the size of the TSR.
752768 Free the byte when we know the address. */
753769 onebyte = malloc (1 );
754770 if (onebyte == NULL ) {
755- free (file_table );
771+ free (p );
756772 file_table = NULL ;
757- free (lock_table );
758773 lock_table = NULL ;
759774 return 0 ;
760775 }
761776 fptr = (char far * )onebyte ;
762777 free (onebyte );
763-
778+ }
764779#else /* GNUC */
765- char * p ;
766-
767- file_table_size = file_table_size_bytes / sizeof (file_t );
768- lock_table_size_bytes = lock_table_size * sizeof (lock_t );
769- file_table_free = file_table_size ;
770- lock_table_free = lock_table_size ;
771-
772- p = sbrk (file_table_size_bytes + lock_table_size_bytes );
773- if (p == (void * )-1 )
774- return 0 ;
775-
776- // No need to memset() as sbrk() does it for us
777-
778- file_table = (void * )p ;
779- lock_table = (void * )(p + file_table_size_bytes );
780780 file_table_offset = (uint16_t )file_table ;
781781 lock_table_offset = (uint16_t )lock_table ;
782782
@@ -945,9 +945,7 @@ int main(int argc, char **argv) {
945945 int i ;
946946 uint8_t ii ;
947947
948- #if defined(__GNUC__ ) || defined(__WATCOMC__ )
949948 file_table_size_bytes = 2048 ;
950- #endif
951949
952950 cat = catopen ("share" , 0 );
953951
@@ -1212,16 +1210,22 @@ int main(int argc, char **argv) {
12121210 /* Hook the interrupt for the handler routine. */
12131211 /* disable(); */
12141212 i2D_next = getvect (0x2D );
1215- setvect (0x2D , i2D_handler ); /* TODO this causes relocation on TC & OW */
1213+ #if defined(__WATCOMC__ )
1214+ printf ("cs=ds=_psp=%04x : i2D_handler=%04x\n" , _psp , FP_OFF ((void near * )i2D_handler ));
1215+ /* Note: in tiny memory model, _psp == cs == ds */
1216+ setvect (0x2D , MK_FP (_psp , FP_OFF ((void near * )i2D_handler )));
1217+ #else
1218+ setvect (0x2D , i2D_handler ); /* this causes relocation on TC & OW */
1219+ #endif
12161220#endif
12171221 old_handler2f = getvect (MUX_INT_NO );
1218- #if defined(__TURBOC__ ) && (__TURBOC__ >= 0x0300 )
1222+ #if ( defined(__TURBOC__ ) && (__TURBOC__ >= 0x0300 ) )
12191223 {
12201224 void (near * isr )() = FP_OFF (handler2f );
12211225 setvect (MUX_INT_NO ,(void (interrupt far * )())MK_FP (_DS ,isr ));
12221226 }
12231227#elif defined(__WATCOMC__ )
1224- setvect (MUX_INT_NO ,handler2f ); // TODO get handler2f without relocation
1228+ setvect (MUX_INT_NO ,MK_FP ( _psp , FP_OFF (( void near * ) handler2f )));
12251229#else /* causes relocations when built with Turbo C/C++ 3 and OW */
12261230 setvect (MUX_INT_NO ,handler2f );
12271231#endif
0 commit comments