wiremod / wire

Garry's Mod add-on that allows users to wire up components in order to make more elaborate automatic and user-controlled contraptions.
http://www.wiremod.com
Apache License 2.0
549 stars 333 forks source link

E2 Incoming hispeed read/write events #3106

Closed DerelictDrone closed 1 month ago

DerelictDrone commented 1 month ago

Part of wirelink.lua

Adds events for read, and write

event readCell(Address:number) {}
event writeCell(Address:number, Value:number) {}

Three functions for use inside of these events.

void hispeedReturnValue(Value:number)

Returns Value when exiting a readCell event, also clears hispeed error flag

void hispeedSetError(Value:number)

If nonzero, indicates that the request couldn't be satisfied, doesn't error your chip but may raise errors in things like ZCPU which use return of nil/false from read/write to indicate out of range or other errors.

DerelictDrone commented 1 month ago

Self read/write works btw, here's an example program for using internal memory first and then an external memory device, like how ZCPU read/write works

@inputs ExternalMem:wirelink
@outputs Hispeed:number 
@persist Memory:array Ptr:number InternalMem:wirelink
@strict

Memory = array()
InternalMem = wirelink()
Ptr = 1

event readCell(Address:number) {
    if(Address > 128) {
        returnReadValue(ExternalMem[Address-128])
    } else {
        returnReadValue(Memory[Address,number])
    }
}

event writeCell(Address:number, Value:number) {
    if(Address > 128) {
        ExternalMem[Address-128] = Value
    } else {
        Memory[Address] = Value
    }
}

event tick() {
    Hispeed = InternalMem[Ptr]
    InternalMem[Ptr] = Ptr
    Ptr++
    Ptr = Ptr%256
}
deltamolfar commented 1 month ago

Isn't read/write cell functions deprecated for some reason?

WallopingEwe commented 1 month ago

Isn't read/write cell functions deprecated for some reason?

Don't all of the memory devices such as the RAM gates and EEPROM and DHDD and the highspeed read/write gates use these to function.

DerelictDrone commented 1 month ago

Isn't read/write cell functions deprecated for some reason?

xwl:readCell(n) and xwl:writeCell(nn) are, because they were replaced by wirelink's [] operator.

Wirelink [] still calls ReadCell and WriteCell on entities, this just adds events that are executed when something calls ReadCell and WriteCell on this E2's chip

Vurv78 commented 1 month ago

Just FYI, you can pass an event constructor to E2Lib.registerEvent for this that runs on the chip initialization if the event is present (which passes the e2's context). That's what chipUsed does. So you don't need a redundant registered_events check slowing the runtime down. I didn't see the rest, maybe could've been written simpler if you knew this

DerelictDrone commented 1 month ago

Just FYI, you can pass an event constructor to E2Lib.registerEvent for this that runs on the chip initialization if the event is present (which passes the e2's context). That's what chipUsed does. So you don't need a redundant registered_events check slowing the runtime down. I didn't see the rest, maybe could've been written simpler if you knew this

I had a constructor and destructor like chipUsed does, but tried to cache any previously existing readcell/writecell(non-strict, error if they existed previously in strict), my reasoning being to potentially save anyone who's trying to run E2 scripts using another entity from overwriting and then losing that entity's original readcell or writecell.

Constructor/destructors were removed here, in this commit if you wanted to see them.