
MODULE keyfile_180_to_sim_format;
?? SET (CHKALL := OFF) ??
     {! ! ! no checking ! ! !}
?? PUSH (LISTEXT := ON) ??
*copyc PXIOTYP
*callall biz
*callall fz
?? POP ??
?? SKIP := 3 ??
{--------------------------------------------------------------------}
{              T Y P E S                                     }

  TYPE
    b1 = 0 .. 1,
    b4 = 0 .. 0f(16),
    b8 = 0 .. 0ff(16),
    b16 = 0 .. 0ffff(16),
    b23 = 0 .. 7fffff(16),
    b24 = 0 .. 0ffffff(16),
    b27 = 0 .. 07ffffff(16),
    b32 = 0 .. 0ffffffff(16),
    b12 = 0 .. 0fff(16),
    b15 = 0 .. 7fff(16),
    b7 = 0 .. 7f(16),
    b20 = 0 .. 0fffff(16);

  CONST
    bufsize = 100,
    sh4 = 10(16),
    s8 = 100(16),
    s16 = 10000(16),
    s24 = 1000000(16),
    s12 = 1000(16),
    s20 = 100000(16);


  TYPE
    pmf_record_type = (pmfr_keypoint, pmfr_overflow),
    pmf_record = record
      rec_type: pmf_record_type,
      time: 0 .. 3ffffff(16),
      class: 0 .. 0f(16),
      kcode: 0 .. 0ffffffff(16),
    recend;


  TYPE
    simblock = record
      s1,
      s2,
      s3,
      s4,
      s5,
      s6,
      s7: pmf_record,
    recend,
    packed_b56 = packed record
      f1: b4,
      o1: pmf_record_type,
      t1: b27,
      c1: b4,
      d1l: b24,
      f2: b4,
      d1r: b8,
      o2: pmf_record_type,
      t2: b27,
      c2: b4,
      d2l: b16,
      f3: b4,
      d2r: b16,
      o3: pmf_record_type,
      t3: b27,
      c3: b4,
      d3l: b8,
      f4: b4,
      d3r: b24,
      o4: pmf_record_type,
      t4: b27,
      c4: b4,
      f5: b4,
      d4: b32,
      o5: pmf_record_type,
      t5l: b23,
      f6: b4,
      t5r: b4,
      c5: b4,
      d5: b32,
      o6: pmf_record_type,
      t6l: b15,
      f7: b4,
      t6r: b12,
      c6: b4,
      d6: b32,
      o7: pmf_record_type,
      t7l: b7,
      f8: b4,
      t7r: b20,
      c7: b4,
      d7: b32,
    recend;

?? EJECT ??

  PROCEDURE convert (VAR h: packed_b56;
    VAR s: simblock);
    s.s1.rec_type := h.o1;
    s.s1.time := h.t1;
    s.s1.class := h.c1;
    s.s1.kcode := h.d1l * s8 + h.d1r;
    s.s2.rec_type := h.o2;
    s.s2.time := h.t2;
    s.s2.class := h.c2;
    s.s2.kcode := h.d2l * s16 + h.d2r;
    s.s3.rec_type := h.o3;
    s.s3.time := h.t3;
    s.s3.class := h.c3;
    s.s3.kcode := h.d3l * s24 + h.d3r;
    s.s4.rec_type := h.o4;
    s.s4.time := h.t4;
    s.s4.class := h.c4;
    s.s4.kcode := h.d4;
    s.s5.rec_type := h.o5;
    s.s5.time := h.t5l * sh4 + h.t5r;
    s.s5.class := h.c5;
    s.s5.kcode := h.d5;
    s.s6.rec_type := h.o6;
    s.s6.time := h.t6l * s12 + h.t6r;
    s.s6.class := h.c6;
    s.s6.kcode := h.d6;
    s.s7.rec_type := h.o7;
    s.s7.time := h.t7l * s20 + h.t7r;
    s.s7.class := h.c7;
    s.s7.kcode := h.d7;
  PROCEND convert;
?? EJECT ??

  PROGRAM [XDCL] main;

    VAR
      inbuf: array [1 .. bufsize] of packed_b56,
      outbuf: array [1 .. bufsize] of simblock,
      keypoints: integer,
      r: [STATIC] array [0 .. 7] of integer := [0, 0, 1, 2, 3, 4, 5, 6],
      words: integer,
      infile: file,
      outfile: file,
      i: integer;

    bi#open (infile, 'keyfile', old#, input#, first#);
    bi#open (outfile, 'sessmkf', new#, output#, first#);

    bi#get (infile, #LOC (inbuf), #SIZE (inbuf));
    f#words (infile, words);
    WHILE words <> 0 DO
      FOR i := 1 TO bufsize DO
        convert (inbuf [i], outbuf [i]);
      FOREND;
      keypoints := (words DIV 8) * 7 + r [words MOD 8];
      bi#put (outfile, #LOC (outbuf), #SIZE (pmf_record) * keypoints);
      bi#get (infile, #LOC (inbuf), #SIZE (inbuf));
      f#words (infile, words);
    WHILEND;

    bi#close (infile, first#);
    bi#close (outfile, first#);
  PROCEND main;
MODEND
