3 * Common Driver/Filesystem Helper Functions
7 #include <tpl_drv_disk.h>
8 #include <tpl_drv_video.h>
11 // --- Video Driver Helpers ---
12 Uint64 DrvUtil_Video_2DStream(void *Ent, void *Buffer, int Length,
13 tDrvUtil_Video_2DHandlers *Handlers, int SizeofHandlers)
15 Uint8 *stream = Buffer;
23 if(op > NUM_VIDEO_2DOPS) {
24 Log_Warning("DrvUtil", "DrvUtil_Video_2DStream: Unknown"
28 if(op*4 > SizeofHandlers) {
29 Log_Warning("DrvUtil", "DrvUtil_Video_2DStream: Driver does"
30 " not support op %i", op);
36 case VIDEO_2DOP_NOP: break;
39 if(rem < 12) return Length-rem;
42 Log_Warning("DrvUtil", "DrvUtil_Video_2DStream: Driver"
43 " does not support VIDEO_2DOP_FILL");
49 *(Uint16*)(&stream[0]), *(Uint16*)(&stream[2]),
50 *(Uint16*)(&stream[4]), *(Uint16*)(&stream[6]),
51 *(Uint32*)(&stream[8])
59 if(rem < 12) return Length-rem;
62 Log_Warning("DrvUtil", "DrvUtil_Video_2DStream: Driver"
63 " does not support VIDEO_2DOP_BLIT");
69 *(Uint16*)(&stream[0]), *(Uint16*)(&stream[2]),
70 *(Uint16*)(&stream[4]), *(Uint16*)(&stream[6]),
71 *(Uint16*)(&stream[8]), *(Uint16*)(&stream[10])
83 // --- Disk Driver Helpers ---
84 Uint64 DrvUtil_ReadBlock(Uint64 Start, Uint64 Length, void *Buffer,
85 tDrvUtil_Callback ReadBlocks, Uint64 BlockSize, Uint Argument)
87 Uint8 tmp[BlockSize]; // C99
88 Uint64 block = Start / BlockSize;
89 int offset = Start - block * BlockSize;
90 int leading = BlockSize - offset;
95 ENTER("XStart XLength pBuffer pReadBlocks XBlockSize xArgument",
96 Start, Length, Buffer, ReadBlocks, BlockSize, Argument);
98 // Non aligned start, let's fix that!
101 if(leading > Length) leading = Length;
102 LOG("Reading %i bytes from Block1+%i", leading, offset);
103 ret = ReadBlocks(block, 1, tmp, Argument);
108 memcpy( Buffer, &tmp[offset], leading );
110 if(leading == Length) {
117 num = ( Length - leading ) / BlockSize;
118 tailings = Length - num * BlockSize - leading;
121 num = Length / BlockSize;
122 tailings = Length % BlockSize;
125 // Read central blocks
128 LOG("Reading %i blocks", num);
129 ret = ReadBlocks(block, num, Buffer, Argument);
131 LEAVE('X', leading + ret * BlockSize);
132 return leading + ret * BlockSize;
136 // Read last tailing block
139 LOG("Reading %i bytes from last block", tailings);
141 Buffer += num * BlockSize;
142 ret = ReadBlocks(block, 1, tmp, Argument);
144 LEAVE('X', leading + num * BlockSize);
145 return leading + num * BlockSize;
147 memcpy( Buffer, tmp, tailings );
154 Uint64 DrvUtil_WriteBlock(Uint64 Start, Uint64 Length, void *Buffer,
155 tDrvUtil_Callback ReadBlocks, tDrvUtil_Callback WriteBlocks,
156 Uint64 BlockSize, Uint Argument)
158 Uint8 tmp[BlockSize]; // C99
159 Uint64 block = Start / BlockSize;
160 int offset = Start - block * BlockSize;
161 int leading = BlockSize - offset;
166 ENTER("XStart XLength pBuffer pReadBlocks pWriteBlocks XBlockSize xArgument",
167 Start, Length, Buffer, ReadBlocks, WriteBlocks, BlockSize, Argument);
169 // Non aligned start, let's fix that!
172 if(leading > Length) leading = Length;
173 LOG("Writing %i bytes to Block1+%i", leading, offset);
174 // Read a copy of the block
175 ret = ReadBlocks(block, 1, tmp, Argument);
181 memcpy( &tmp[offset], Buffer, leading );
183 ret = WriteBlocks(block, 1, tmp, Argument);
189 if(leading == Length) {
196 num = ( Length - leading ) / BlockSize;
197 tailings = Length - num * BlockSize - leading;
200 num = Length / BlockSize;
201 tailings = Length % BlockSize;
204 // Read central blocks
207 LOG("Writing %i blocks", num);
208 ret = WriteBlocks(block, num, Buffer, Argument);
210 LEAVE('X', leading + ret * BlockSize);
211 return leading + ret * BlockSize;
215 // Read last tailing block
218 LOG("Writing %i bytes to last block", tailings);
220 Buffer += num * BlockSize;
222 ret = ReadBlocks(block, 1, tmp, Argument);
224 LEAVE('X', leading + num * BlockSize);
225 return leading + num * BlockSize;
228 memcpy( tmp, Buffer, tailings );
230 ret = WriteBlocks(block, 1, tmp, Argument);
232 LEAVE('X', leading + num * BlockSize);
233 return leading + num * BlockSize;