简体   繁体   English

在C#中模拟一个memory空间

[英]Simulate a memory space in C#

I want to simulate a flash memory architecture in C#. More specifically the architecture looks like the following:我想在 C# 中模拟一个 flash memory 架构。更具体地说,架构如下所示:

  • Flash memory is a collection of blocks Flash memory 是块的集合
  • 1 block = 128 sectors 1 个块 = 128 个扇区
  • a sector is composed of a data area and spare area一个扇区由数据区和备用区组成
  • data area = 8 kB数据区 = 8 kB
  • spare area = 16 B备用区 = 16 B

I wanted to represent this in a struct or in a class but the problem is I don't know how to represent a certain amount of memory space in the code.我想在结构或 class 中表示它,但问题是我不知道如何在代码中表示一定数量的 memory 空间。 I can't use int or char arrays since I don't know what is to be stored in that memory space.... I am not very sure but I think I can represent it using byte datatype....我不能使用 int 或 char arrays,因为我不知道该 memory 空间中要存储什么....我不太确定,但我认为我可以使用字节数据类型来表示它....

Yes, it sounds like you want a byte array.是的,听起来您想要一个字节数组。 For example:例如:

 public sealed class Block
 {
     private readonly Sector[] sectors = new Sector[128];

     public Sector this[int index] { get { return sectors[index]; } }
 }

 public sealed class Sector
 {
     private readonly byte[] data = new byte[8 * 1024];

     public byte this[int index]
     {
         get { return data[index]; }
         set { data[index] = value; }
     }
 }

(You can model the "spare" area as well if you want - it's not clear whether you really need to though.) (如果你愿意,你也可以拨打 model 到“备用”区域——但不清楚你是否真的需要这样做。)

That's only allowing single-byte-at-a-time access - you may well want to have GetData and SetData methods on Block which read/write chunks of data at a time.这只允许一次访问一个字节——您可能希望在Block上使用GetDataSetData方法,这些方法一次读/写数据块。 Hopefully this will get you started though.希望这会让你开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM