Establish a common library for Watcom tools

This commit is contained in:
RichardG867
2021-08-15 20:36:40 -03:00
parent 0c3a813520
commit 40beb1be28
13 changed files with 138 additions and 80 deletions

3
.gitignore vendored
View File

@@ -1,7 +1,8 @@
# cp437 tool
cp437/cp437
cp437/cp437.exe
*.cp437
cp437.tmp
*_cp437
# Watcom C
*.err

View File

@@ -7,6 +7,7 @@ Usage
```
cp437 infile [infile...]
- Converts UTF-8 input file(s) to CP437 output file(s) with .cp437 appended.
The new file names are also printed to stdout.
```
Building
@@ -14,4 +15,4 @@ Building
This tool is automatically built as needed by the build scripts for other tools. Alternatively:
* **Windows:** Run `build.bat` from an OpenWatcom "Build Environment" command prompt.
* **Linux:** Run `./build.sh` with OpenWatcom tools present on `$PATH`.
* **Linux:** Run `./build.sh` with OpenWatcom tools present in `$PATH`.

View File

@@ -48,8 +48,9 @@ main(int argc, char **argv)
char *buf;
FILE *fin, *fout;
/* Disable stdout buffering. */
/* Disable stdout/stderr buffering. */
setbuf(stdout, NULL);
setbuf(stderr, NULL);
/* Print usage if no input files were specified. */
if (argc < 2) {
@@ -57,6 +58,7 @@ main(int argc, char **argv)
printf("\n");
printf("cp437 infile [infile...]\n");
printf("- Converts UTF-8 input file(s) to CP437 output file(s) with .cp437 appended.\n");
printf(" The new file names are also printed to stdout.\n");
return 1;
}
@@ -66,23 +68,23 @@ main(int argc, char **argv)
/* Open input file. */
fin = fopen(argv[i], "rb");
if (!fin) {
printf("Could not open input file \"%s\"\n", argv[i]);
fprintf(stderr, "Could not open input file \"%s\"\n", argv[i]);
continue;
}
/* Generate output file name. */
buf = malloc(strlen(argv[i]) + 7);
sprintf(buf, "%s.cp437", argv[i]);
sprintf(buf, "%s_cp437", argv[i]);
/* Open output file. */
fout = fopen(buf, "wb");
if (!fout) {
fclose(fin);
printf("Could not open output file \"%s\"\n", buf);
fprintf(stderr, "Could not open output file \"%s\"\n", buf);
continue;
}
printf("Processing \"%s\"\n", argv[i]);
printf("%s\n", buf);
/* Perform the conversion. */
while (!feof(fin)) {

View File

@@ -33,8 +33,8 @@ if not exist ..\cp437\cp437.exe (
)
:: Convert source file to CP437.
echo *** Converting %1 to CP437...
..\cp437\cp437.exe %1
echo *** Converting source to CP437...
..\cp437\cp437.exe %* > cp437.tmp
if errorlevel 1 (
echo *** Conversion failed.
exit /b 2
@@ -48,7 +48,9 @@ for %%i in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do set "destfil
:: Call compiler.
echo *** Building...
wcl -bcl=dos -fo="!destfile!" "%1.cp437"
set srcfiles=
for /f "delims=" %%i in (cp437.tmp) do set srcfiles=!srcfiles! %%i
wcl -bcl=dos -i=..\lib -fe="!destfile!" %srcfiles%
:: Check for success.
if errorlevel 1 (

View File

@@ -35,9 +35,9 @@ then
popd
fi
# Convert source file to CP437.
echo '***' Converting $1 to CP437...
if ! ../cp437/cp437 "$1"
# Convert source files to CP437.
echo '***' Converting source to CP437...
if ! ../cp437/cp437 "$@" > cp437.tmp
then
echo '***' Conversion failed.
exit 2
@@ -47,7 +47,7 @@ fi
destfile=$(basename "$1" .c | tr [:lower:] [:upper:]).EXE
# Call compiler and check for success.
if wcl -bcl=dos -fo="$destfile" "$1".cp437
if wcl -bcl=dos -fo="$destfile" $(cat cp437.tmp)
then
echo '***' Build successful.
else

View File

@@ -29,7 +29,7 @@ set destfile=%srcfile:~0,-2%.exe
:: Call compiler.
echo *** Building...
wcl386 -bcl=nt -fo="%destfile%" "%1"
wcl386 -bcl=nt -fo="%destfile%" %*
:: Check for success.
if errorlevel 1 (

View File

@@ -26,7 +26,7 @@ fi
destfile=$(basename "$1" .c)
# Call compiler and check for success.
if wcl386 -bcl=linux -fo="$destfile" "$1"
if wcl386 -bcl=linux -fo="$destfile" "$@"
then
echo '***' Build successful.
chmod +x "$destfile"

33
lib/wlib.c Normal file
View File

@@ -0,0 +1,33 @@
/*
* 86Box A hypervisor and IBM PC system emulator that specializes in
* running old operating systems and software designed for IBM
* PC systems and compatibles from 1981 through fairly recent
* system designs based on the PCI bus.
*
* This file is part of the 86Box Probing Tools distribution.
*
* Common functions for Watcom C-based tools.
*
*
*
* Authors: RichardG, <richardg867@gmail.com>
*
* Copyright 2021 RichardG.
*
*/
#include <stdint.h>
#include "wlib.h"
uint32_t
pci_cf8(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg)
{
/* Generate a PCI port CF8h dword. */
multi_t ret;
ret.u8[3] = 0x80;
ret.u8[2] = bus;
ret.u8[1] = dev << 3;
ret.u8[1] |= func & 7;
ret.u8[0] = reg & 0xfc;
return ret.u32;
}

70
lib/wlib.h Normal file
View File

@@ -0,0 +1,70 @@
/*
* 86Box A hypervisor and IBM PC system emulator that specializes in
* running old operating systems and software designed for IBM
* PC systems and compatibles from 1981 through fairly recent
* system designs based on the PCI bus.
*
* This file is part of the 86Box Probing Tools distribution.
*
* Definitions for common functions for Watcom C-based tools.
*
*
*
* Authors: RichardG, <richardg867@gmail.com>
*
* Copyright 2021 RichardG.
*
*/
#ifndef WLIB_H
# define WLIB_H
#include <stdint.h>
#pragma pack(push, 0)
/* Convenience type for breaking a dword value down into words and bytes. */
typedef union {
uint8_t u8[4];
uint16_t u16[2];
uint32_t u32;
} multi_t;
#pragma pack(pop)
/* Port I/O functions. */
uint8_t inb(uint16_t port);
#pragma aux inb = "in al, dx" parm [dx] value [al];
void outb(uint16_t port, uint8_t data);
#pragma aux outb = "out dx, al" parm [dx] [al];
uint16_t inw(uint16_t port);
#pragma aux inw = "in ax, dx" parm [dx] value [ax];
void outw(uint16_t port, uint16_t data);
#pragma aux outw = "out dx, ax" parm [dx] [ax];
#ifdef M_I386
uint32_t inl(uint16_t port);
# pragma aux inl = "in eax, dx" parm [dx] value [eax];
void outl(uint16_t port, uint32_t data);
# pragma aux outl = "out dx, eax" parm [dx] [eax];
#else
/* Some manual prefixing trickery to perform 32-bit I/O and access
the extended part of EAX in real mode. Exchanging is necessary
due to Watcom ignoring the order registers are specified in... */
uint32_t inl(uint16_t port);
# pragma aux inl = "db 0x66" "in ax, dx" /* in eax, dx */ \
"mov cx, ax" \
"db 0x66, 0xc1, 0xe8, 0x10" /* shr eax, 16 */ \
"xchg ax, cx" \
parm [dx] \
value [ax cx];
void outl(uint16_t port, uint32_t data);
# pragma aux outl = "xchg ax, cx" \
"db 0x66, 0xc1, 0xe0, 0x10" /* shl eax, 16 */ \
"mov ax, cx" \
"db 0x66" "out dx, ax" /* out dx, eax */ \
parm [dx] [ax cx] \
modify [ax cx];
#endif
/* PCI functions. */
extern uint32_t pci_cf8(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg);
#endif

Binary file not shown.

View File

@@ -1,2 +1,2 @@
@echo off
..\lib\build_watcom_dos.bat pcireg.c
..\lib\build_watcom_dos.bat pcireg.c ..\lib\wlib.c

View File

@@ -1,2 +1,2 @@
#!/bin/sh
exec ../lib/build_watcom_dos.sh pcireg.c
exec ../lib/build_watcom_dos.sh pcireg.c ../lib/wlib.c

View File

@@ -27,14 +27,9 @@
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "wlib.h"
typedef union {
uint8_t u8[4];
uint16_t u16[2];
uint32_t u32;
} multi_t;
struct videoconfig vc;
union REGPACK rp; /* things break if this is not a global variable... */
@@ -74,52 +69,6 @@ typedef struct {
#pragma pack(pop)
void outb(uint16_t port, uint8_t data);
#pragma aux outb = "out dx, al" parm [dx] [al];
uint8_t inb(uint16_t port);
#pragma aux inb = "in al, dx" parm [dx] value [al];
void outw(uint16_t port, uint16_t data);
#pragma aux outw = "out dx, ax" parm [dx] [ax];
uint16_t inw(uint16_t port);
#pragma aux inw = "in ax, dx" parm [dx] value [ax];
#ifdef M_I386
void outl(uint16_t port, uint32_t data);
# pragma aux outl = "out dx, eax" parm [dx] [eax];
uint32_t inl(uint16_t port);
# pragma aux inl = "in eax, dx" parm [dx] value [eax];
#else
void outl(uint16_t port, uint32_t data);
# pragma aux outl = "xchg ax, cx" \
"db 0x66, 0xc1, 0xe0, 0x10" /* shl eax, 16 */ \
"mov ax, cx" \
"db 0x66" "out dx, ax" /* out dx, eax */ \
parm [dx] [ax cx] \
modify [ax cx];
uint32_t inl(uint16_t port);
# pragma aux inl = "db 0x66" "in ax, dx" /* in eax, dx */ \
"mov cx, ax" \
"db 0x66, 0xc1, 0xe8, 0x10" /* shr eax, 16 */ \
"xchg ax, cx" \
parm [dx] \
value [ax cx];
#endif
uint32_t
make_cf8(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg)
{
multi_t ret;
ret.u8[3] = 0x80;
ret.u8[2] = bus;
ret.u8[1] = dev << 3;
ret.u8[1] |= func & 7;
ret.u8[0] = reg & 0xfc;
return ret.u32;
}
char *
read_string(FILE *f, uint32_t offset)
{
@@ -148,7 +97,7 @@ dump_regs(uint8_t bus, uint8_t dev, uint8_t func, uint8_t start_reg, char sz)
start_reg &= 0xfc;
/* Build the base CF8h dword for this dump. */
cf8 = make_cf8(bus, dev, func, 0x00);
cf8 = pci_cf8(bus, dev, func, 0x00);
/* Generate dump file name. */
sprintf(buf, "PCI%02X%02X%d.BIN", bus, dev, func);
@@ -568,7 +517,7 @@ scan_bus(uint8_t bus, int nesting, char dump, FILE *f, char *buf)
dev_id.u32 = 0xffffffff;
}
#else
cf8 = make_cf8(bus, dev, func, 0x00);
cf8 = pci_cf8(bus, dev, func, 0x00);
outl(0xcf8, cf8);
dev_id.u32 = inl(0xcfc);
#endif
@@ -595,7 +544,7 @@ scan_bus(uint8_t bus, int nesting, char dump, FILE *f, char *buf)
dev_rev_class.u16[0] = rand();
dev_rev_class.u16[1] = rand();
#else
cf8 = make_cf8(bus, dev, func, 0x08);
cf8 = pci_cf8(bus, dev, func, 0x08);
outl(0xcf8, cf8);
dev_rev_class.u32 = inl(0xcfc);
#endif
@@ -721,7 +670,7 @@ unknown:
#ifdef DEBUG
header_type = (bus < (DEBUG - 1)) ? 0x01 : 0x00;
#else
cf8 = make_cf8(bus, dev, func, 0x0c);
cf8 = pci_cf8(bus, dev, func, 0x0c);
outl(0xcf8, cf8);
header_type = inb(0xcfe);
#endif
@@ -732,7 +681,7 @@ unknown:
#ifdef DEBUG
new_bus = bus + 1;
#else
cf8 = make_cf8(bus, dev, func, 0x18);
cf8 = pci_cf8(bus, dev, func, 0x18);
outl(0xcf8, cf8);
new_bus = inb(0xcfd);
#endif
@@ -803,7 +752,7 @@ read_reg(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg)
bus, dev, func, reg | 3, reg & 0xfc);
/* Read dword value from register. */
cf8 = make_cf8(bus, dev, func, reg);
cf8 = pci_cf8(bus, dev, func, reg);
#ifdef DEBUG
reg_val.u32 = cf8;
#else
@@ -916,7 +865,7 @@ retry_buf:
if (entries > 0) {
/* Assume device 00 is the northbridge if it has a host bridge class. */
if (entry->dev > 0x00) {
cf8 = make_cf8(0x00, 0x00, 0, 0x08);
cf8 = pci_cf8(0x00, 0x00, 0, 0x08);
outl(0xcf8, cf8);
dev_class = inw(0xcfe);
if (dev_class == 0x0600)
@@ -924,7 +873,7 @@ retry_buf:
}
/* Assume device 01 is the AGP bridge if it has a PCI bridge class. */
if (entry->dev > 0x01) {
cf8 = make_cf8(0x00, 0x01, 0, 0x08);
cf8 = pci_cf8(0x00, 0x01, 0, 0x08);
outl(0xcf8, cf8);
dev_class = inw(0xcfe);
if (dev_class == 0x0604)
@@ -1000,7 +949,7 @@ retry_buf:
printf("NORTHBRIDGE,");
} else {
/* Read device class. */
cf8 = make_cf8(0x00, entry->dev, 0, 0x08);
cf8 = pci_cf8(0x00, entry->dev, 0, 0x08);
outl(0xcf8, cf8);
dev_class = inw(0xcfe);
@@ -1081,7 +1030,7 @@ write_reg(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg, char *val)
multi_t reg_val;
/* Write a byte, word or dword depending on the input value's length. */
cf8 = make_cf8(bus, dev, func, reg);
cf8 = pci_cf8(bus, dev, func, reg);
data_port = 0xcfc;
switch (strlen(val)) {
case 1: