File size: 2,224 Bytes
684cc60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
Virtual Driver Module - Translates write commands into block operations
"""
import threading
from typing import Optional

class VirtualDriver:
    """
    Simulates a device driver that translates high-level write commands
    into block-level operations for the SSD controller.
    """
    
    def __init__(self, vvd, block_size: int = 4096):
        self.vvd = vvd
        self.block_size = block_size
        self.lock = threading.RLock()
        
        print(f"VirtualDriver initialized with block size {block_size} bytes")
    
    def send_write(self, logical_block: int, data: bytes) -> bool:
        """
        Send a write command to the SSD controller.
        Ensures data is properly sized for the block.
        """
        with self.lock:
            if len(data) > self.block_size:
                print(f"Warning: Data size {len(data)} exceeds block size {self.block_size}. Truncating.")
                data = data[:self.block_size]
            elif len(data) < self.block_size:
                # Pad with zeros to fill the block
                data = data + b'\x00' * (self.block_size - len(data))
            
            return self.vvd.write_page(logical_block, data)
    
    def send_read(self, logical_block: int) -> Optional[bytes]:
        """
        Send a read command to the SSD controller.
        """
        with self.lock:
            return self.vvd.read_page(logical_block)
    
    def send_trim(self, logical_block: int):
        """
        Send a TRIM command to the SSD controller.
        """
        with self.lock:
            self.vvd.erase_block(logical_block)
    
    def get_driver_stats(self) -> dict:
        """
        Get driver statistics (mostly pass-through to SSD controller).
        """
        return {
            "flash_stats": self.vvd.pvd_flash.get_flash_stats(),
            "ftl_stats": self.vvd.pvd_controller.get_ftl_stats(),
            "file_system_stats": self.vvd.pvd_file_system.get_usage_stats()
        }
    
    def shutdown(self):
        """
        Shutdown the driver.
        """
        print("VirtualDriver shutdown complete")
    
    def __del__(self):
        try:
            self.shutdown()
        except:
            pass