Programming CPU Emulator Functional Style [closed]

I want to write an 8086 CPU emulator in javascript, functional style.
How would one conceptualise / design an 8086 emulator, or any CPU emulator that has registers and realmode memory access in a functional way?

I can’t wrap my head around how an emulator that is pure / mostly side effect free in the core parts, like memory IO and registers, would work.

I have seen this Code Golf: Emulate an Intel 8086 CPU – In it, someone gave an answer in Haskell, so it seems possible.

5

You have a couple of stateful properties. Those are the registers and memory. And we have a transition between states. This is each CPU cycle. We can therefore model this architecture in a straightforward fashion:

function cycle(state: State) -> State { ... }

Our cycle function will treat the input state as an immutable object, and create a new state to return. This involves copying most data over to the new state. This is not a problem until you start caring about performance.

The CPU cycles until some halting state is reached. This could be expressed as a loop:

while(! state.isHaltingState()) {
    state = cycle(state);
}

which could of course also be expressed as infinite recursion.

Inside our cycle function, we locate, decode, and perform the current instruction. All instructions will at least mutate the instruction pointer. In JavaScript (which is not a pure functional language), the approach would be:

  1. Make a copy of the old registers
  2. Execute the current instruction, which possible mutates the current registers
  3. Treat the registers as immutable and add them to the next state
  4. Return the next state.

Unlike registers, memory (= an array of memory cells, e.g. bytes) will not necessarily be mutated during each instruction. If memory was not touched, we don’t have to perform a copy and can simply reference the old memory in the next state. If we do change any cell, we have to create an updated copy for use in the new state. The impact of this can be reduced by dividing memory into pages, and only replacing pages that were actually touched. Effectively, this approach turns the array into an n-ary tree of arrays of fixed depth.

I would model each instruction as a function that calculates a delta from the current state. This way, the code for applying the delta to the current state and returning a new state in an effective manner does not have to be repeated throughout the code. A delta could be a small object such as { registers: { eax: 0x42}, memory: {0x1234: 0x00} }.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật