mirror of
https://github.com/a2-4am/wozardry.git
synced 2024-05-31 16:41:29 +00:00
Move guts of Track() class back from moofimage.py to wozimage.py. The Track() class bit and nibble processing methods are useful in both, and since MoofTrack is a subclass of Track, it seems worthwile to keep the methods in the base class.
This commit is contained in:
parent
82352e50f9
commit
5d87003d90
56
moofimage.py
56
moofimage.py
|
@ -13,62 +13,6 @@ def myhex(b):
|
||||||
class MoofTrack(wozardry.Track):
|
class MoofTrack(wozardry.Track):
|
||||||
def __init__(self, raw_bytes, raw_count):
|
def __init__(self, raw_bytes, raw_count):
|
||||||
wozardry.Track.__init__(self, raw_bytes, raw_count)
|
wozardry.Track.__init__(self, raw_bytes, raw_count)
|
||||||
self.bit_index = 0
|
|
||||||
self.revolutions = 0
|
|
||||||
self.bits = bitarray.bitarray(endian="big")
|
|
||||||
self.bits.frombytes(self.raw_bytes)
|
|
||||||
while len(self.bits) > raw_count:
|
|
||||||
self.bits.pop()
|
|
||||||
|
|
||||||
def rewind(self, bit_count=1):
|
|
||||||
self.bit_index -= bit_count
|
|
||||||
while self.bit_index < 0:
|
|
||||||
self.bit_index += self.raw_count
|
|
||||||
self.revolutions -= 1
|
|
||||||
|
|
||||||
def forward(self, bit_count=1):
|
|
||||||
self.bit_index += bit_count
|
|
||||||
while self.bit_index >= self.raw_count:
|
|
||||||
self.bit_index -= self.raw_count
|
|
||||||
self.revolutions += 1
|
|
||||||
|
|
||||||
def bit(self):
|
|
||||||
b = self.bits[self.bit_index]
|
|
||||||
self.forward()
|
|
||||||
yield b
|
|
||||||
|
|
||||||
def nibble(self):
|
|
||||||
while not next(self.bit()): pass
|
|
||||||
n = 0b10000000
|
|
||||||
for bit_index in range(6, -1, -1):
|
|
||||||
b = next(self.bit())
|
|
||||||
n |= b << bit_index
|
|
||||||
yield n
|
|
||||||
|
|
||||||
def find(self, sequence):
|
|
||||||
starting_revolutions = self.revolutions
|
|
||||||
seen = [0] * len(sequence)
|
|
||||||
while (self.revolutions < starting_revolutions + 2):
|
|
||||||
del seen[0]
|
|
||||||
seen.append(next(self.nibble()))
|
|
||||||
if tuple(seen) == tuple(sequence): return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
def find_this_not_that(self, good, bad):
|
|
||||||
starting_revolutions = self.revolutions
|
|
||||||
good = tuple(good)
|
|
||||||
bad = tuple(bad)
|
|
||||||
seen_good = [0] * len(good)
|
|
||||||
seen_bad = [0] * len(bad)
|
|
||||||
while (self.revolutions < starting_revolutions + 2):
|
|
||||||
del seen_good[0]
|
|
||||||
del seen_bad[0]
|
|
||||||
n = next(self.nibble())
|
|
||||||
seen_good.append(n)
|
|
||||||
seen_bad.append(n)
|
|
||||||
if tuple(seen_bad) == bad: return False
|
|
||||||
if tuple(seen_good) == good: return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
class MoofAddressField:
|
class MoofAddressField:
|
||||||
def __init__(self, valid, volume, track_id, sector_id):
|
def __init__(self, valid, volume, track_id, sector_id):
|
||||||
|
|
58
wozardry.py
58
wozardry.py
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import binascii
|
import binascii
|
||||||
|
import bitarray # https://pypi.org/project/bitarray/
|
||||||
import collections
|
import collections
|
||||||
import io
|
import io
|
||||||
import json
|
import json
|
||||||
|
@ -160,6 +161,63 @@ class Track:
|
||||||
self.raw_bytes = raw_bytes
|
self.raw_bytes = raw_bytes
|
||||||
self.raw_count = raw_count
|
self.raw_count = raw_count
|
||||||
|
|
||||||
|
self.bits = bitarray.bitarray(endian="big")
|
||||||
|
self.bits.frombytes(self.raw_bytes)
|
||||||
|
while len(self.bits) > raw_count:
|
||||||
|
self.bits.pop()
|
||||||
|
self.bit_index = 0
|
||||||
|
self.revolutions = 0
|
||||||
|
|
||||||
|
def rewind(self, bit_count=1):
|
||||||
|
self.bit_index -= bit_count
|
||||||
|
while self.bit_index < 0:
|
||||||
|
self.bit_index += self.raw_count
|
||||||
|
self.revolutions -= 1
|
||||||
|
|
||||||
|
def forward(self, bit_count=1):
|
||||||
|
self.bit_index += bit_count
|
||||||
|
while self.bit_index >= self.raw_count:
|
||||||
|
self.bit_index -= self.raw_count
|
||||||
|
self.revolutions += 1
|
||||||
|
|
||||||
|
def bit(self):
|
||||||
|
b = self.bits[self.bit_index]
|
||||||
|
self.forward()
|
||||||
|
yield b
|
||||||
|
|
||||||
|
def nibble(self):
|
||||||
|
while not next(self.bit()): pass
|
||||||
|
n = 0b10000000
|
||||||
|
for bit_index in range(6, -1, -1):
|
||||||
|
b = next(self.bit())
|
||||||
|
n |= b << bit_index
|
||||||
|
yield n
|
||||||
|
|
||||||
|
def find(self, sequence):
|
||||||
|
starting_revolutions = self.revolutions
|
||||||
|
seen = [0] * len(sequence)
|
||||||
|
while (self.revolutions < starting_revolutions + 2):
|
||||||
|
del seen[0]
|
||||||
|
seen.append(next(self.nibble()))
|
||||||
|
if tuple(seen) == tuple(sequence): return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def find_this_not_that(self, good, bad):
|
||||||
|
starting_revolutions = self.revolutions
|
||||||
|
good = tuple(good)
|
||||||
|
bad = tuple(bad)
|
||||||
|
seen_good = [0] * len(good)
|
||||||
|
seen_bad = [0] * len(bad)
|
||||||
|
while (self.revolutions < starting_revolutions + 2):
|
||||||
|
del seen_good[0]
|
||||||
|
del seen_bad[0]
|
||||||
|
n = next(self.nibble())
|
||||||
|
seen_good.append(n)
|
||||||
|
seen_bad.append(n)
|
||||||
|
if tuple(seen_bad) == bad: return False
|
||||||
|
if tuple(seen_good) == good: return True
|
||||||
|
return False
|
||||||
|
|
||||||
class WozDiskImage:
|
class WozDiskImage:
|
||||||
def __init__(self, iostream=None):
|
def __init__(self, iostream=None):
|
||||||
if iostream:
|
if iostream:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user