OpenCores
URL https://opencores.org/ocsvn/fpga-cf/fpga-cf/trunk

Subversion Repositories fpga-cf

[/] [fpga-cf/] [trunk/] [java/] [src/] [edu/] [byu/] [cc/] [plieber/] [fpgaenet/] [icapif/] [IcapInterface.java] - Blame information for rev 10

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 peteralieb
/**
2
 *
3
 */
4
package edu.byu.cc.plieber.fpgaenet.icapif;
5
 
6
import java.io.File;
7
import java.io.FileInputStream;
8
import java.io.FileNotFoundException;
9
import java.io.IOException;
10
import java.io.InputStream;
11
import java.net.InetAddress;
12
import java.util.ArrayList;
13
 
14
import edu.byu.cc.plieber.fpgaenet.fcp.FCPException;
15
import edu.byu.cc.plieber.fpgaenet.fcp.FCPProtocol;
16
 
17
/**
18
 * @author plieber
19
 *
20
 * @version 0.1
21
 */
22
public class IcapInterface {
23
 
24
        protected FCPProtocol fcpprotocol;
25
 
26
        protected int icapWritePort = 3;
27
        protected int icapReadPort = 4;
28
 
29
        /**
30
         * Create IcapInterface, attached to the given FCP layer, with the default
31
         * FCP ports for the ICAP functions.
32
         *
33
         * @param fcpprotocol
34
         */
35
        public IcapInterface(FCPProtocol fcpprotocol) {
36
                this.init(fcpprotocol, 3, 4);
37
        }
38
 
39
        /**
40
         * Create IcapInterface, attached to the given FCP layer, with the given
41
         * FCP ports for the ICAP functions.
42
         *
43
         * @param fcpprotocol
44
         * @param icapWritePort
45
         * @param icapReadPort
46
         */
47
        public IcapInterface(FCPProtocol fcpprotocol, int icapWritePort,
48
                        int icapReadPort) {
49
                this.init(fcpprotocol, icapWritePort, icapReadPort);
50
        }
51
 
52
        /**
53
         * Initialize this object
54
         * @param fcpprotocol
55
         * @param icapWritePort
56
         * @param icapReadPort
57
         */
58
        private void init(FCPProtocol fcpprotocol, int icapWritePort,
59
                        int icapReadPort) {
60
                this.fcpprotocol = fcpprotocol;
61
                this.icapReadPort = icapReadPort;
62
                this.icapWritePort = icapWritePort;
63
        }
64
 
65
        @Override
66
        public String toString() {
67
                return "ICAP Interface< " + this.fcpprotocol.toString() + " >";
68
        }
69
 
70
        /**
71
         * Send a list of bytes to the ICAP.
72
         * @param bytes
73 7 peteralieb
         * @throws FCPException
74 2 peteralieb
         */
75 7 peteralieb
        public void sendIcapData(java.util.List<Byte> bytes) throws FCPException {
76
                this.fcpprotocol.send(this.icapWritePort, bytes, bytes.size());
77 2 peteralieb
        }
78
 
79
        /**
80
         * Send an array of byte to the ICAP.
81
         * @param bytes
82
         * @throws FCPException
83
         */
84
        public void sendIcapData(byte[] bytes, int numBytes) throws FCPException {
85
                byte[] writereg = new byte[1024];
86
                int offset = 0;
87
                int numRead = 0;
88
                while (offset < numBytes) {
89
                        for (int i = 0; i < 1024 && offset < numBytes; i++, offset++) {
90
                                writereg[i] = bytes[offset];
91
                                numRead++;
92
                        }
93
                        this.fcpprotocol.send(3, writereg, numRead);
94
                }
95
        }
96
 
97
        public void sendIcapFile(String fileName) throws FCPException {
98
                try {
99
                        File file = new File(fileName);
100
                        InputStream is = new FileInputStream(file);
101
                        long length = file.length();
102
                        if (length > Integer.MAX_VALUE) {
103
 
104
                        }
105
 
106
                        byte[] writereg = new byte[1024];
107
                        int offset = 0;
108
                        int numRead = 0;
109
                        while (offset < file.length()
110
                                        && (numRead = is.read(writereg, 0, 1024)) >= 0) {
111
                                offset += numRead;
112 7 peteralieb
                                fcpprotocol.send(3, writereg, numRead);
113 2 peteralieb
                        }
114
                } catch (FileNotFoundException e) {
115
                        throw new FCPException("File not found: " + fileName);
116
                } catch (IOException e) {
117
                        throw new FCPException("Error reading file: " + fileName);
118
                }
119
        }
120
 
121
        /**
122
         * Send a data request for ICAP data from the ICAP controller.
123
         * @param numBytes The number of bytes expected at the next receiveIcapData function call.
124
         * @throws FCPException
125
         */
126
        public void requestIcapData(int numBytes) throws FCPException {
127
                byte[] read4 = new byte[] { (byte) ((numBytes >> 8) & 0xff), (byte) (numBytes & 0xff) };
128
                this.fcpprotocol.send(this.icapReadPort, read4, read4.length);
129
                this.fcpprotocol.sendDataRequest(this.icapReadPort, numBytes);
130
        }
131
 
132
        /**
133
         * Receive a list of bytes from the ICAP.
134
         * @return List of bytes returned from the ICAP.
135
         */
136
        public ArrayList<Byte> receiveIcapData() {
137
                ArrayList<Byte> ret = new ArrayList<Byte>();
138
                byte[] bytes = this.fcpprotocol.getDataResponse();
139
                for (int i = 0; i < bytes.length; i++) {
140
                        ret.add(bytes[i]);
141
                }
142
                return ret;
143
        }
144
 
145
        /**
146
         * Read a certain number of bytes from the ICAP. This function encapsulates calls to both requestIcapData and receiveIcapData.
147
         * @param numBytes The number of bytes expected
148
         * @return List of bytes returned from the ICAP
149
         * @throws FCPException
150
         */
151
        public ArrayList<Byte> read(int numBytes) throws FCPException {
152
                this.requestIcapData(numBytes);
153
                return this.receiveIcapData();
154
        }
155
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.