disk8: Initial commit
[pdp8.git] / sw / disk8 / src / ops.c
diff --git a/sw/disk8/src/ops.c b/sw/disk8/src/ops.c
new file mode 100644 (file)
index 0000000..da88b90
--- /dev/null
@@ -0,0 +1,84 @@
+
+#include <stdio.h>
+#include "structures.h"
+#include "ops.h"
+
+/*
+ * Pack an OS/8 filename into 6 bit code, 2 digits per 12 bit word.
+ * Argument is a zero-terminated string, containing the full name with
+ * the dot, i.E. "XXXXXX.YY",0 is the legal form.
+ */
+void os8_file_name_pack(os8_file_name_t * filename, char * data){
+  
+  /* Read and write positions */
+  int inpos=0, outpos=0;
+
+  /* Zero out all data in target */
+  for (outpos=0; outpos<4; outpos++) filename->data[outpos]=0;
+
+  for (outpos=0; outpos<8 && data[inpos];){
+    if (data[inpos]=='.'){
+      inpos++;
+      if (outpos<6) outpos=6;
+      continue;
+    }
+    filename->data[outpos/2] |= os8_ascii2pdp(data[inpos++]) << ((!(outpos%2))*6);
+    outpos++;
+  }    
+}
+
+void os8_file_name_extract(os8_file_name_t * filename, char * data){
+  
+  /* Read and write positions */
+  int inpos=0, outpos=0;
+
+  for (inpos=0; inpos<8; inpos++){
+
+    unsigned short int current=filename->data[inpos/2]; /* Get right word */
+    if (!(inpos%2)) current >>= 6;
+    current &= 077;
+
+    if ((!current) && ((inpos==0)||(inpos==6))){ /* Empty file name or extension*/
+      data[outpos]=0;                            /* Terminate file name in buffer */
+      break;                                     /* Leave the loop! */
+    }
+
+    if (inpos==6){
+      data[outpos++]='.';          /* Insert dot into new name */
+    }
+    
+    if (current) {                 /* Insert character into output */
+      data[outpos++]=os8_pdp2ascii(current);
+    }
+  }
+  data[outpos]=0;                  /* Terminate string. */
+}
+
+/* Pack one character into 6 bit OS/8 reduced ASCII code */
+unsigned char os8_ascii2pdp(unsigned char inchar){
+  unsigned char result=inchar & 077;  /* Strip upper bits */
+  return result;
+}
+
+/* Extract one character from 6 bit OS/8 reduced ASCII code */
+unsigned char os8_pdp2ascii(unsigned char inchar){
+  inchar &=077;
+  if (inchar<040) return inchar|0100;
+  return inchar;
+}
+
+/* Negate a p8word */
+p8word p8_neg(p8word in){
+  return (-in)&07777;
+}
+
+/* Twist the bytes for little endian machines */
+p8word p8_twist(p8word in){
+  return (in&0xff)<<8 | ((in>>8)&0xff);
+}
+
+
+
+
+
+