summaryrefslogtreecommitdiff
path: root/mzf2wav/src/physical.c
diff options
context:
space:
mode:
Diffstat (limited to 'mzf2wav/src/physical.c')
-rw-r--r--mzf2wav/src/physical.c160
1 files changed, 160 insertions, 0 deletions
diff --git a/mzf2wav/src/physical.c b/mzf2wav/src/physical.c
new file mode 100644
index 0000000..6224388
--- /dev/null
+++ b/mzf2wav/src/physical.c
@@ -0,0 +1,160 @@
+#include "physical.h"
+
+// Defenitions.
+#define CONTP 0x37a
+extern dword fs;
+
+// Global variables.
+byte ZERO = 0x30,
+ ONE = 0xd0;
+int LONG_UP = 0, // These variables define the long wave.
+ LONG_DOWN = 0,
+ SHORT_UP = 0, // These variables define the short wave.
+ SHORT_DOWN = 0,
+ corr_1 = 0,
+ corr_2 = 0;
+
+// Private function prototypes.
+void lp(void), // Long pulse.
+ sp(void); // Short pulse.
+
+// Private functions.
+// Write a long pulse.
+void lp(void) {
+ int j = 0;
+
+ for (j = 0; j < LONG_UP; j++)
+ outb(ZERO, CONTP);
+ for (j = 0; j < LONG_DOWN; j++)
+ outb(ONE, CONTP);
+ fs += LONG_UP + LONG_DOWN;
+}//lp
+
+// Write a short pulse.
+void sp(void) {
+ int j = 0;
+
+ for (j = 0; j < SHORT_UP; j++)
+ outb(ZERO, CONTP);
+ for (j = 0; j < SHORT_DOWN; j++)
+ outb(ONE, CONTP);
+ fs += SHORT_UP + SHORT_DOWN;
+}//lp
+
+// Public functions.
+// Reverse polarity.
+void reversepol(void) {
+ ZERO = 0xd0;
+ ONE = 0x30;
+}//reversepol
+
+// Write a gap of i short pulses.
+void gap(int i) {
+ int j = 0;
+
+ for (j = 0; j < i; j++)
+ sp();
+}//gap
+
+// Write a tapemark of i long pulses, i short pulses and one long pulse.
+void tapemark(int i) {
+ int j = 0;
+
+ for (j = 0; j < i; j++)
+ lp();
+ for (j = 0; j < i; j++)
+ sp();
+ lp();
+ lp();
+}//tapemark
+
+// Write the checksum.
+void writecs(word cs) {
+ byte i = 0x0;
+ int j = 0;
+
+ cs &= 0xffff;
+ for (j = 0x3; j; j >>= 1) { // for (j = 0; j < 2; j++)
+ for (i = 0xff; i; i >>= 1) { // for (i = 0; i < 8; i++)
+ if (cs & 0x8000) // If the most significant bit is set
+ lp(); // wite a one.
+ else
+ sp(); // Else write a zero.
+ cs <<= 1; // Go to the next bit.
+ }//for
+ lp();
+ }//for
+ lp();
+}//writecs
+
+// Define the waveform to use.
+void setspeed(int i) {
+ switch (i) {
+ case 1: // Fastest in normal mode. Probably unstable...
+ LONG_UP = FAST_LONG_UP + corr_1;
+ LONG_DOWN = FAST_LONG_DOWN + corr_1;
+ SHORT_UP = FAST_SHORT_UP + corr_1;
+ SHORT_DOWN = FAST_SHORT_DOWN + corr_1;
+ break;
+ case 2: // Turbo mode 2x.
+ LONG_UP = TURBO_2_LONG_UP;
+ LONG_DOWN = TURBO_2_LONG_DOWN;
+ SHORT_UP = TURBO_2_SHORT_UP;
+ SHORT_DOWN = TURBO_2_SHORT_DOWN;
+ break;
+ case 3: // Turbo mode 3x.
+ LONG_UP = TURBO_3_LONG_UP;
+ LONG_DOWN = TURBO_3_LONG_DOWN;
+ SHORT_UP = TURBO_3_SHORT_UP;
+ SHORT_DOWN = TURBO_3_SHORT_DOWN;
+ break;
+ case 4: // Fastest in turbo mode. Probably unstable...
+ LONG_UP = TURBO_FAST_LONG_UP;
+ LONG_DOWN = TURBO_FAST_LONG_DOWN + corr_2;
+ SHORT_UP = TURBO_FAST_SHORT_UP;
+ SHORT_DOWN = TURBO_FAST_SHORT_DOWN + corr_2;
+ break;
+ default: // Normal mode.
+ LONG_UP = DEFAULT_LONG_UP;
+ LONG_DOWN = DEFAULT_LONG_DOWN;
+ SHORT_UP = DEFAULT_SHORT_UP;
+ SHORT_DOWN = DEFAULT_SHORT_DOWN;
+ }//switch
+}//setspeed
+
+// Write a byte and count the ones for the checksum.
+word writebyte(byte b) {
+ word cs = 0x0;
+ byte i = 0x0;
+
+ for (i = 0xff; i; i >>= 1) {
+ if (b & 0x80) {
+ lp();
+ cs++;
+ }//if
+ else
+ sp();
+ b <<= 1;
+ }//for
+ lp();
+ return cs;
+}//writebyte
+
+// Get the file size.
+word getfilesize(byte *image) {
+ return image[0x12] | (image[0x13] << 8);
+}//getfilesize
+
+// See if the MZF file is valid.
+int assert(byte *image, word i) {
+ word fs = getfilesize(image);
+
+ if (fs + 0x80 != i) {
+ if (i - fs > 0x200)
+ return 2;
+ if (i < fs)
+ return 2;
+ return 1;
+ }//if
+ return 0;
+}//assert