NHMLFixup v10
[jpcrr.git] / org / jpc / output / OutputFrame.java
blob62ed0534bfe4a91f886de724caccfcca721f434d
1 /*
2 JPC-RR: A x86 PC Hardware Emulator
3 Release 1
5 Copyright (C) 2007-2009 Isis Innovation Limited
6 Copyright (C) 2009-2010 H. Ilari Liusvaara
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as published by
10 the Free Software Foundation.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 Based on JPC x86 PC Hardware emulator,
22 A project from the Physics Dept, The University of Oxford
24 Details about original JPC can be found at:
26 www-jpc.physics.ox.ac.uk
30 package org.jpc.output;
32 import java.io.*;
34 public abstract class OutputFrame
36 private long time;
37 private byte minor;
39 public OutputFrame(long timeStamp, byte minorType)
41 time = timeStamp;
42 minor = minorType;
45 protected void adjustTime(long delta)
47 time += delta;
50 public byte[] dump(short channel, long timeBase) throws IOException
52 int len = 8;
53 int llen = 1;
54 int skips = 0;
55 if(channel == -1)
56 throw new IOException("Channel 0xFFFF is reserved");
57 long delta = time - timeBase;
58 if(delta < 0)
59 throw new IOException("Event times must be monotonic");
61 byte[] internal = dumpInternal();
63 //Calculate length of frame and allocate it.
64 while(delta >= 0xFFFFFFFFL) {
65 skips++;
66 len += 6;
67 delta -= 0xFFFFFFFFL;
69 int ilen = (internal != null) ? internal.length : 0;
70 while(ilen > 127) {
71 len++;
72 llen++;
73 ilen >>>= 7;
75 len += ((internal != null) ? internal.length : 0);
76 byte[] frame = new byte[len];
78 //Write the frame.
79 int offset = 6 * skips;
80 for(int i = 0; i < offset; i++)
81 frame[i] = (byte)255;
82 frame[offset + 0] = (byte)((channel >>> 8) & 0xFF);
83 frame[offset + 1] = (byte)(channel & 0xFF);
84 frame[offset + 2] = (byte)((delta >>> 24) & 0xFF);
85 frame[offset + 3] = (byte)((delta >>> 16) & 0xFF);
86 frame[offset + 4] = (byte)((delta >>> 8) & 0xFF);
87 frame[offset + 5] = (byte)(delta & 0xFF);
88 frame[offset + 6] = minor;
89 ilen = (internal != null) ? internal.length : 0;
90 for(int i = llen; i > 0; i--) {
91 frame[offset + 6 + i] = (byte)(((i == llen) ? 0x00 : 0x80) | (ilen & 0x7F));
92 ilen >>>= 7;
94 if(internal != null)
95 System.arraycopy(internal, 0, frame, offset + 7 + llen, internal.length);
96 return frame;
99 public long getTime()
101 return time;
104 protected abstract byte[] dumpInternal();