Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Guido committed Mar 14, 2022
1 parent 31ad454 commit 0d49191
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 94 deletions.
195 changes: 103 additions & 92 deletions src/md_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,105 +51,116 @@ void md_expect(FILE *infd, uint16_t w)
//
void md_decode_file(FILE *infd, FILE *ofd)
{
uint16_t w;
uint16_t w, n, a;
uint16_t vers;
bool proc_section = true;
bool eof = false;

md_expect(infd, 0200);
md_expect(infd, 1);
w = md_rword(infd);

// Header section
md_expect(infd, 0201);
vers = md_rword(infd);
fprintf(ofd, "HEADER\n");
md_rname(infd, ofd);

// Skip bytes following filename in later versions
if (vers == 0x11)
md_skip(infd, 6);

w = md_rword(infd);
fprintf(ofd, "\n DataSize: %07o (%d)", w, w);
w = md_rword(infd);
fprintf(ofd, "\n CodeSize: %07o (%d)\n\n", w, w);
md_rword(infd);

// Import section
w = md_rword(infd);
if (w == 0202)
{
fprintf(ofd, "IMPORTS\n");
uint16_t n = md_rword(infd);
while (n > 0)
{
md_rname(infd, ofd);
n -= 11;
}
w = md_rword(infd);
fprintf(ofd, "\n");
}
// Parse all sections
while (! eof) {

// Data sections
while (w == 0204)
{
fprintf(ofd, "DATA\n");
uint16_t n = md_rword(infd);
uint16_t a = md_rword(infd);
while (n-- > 1)
{
fprintf(ofd, "%7d: %07o\n", a++, md_rword(infd));
}
// Read next word
w = md_rword(infd);
fprintf(ofd, "\n");
}
eof = feof(infd);

// Entries section
if (w == 0203)
{
fprintf(ofd, "PROCEDURES\n");
uint16_t n = md_rword(infd);
uint16_t a = 0;

md_rword(infd);
// md_expect(infd, 0);
while (n-- > 1)
switch (w)
{
fprintf(ofd, "%7d: %07o\n", a, md_rword(infd));
a ++;
}
w = md_rword(infd);
fprintf(ofd, "\n");
}

// Code section
if (w == 0203)
{
fprintf(ofd, "CODE\n");
uint16_t n = md_rword(infd);
uint16_t a = md_rword(infd);
case 0200 :
// Start of file
md_expect(infd, 1);
md_rword(infd);
break;

n = (a + n - 1) << 1;
a <<= 1;
while (a < n)
{
fprintf(ofd, " %07o ", a);
a += md_opcode(infd, ofd, md_rbyte(infd));
}
w = md_rword(infd);
fprintf(ofd, "\n");
}

// Relocation section
if (w == 0205)
{
fprintf(ofd, "RELOCATION\n");
uint16_t n = md_rword(infd);
case 0201 :
// Header section
vers = md_rword(infd);
fprintf(ofd, "HEADER (v.%d)\n", vers);
md_rname(infd, ofd);

while (n-- > 0)
{
uint16_t w = md_rword(infd);
fprintf(ofd, " %07o\n", w);
// Skip bytes following filename in later versions
if (vers == 0x11)
md_skip(infd, 6);

w = md_rword(infd);
fprintf(ofd, "\n DataSize: %07o (%d)", w, w);
w = md_rword(infd);
fprintf(ofd, "\n CodeSize: %07o (%d)\n\n", w, w);
md_rword(infd);
break;

case 0202 :
// Import section
fprintf(ofd, "IMPORTS\n");
n = md_rword(infd);
while (n > 0)
{
md_rname(infd, ofd);
n -= 11;
}
break;

case 0204 :
// Data sections
fprintf(ofd, "DATA [G]\n");
n = md_rword(infd);
a = md_rword(infd);
while (n-- > 1)
{
fprintf(ofd, "%7d: %07o\n", a++, md_rword(infd));
}
break;

case 0203 :
if (proc_section)
{
// Procedure entry point section
n = md_rword(infd);
a = 0;
w = md_rword(infd);

fprintf(ofd, "PROCEDURES [F,%03o]\n", w);
while (n-- > 1)
{
fprintf(ofd, "%7d: %07o\n", a, md_rword(infd));
a ++;
}
}
else {
n = md_rword(infd);
a = md_rword(infd);
n = (a + n - 1) << 1;
a <<= 1;
fprintf(ofd, "CODE [F]\n");

while (a < n)
{
fprintf(ofd, " %07o ", a);
a += md_opcode(infd, ofd, md_rbyte(infd));
}
}
proc_section = ! proc_section;
break;

case 0205 :
// Relocation section
fprintf(ofd, "RELOCATION\n");
n = md_rword(infd);

while (n-- > 0)
{
w = md_rword(infd);
fprintf(ofd, " %07o\n", w);
}
break;

default :
// No more readable sections
eof = true;
break;
}
fprintf(ofd, "\n");
}

// End of section
if (! eof)
fprintf(ofd, "\n");
};
}
5 changes: 3 additions & 2 deletions src/md_fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ uint16_t md_rword(FILE *infd)
uint16_t wr;

if (fread(&wr, sizeof(uint16_t), 1, infd) != 1)
error(1, 1, "Could not read from input file");
wr = 0xffff;

// Swap byte order
return ((wr >> 8) | (wr << 8));
Expand All @@ -42,8 +42,9 @@ uint8_t md_rbyte(FILE *infd)
{
uint16_t b;

// Errors will be caught by caller
if (fread(&b, 1, 1, infd) != 1)
error(1, 1, "Could not read from input file");
b = 0xff;

return b;
}
1 change: 1 addition & 0 deletions src/mdecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
#include <unistd.h>
#include <errno.h>
#include <error.h>
#include <stdbool.h>

#endif

0 comments on commit 0d49191

Please # to comment.