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");
67 //Log("Handlers->Blit{%}}(%p, %i,%i, %i,%i, %i,%i)",
68 // Handlers->Blit, Ent,
69 // *(Uint16*)(&stream[0]), *(Uint16*)(&stream[2]),
70 // *(Uint16*)(&stream[4]), *(Uint16*)(&stream[6]),
71 // *(Uint16*)(&stream[8]), *(Uint16*)(&stream[10])
75 *(Uint16*)(&stream[0]), *(Uint16*)(&stream[2]),
76 *(Uint16*)(&stream[4]), *(Uint16*)(&stream[6]),
77 *(Uint16*)(&stream[8]), *(Uint16*)(&stream[10])
89 // --- Disk Driver Helpers ---
90 Uint64 DrvUtil_ReadBlock(Uint64 Start, Uint64 Length, void *Buffer,
91 tDrvUtil_Callback ReadBlocks, Uint64 BlockSize, Uint Argument)
93 Uint8 tmp[BlockSize]; // C99
94 Uint64 block = Start / BlockSize;
95 int offset = Start - block * BlockSize;
96 int leading = BlockSize - offset;
101 ENTER("XStart XLength pBuffer pReadBlocks XBlockSize xArgument",
102 Start, Length, Buffer, ReadBlocks, BlockSize, Argument);
104 // Non aligned start, let's fix that!
107 if(leading > Length) leading = Length;
108 LOG("Reading %i bytes from Block1+%i", leading, offset);
109 ret = ReadBlocks(block, 1, tmp, Argument);
114 memcpy( Buffer, &tmp[offset], leading );
116 if(leading == Length) {
123 num = ( Length - leading ) / BlockSize;
124 tailings = Length - num * BlockSize - leading;
127 num = Length / BlockSize;
128 tailings = Length % BlockSize;
131 // Read central blocks
134 LOG("Reading %i blocks", num);
135 ret = ReadBlocks(block, num, Buffer, Argument);
137 LEAVE('X', leading + ret * BlockSize);
138 return leading + ret * BlockSize;
142 // Read last tailing block
145 LOG("Reading %i bytes from last block", tailings);
147 Buffer += num * BlockSize;
148 ret = ReadBlocks(block, 1, tmp, Argument);
150 LEAVE('X', leading + num * BlockSize);
151 return leading + num * BlockSize;
153 memcpy( Buffer, tmp, tailings );
160 Uint64 DrvUtil_WriteBlock(Uint64 Start, Uint64 Length, void *Buffer,
161 tDrvUtil_Callback ReadBlocks, tDrvUtil_Callback WriteBlocks,
162 Uint64 BlockSize, Uint Argument)
164 Uint8 tmp[BlockSize]; // C99
165 Uint64 block = Start / BlockSize;
166 int offset = Start - block * BlockSize;
167 int leading = BlockSize - offset;
172 ENTER("XStart XLength pBuffer pReadBlocks pWriteBlocks XBlockSize xArgument",
173 Start, Length, Buffer, ReadBlocks, WriteBlocks, BlockSize, Argument);
175 // Non aligned start, let's fix that!
178 if(leading > Length) leading = Length;
179 LOG("Writing %i bytes to Block1+%i", leading, offset);
180 // Read a copy of the block
181 ret = ReadBlocks(block, 1, tmp, Argument);
187 memcpy( &tmp[offset], Buffer, leading );
189 ret = WriteBlocks(block, 1, tmp, Argument);
195 if(leading == Length) {
202 num = ( Length - leading ) / BlockSize;
203 tailings = Length - num * BlockSize - leading;
206 num = Length / BlockSize;
207 tailings = Length % BlockSize;
210 // Read central blocks
213 LOG("Writing %i blocks", num);
214 ret = WriteBlocks(block, num, Buffer, Argument);
216 LEAVE('X', leading + ret * BlockSize);
217 return leading + ret * BlockSize;
221 // Read last tailing block
224 LOG("Writing %i bytes to last block", tailings);
226 Buffer += num * BlockSize;
228 ret = ReadBlocks(block, 1, tmp, Argument);
230 LEAVE('X', leading + num * BlockSize);
231 return leading + num * BlockSize;
234 memcpy( tmp, Buffer, tailings );
236 ret = WriteBlocks(block, 1, tmp, Argument);
238 LEAVE('X', leading + num * BlockSize);
239 return leading + num * BlockSize;